Skip to content

Commit bea8404

Browse files
authored
MinimumCostPolygonTriangulation (williamfiset#164)
* MinimumCostPolygonTriangulation * style fix * updating tests, renaming files, comments
1 parent 9e6bfae commit bea8404

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* This file shows you how to find the minimum cost convex polygon triangulation of a set of points.
3+
* Points must be in either clockwise or counterclockwise order.
4+
*
5+
* <p>Time Complexity: O(n^3)
6+
*
7+
* @author Bryan Bowles
8+
*/
9+
package com.williamfiset.algorithms.geometry;
10+
11+
import java.awt.geom.Point2D;
12+
13+
// Problem explanation: https://www.geeksforgeeks.org/minimum-cost-polygon-triangulation/
14+
public class MinimumCostConvexPolygonTriangulation {
15+
16+
// Returns the perimeter (cost) of the triangle
17+
private static double cost(Point2D i, Point2D j, Point2D k) {
18+
return i.distance(j) + i.distance(k) + j.distance(k);
19+
}
20+
21+
public static double minimumCostTriangulation(Point2D[] points) {
22+
int len = points.length;
23+
if (len < 3) return 0;
24+
25+
double[][] dp = new double[len][len];
26+
for (int i = 2; i < len; i++) {
27+
for (int j = 0; j + i < len; j++) {
28+
dp[j][j + i] = Integer.MAX_VALUE;
29+
for (int k = j + 1; k < j + i; k++) {
30+
dp[j][j + i] =
31+
Math.min(
32+
dp[j][j + i],
33+
dp[j][k] + dp[k][j + i] + cost(points[j], points[j + i], points[k]));
34+
}
35+
}
36+
}
37+
return dp[0][len - 1];
38+
}
39+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.williamfiset.algorithms.geometry;
2+
3+
import static com.google.common.truth.Truth.assertThat;
4+
5+
import java.awt.geom.*;
6+
import org.junit.*;
7+
8+
public class MinimumCostConvexPolygonTriangulationTest {
9+
10+
private static final double TOLERANCE = 1e-3;
11+
12+
@Test
13+
public void MinimumCostConvexPolygonTriangulationBasicTest() {
14+
Point2D[] pts = new Point2D[5];
15+
16+
pts[0] = new Point2D.Double(0, 0);
17+
pts[1] = new Point2D.Double(1, 0);
18+
pts[2] = new Point2D.Double(2, 1);
19+
pts[3] = new Point2D.Double(1, 2);
20+
pts[4] = new Point2D.Double(0, 2);
21+
22+
double cost = MinimumCostConvexPolygonTriangulation.minimumCostTriangulation(pts);
23+
assertThat(cost).isWithin(TOLERANCE).of(15.3);
24+
}
25+
26+
@Test
27+
public void MinimumCostConvexPolygonTriangulationInvalidTest() {
28+
Point2D[] pts = new Point2D[2];
29+
30+
pts[0] = new Point2D.Double(0, 0);
31+
pts[1] = new Point2D.Double(1, 0);
32+
33+
double cost = MinimumCostConvexPolygonTriangulation.minimumCostTriangulation(pts);
34+
assertThat(cost).isEqualTo(0);
35+
}
36+
37+
@Test
38+
public void MinimumCostConvexPolygonTriangulationConvex() {
39+
Point2D[] pts = new Point2D[6];
40+
41+
pts[0] = new Point2D.Double(0, 0);
42+
pts[1] = new Point2D.Double(4, 0);
43+
pts[2] = new Point2D.Double(4, 2);
44+
pts[3] = new Point2D.Double(1, 3);
45+
pts[4] = new Point2D.Double(0, 2);
46+
pts[5] = new Point2D.Double(0, 1);
47+
48+
double cost = MinimumCostConvexPolygonTriangulation.minimumCostTriangulation(pts);
49+
assertThat(cost).isWithin(TOLERANCE).of(31.386);
50+
}
51+
}

0 commit comments

Comments
 (0)