Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Resolved one of the Delaunay FIXMEs.

  • Loading branch information...
commit ffa76e4e98cd4de875c653c8e1bc4126837b29cf 1 parent a03cd8a
J. T. L. ironwallaby authored

Showing 2 changed files with 75 additions and 34 deletions. Show diff stats Hide diff stats

  1. +47 0 examples/2d.html
  2. +28 34 lib/delaunay.js
47 examples/2d.html
@@ -101,6 +101,53 @@
101 101 }
102 102
103 103 ctx.putImageData(id, 0, 0);
  104 +
  105 + ctx.strokeStyle = "yellow";
  106 + for(i = mesh.length; i; ) {
  107 + ctx.beginPath();
  108 + --i;
  109 + ctx.moveTo(
  110 + vertices[mesh[i]]["location"][0],
  111 + vertices[mesh[i]]["location"][1]
  112 + );
  113 +
  114 + --i;
  115 + ctx.lineTo(
  116 + vertices[mesh[i]]["location"][0],
  117 + vertices[mesh[i]]["location"][1]
  118 + );
  119 +
  120 + --i;
  121 + ctx.lineTo(
  122 + vertices[mesh[i]]["location"][0],
  123 + vertices[mesh[i]]["location"][1]
  124 + );
  125 + ctx.closePath();
  126 + ctx.stroke();
  127 + }
  128 +
  129 + for(i = vertices.length; i--; ) {
  130 + ctx.fillStyle =
  131 + "rgb(" +
  132 + vertices[i]["color"] +
  133 + "," +
  134 + vertices[i]["color"] +
  135 + "," +
  136 + vertices[i]["color"] +
  137 + ")";
  138 +
  139 + ctx.beginPath();
  140 + ctx.arc(
  141 + vertices[i]["location"][0],
  142 + vertices[i]["location"][1],
  143 + 4,
  144 + 0,
  145 + 2 * Math.PI,
  146 + false
  147 + );
  148 + ctx.fill();
  149 + ctx.stroke();
  150 + }
104 151 </script>
105 152 </body>
106 153 </html>
62 lib/delaunay.js
@@ -13,7 +13,8 @@ var Delaunay;
13 13 (function() {
14 14 "use strict";
15 15
16   - var Simplex = function(indices, vertices, n) {
  16 + var M = typeof Matrix !== "undefined" ? Matrix : require("./matrix"),
  17 + Simplex = function(indices, vertices, n) {
17 18 var list = new Array(indices.length),
18 19 i;
19 20
@@ -154,44 +155,37 @@ var Delaunay;
154 155
155 156 return matrix;
156 157 },
157   - /* FIXME: This can be genericized by making a vector, n + 1 long. For each
158   - * index in that vector, make a square matrix from the rectangular one by
159   - * omitting the respective column, and taking the determinant of that
160   - * matrix. Be sure to negate every odd-indexed cell (1, 3, etc.). */
161 158 cross: function(matrix, n) {
162 159 if(matrix.length !== n * n + n)
163 160 throw new Error("Invalid matrix shape.");
164 161
165   - switch(n) {
166   - case 2:
167   - return [
168   - matrix[1] * matrix[5] - matrix[2] * matrix[4],
169   - matrix[2] * matrix[3] - matrix[0] * matrix[5],
170   - matrix[0] * matrix[4] - matrix[1] * matrix[3]
171   - ];
172   -
173   - case 3:
174   - return [
175   - matrix[1] * (matrix[6] * matrix[11] - matrix[7] * matrix[10]) +
176   - matrix[2] * (matrix[7] * matrix[ 9] - matrix[5] * matrix[11]) +
177   - matrix[3] * (matrix[5] * matrix[10] - matrix[6] * matrix[ 9]) ,
178   -
179   - matrix[0] * (matrix[7] * matrix[10] - matrix[6] * matrix[11]) +
180   - matrix[2] * (matrix[4] * matrix[11] - matrix[7] * matrix[ 8]) +
181   - matrix[3] * (matrix[6] * matrix[ 8] - matrix[4] * matrix[10]) ,
182   -
183   - matrix[0] * (matrix[5] * matrix[11] - matrix[7] * matrix[ 9]) +
184   - matrix[1] * (matrix[7] * matrix[ 8] - matrix[4] * matrix[11]) +
185   - matrix[3] * (matrix[4] * matrix[ 9] - matrix[5] * matrix[ 8]) ,
186   -
187   - matrix[0] * (matrix[6] * matrix[ 9] - matrix[5] * matrix[10]) +
188   - matrix[1] * (matrix[4] * matrix[10] - matrix[6] * matrix[ 8]) +
189   - matrix[2] * (matrix[5] * matrix[ 8] - matrix[4] * matrix[ 9])
190   - ];
191   -
192   - default:
193   - throw new Error("FIXME: Delaunay.cross has not been generalized to 4 or more dimensions!");
  162 + var m = n + 1,
  163 + sgn = 1,
  164 + vec = new Array(m),
  165 + sq = new Array(n * n),
  166 + i, j, k;
  167 +
  168 + for(i = 0; i !== m; ++i) {
  169 + /* If it's the first time through the loop, initialize the square
  170 + * matrix to hold every column but the first. */
  171 + if(i === 0)
  172 + for(j = n; j--; )
  173 + for(k = n; k--; )
  174 + sq[j * n + k] = matrix[j * m + k + 1];
  175 +
  176 + /* Every other time, just replace the one column that's no longer
  177 + * relevant. */
  178 + else {
  179 + k = i - 1;
  180 + for(j = n; j--; )
  181 + sq[j * n + k] = matrix[j * m + k];
  182 + }
  183 +
  184 + vec[i] = sgn * M.determinant(sq);
  185 + sgn = -sgn;
194 186 }
  187 +
  188 + return vec;
195 189 },
196 190 /* FIXME: This should probably test the points for collinearity and use a
197 191 * different algorithm in that case, in order to improve robustness. */

0 comments on commit ffa76e4

Please sign in to comment.
Something went wrong with that request. Please try again.