凸多边形最优三角剖分java源代码
时间: 2023-12-24 07:23:30 浏览: 91
以下是凸多边形最优三角剖分的Java源代码:
```java
public class Triangulation {
public static double minWeightTriangulation(Point[] points) {
int n = points.length;
double[][] dp = new double[n][n];
for (int len = 2; len < n; len++) {
for (int i = 0; i + len < n; i++) {
int j = i + len;
dp[i][j] = Double.MAX_VALUE;
for (int k = i + 1; k < j; k++) {
dp[i][j] = Math.min(dp[i][j], dp[i][k] + dp[k][j] + dist(points[i], points[j], points[k]));
}
}
}
return dp[0][n - 1];
}
private static double dist(Point a, Point b, Point c) {
return a.distance(b) + b.distance(c) + c.distance(a);
}
private static class Point {
double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double distance(Point p) {
double dx = x - p.x;
double dy = y - p.y;
return Math.sqrt(dx * dx + dy * dy);
}
}
}
```
阅读全文