Skip to content

Commit ae0d843

Browse files
Add VoronoiDiagram class for generating Voronoi edges
This class generates Voronoi diagrams by computing the edges between sites using their perpendicular bisectors. It includes a method to compute the Voronoi edges given a list of points.
1 parent 6cba1b3 commit ae0d843

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.thealgorithms.geometry;
2+
3+
import java.awt.geom.Line2D;
4+
import java.util.*;
5+
6+
/**
7+
* A simplified Voronoi Diagram generator using perpendicular bisectors.
8+
*
9+
* This implementation computes the Voronoi edges between each pair of sites
10+
* by finding their perpendicular bisectors (not a full Fortune’s algorithm).
11+
*
12+
* Time Complexity: O(n^2)
13+
*/
14+
public final class VoronoiDiagram {
15+
16+
private VoronoiDiagram() {}
17+
18+
public static List<Line2D.Double> computeVoronoiEdges(List<Point> points) {
19+
List<Line2D.Double> edges = new ArrayList<>();
20+
21+
for (int i = 0; i < points.size(); i++) {
22+
for (int j = i + 1; j < points.size(); j++) {
23+
Point p1 = points.get(i);
24+
Point p2 = points.get(j);
25+
Point mid = new Point((p1.x + p2.x) / 2, (p1.y + p2.y) / 2);
26+
27+
double dx = p2.x - p1.x;
28+
double dy = p2.y - p1.y;
29+
30+
// Perpendicular slope
31+
double length = Math.sqrt(dx * dx + dy * dy);
32+
double ux = -dy / length;
33+
double uy = dx / length;
34+
35+
// Extend line in both directions
36+
double scale = 1000;
37+
Point start = new Point(mid.x + ux * scale, mid.y + uy * scale);
38+
Point end = new Point(mid.x - ux * scale, mid.y - uy * scale);
39+
edges.add(new Line2D.Double(start.x, start.y, end.x, end.y));
40+
}
41+
}
42+
return edges;
43+
}
44+
}

0 commit comments

Comments
 (0)