Permalink
Browse files

Resolved one of the Delaunay FIXMEs.

  • Loading branch information...
1 parent a03cd8a commit ffa76e4e98cd4de875c653c8e1bc4126837b29cf @ironwallaby ironwallaby committed Feb 14, 2013
Showing with 75 additions and 34 deletions.
  1. +47 −0 examples/2d.html
  2. +28 −34 lib/delaunay.js
View
@@ -101,6 +101,53 @@
}
ctx.putImageData(id, 0, 0);
+
+ ctx.strokeStyle = "yellow";
+ for(i = mesh.length; i; ) {
+ ctx.beginPath();
+ --i;
+ ctx.moveTo(
+ vertices[mesh[i]]["location"][0],
+ vertices[mesh[i]]["location"][1]
+ );
+
+ --i;
+ ctx.lineTo(
+ vertices[mesh[i]]["location"][0],
+ vertices[mesh[i]]["location"][1]
+ );
+
+ --i;
+ ctx.lineTo(
+ vertices[mesh[i]]["location"][0],
+ vertices[mesh[i]]["location"][1]
+ );
+ ctx.closePath();
+ ctx.stroke();
+ }
+
+ for(i = vertices.length; i--; ) {
+ ctx.fillStyle =
+ "rgb(" +
+ vertices[i]["color"] +
+ "," +
+ vertices[i]["color"] +
+ "," +
+ vertices[i]["color"] +
+ ")";
+
+ ctx.beginPath();
+ ctx.arc(
+ vertices[i]["location"][0],
+ vertices[i]["location"][1],
+ 4,
+ 0,
+ 2 * Math.PI,
+ false
+ );
+ ctx.fill();
+ ctx.stroke();
+ }
</script>
</body>
</html>
View
@@ -13,7 +13,8 @@ var Delaunay;
(function() {
"use strict";
- var Simplex = function(indices, vertices, n) {
+ var M = typeof Matrix !== "undefined" ? Matrix : require("./matrix"),
+ Simplex = function(indices, vertices, n) {
var list = new Array(indices.length),
i;
@@ -154,44 +155,37 @@ var Delaunay;
return matrix;
},
- /* FIXME: This can be genericized by making a vector, n + 1 long. For each
- * index in that vector, make a square matrix from the rectangular one by
- * omitting the respective column, and taking the determinant of that
- * matrix. Be sure to negate every odd-indexed cell (1, 3, etc.). */
cross: function(matrix, n) {
if(matrix.length !== n * n + n)
throw new Error("Invalid matrix shape.");
- switch(n) {
- case 2:
- return [
- matrix[1] * matrix[5] - matrix[2] * matrix[4],
- matrix[2] * matrix[3] - matrix[0] * matrix[5],
- matrix[0] * matrix[4] - matrix[1] * matrix[3]
- ];
-
- case 3:
- return [
- matrix[1] * (matrix[6] * matrix[11] - matrix[7] * matrix[10]) +
- matrix[2] * (matrix[7] * matrix[ 9] - matrix[5] * matrix[11]) +
- matrix[3] * (matrix[5] * matrix[10] - matrix[6] * matrix[ 9]) ,
-
- matrix[0] * (matrix[7] * matrix[10] - matrix[6] * matrix[11]) +
- matrix[2] * (matrix[4] * matrix[11] - matrix[7] * matrix[ 8]) +
- matrix[3] * (matrix[6] * matrix[ 8] - matrix[4] * matrix[10]) ,
-
- matrix[0] * (matrix[5] * matrix[11] - matrix[7] * matrix[ 9]) +
- matrix[1] * (matrix[7] * matrix[ 8] - matrix[4] * matrix[11]) +
- matrix[3] * (matrix[4] * matrix[ 9] - matrix[5] * matrix[ 8]) ,
-
- matrix[0] * (matrix[6] * matrix[ 9] - matrix[5] * matrix[10]) +
- matrix[1] * (matrix[4] * matrix[10] - matrix[6] * matrix[ 8]) +
- matrix[2] * (matrix[5] * matrix[ 8] - matrix[4] * matrix[ 9])
- ];
-
- default:
- throw new Error("FIXME: Delaunay.cross has not been generalized to 4 or more dimensions!");
+ var m = n + 1,
+ sgn = 1,
+ vec = new Array(m),
+ sq = new Array(n * n),
+ i, j, k;
+
+ for(i = 0; i !== m; ++i) {
+ /* If it's the first time through the loop, initialize the square
+ * matrix to hold every column but the first. */
+ if(i === 0)
+ for(j = n; j--; )
+ for(k = n; k--; )
+ sq[j * n + k] = matrix[j * m + k + 1];
+
+ /* Every other time, just replace the one column that's no longer
+ * relevant. */
+ else {
+ k = i - 1;
+ for(j = n; j--; )
+ sq[j * n + k] = matrix[j * m + k];
+ }
+
+ vec[i] = sgn * M.determinant(sq);
+ sgn = -sgn;
}
+
+ return vec;
},
/* FIXME: This should probably test the points for collinearity and use a
* different algorithm in that case, in order to improve robustness. */

0 comments on commit ffa76e4

Please sign in to comment.