帮我用javascript写一个Delaunay算法
时间: 2023-05-22 19:06:04 浏览: 137
好的,以下是使用 JavaScript 实现 Delaunay 算法的代码:
```javascript
function Triangle(v1, v2, v3) {
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
}
function Edge(v1, v2) {
this.v1 = v1;
this.v2 = v2;
}
function delaunay(points) {
var triangles = [];
// Create a super triangle that covers all the points
var superTriangle = new Triangle(
{ x: -Number.MAX_VALUE, y: -Number.MAX_VALUE },
{ x: 0, y: Number.MAX_VALUE },
{ x: Number.MAX_VALUE, y: -Number.MAX_VALUE }
);
triangles.push(superTriangle);
// Add each point to the Delaunay triangulation
for (var i = 0; i < points.length; i++) {
var edges = [];
// Find all the edges of the triangles that intersect with the new point
for (var j = 0; j < triangles.length; j++) {
var t = triangles[j];
if (circumCircleContains(t, points[i])) {
edges.push(new Edge(t.v1, t.v2));
edges.push(new Edge(t.v2, t.v3));
edges.push(new Edge(t.v3, t.v1));
triangles.splice(j, 1);
j--;
}
}
// Remove duplicate edges
for (var j = 0; j < edges.length; j++) {
for (var k = j + 1; k < edges.length; k++) {
if (
(edges[j].v1 == edges[k].v1 && edges[j].v2 == edges[k].v2) ||
(edges[j].v1 == edges[k].v2 && edges[j].v2 == edges[k].v1)
) {
edges.splice(k, 1);
k--;
edges.splice(j, 1);
j--;
break;
}
}
}
// Create new triangles using the new point
for (var j = 0; j < edges.length; j++) {
triangles.push(new Triangle(edges[j].v1, edges[j].v2, points[i]));
}
}
// Remove any triangles that share vertices with the super triangle
for (var i = 0; i < triangles.length; i++) {
var t = triangles[i];
if (t.v1 == superTriangle.v1 || t.v1 == superTriangle.v2 || t.v1 == superTriangle.v3 ||
t.v2 == superTriangle.v1 || t.v2 == superTriangle.v2 || t.v2 == superTriangle.v3 ||
t.v3 == superTriangle.v1 || t.v3 == superTriangle.v2 || t.v3 == superTriangle.v3) {
triangles.splice(i, 1);
i--;
}
}
return triangles;
}
function circumCircleContains(triangle, point) {
// Calculate the circumcircle of the triangle
var deltaA = triangle.v1.x * triangle.v1.x + triangle.v1.y * triangle.v1.y;
var deltaB = triangle.v2.x * triangle.v2.x + triangle.v2.y * triangle.v2.y;
var deltaC = triangle.v3.x * triangle.v3.x + triangle.v3.y * triangle.v3.y;
var axb = triangle.v1.x * (triangle.v2.y - triangle.v3.y) +
triangle.v2.x * (triangle.v3.y - triangle.v1.y) +
triangle.v3.x * (triangle.v1.y - triangle.v2.y);
var center = {
x: ((deltaA * (triangle.v2.y - triangle.v3.y) +
deltaB * (triangle.v3.y - triangle.v1.y) +
deltaC * (triangle.v1.y - triangle.v2.y)) /
(2 * axb)),
y: ((deltaA * (triangle.v3.x - triangle.v2.x) +
deltaB * (triangle.v1.x - triangle.v3.x) +
deltaC * (triangle.v2.x - triangle.v1.x)) /
(2 * axb))
};
var radius = Math.sqrt(Math.pow(center.x - triangle.v1.x, 2) +
Math.pow(center.y - triangle.v1.y, 2));
// Check if the point is inside the circumcircle
var deltaPoint = point.x * point.x + point.y * point.y;
return deltaPoint <= Math.pow(radius, 2);
}
```
这段代码实现了基本的 Delaunay 三角剖分算法,可以将给定的一系列点(Point)进行三角剖分并得到对应的三角形(Triangle)列表。
阅读全文
相关推荐



















