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