Fortune's Algorithm
- This code has some double precision issues.
- Only 150 lines.
- This code uses splay tree. Worst time complexty is O(n log n).
- Calculating the Voronoi Diagram with 1 million(1,000,000) random points in 2 seconds (single core, Intel(R) Core(TM) i9-9900K CPU @ 3.60GHz).
- Code
https://jacquesh.github.io/post/fortunes-algorithm/
sh build.sh
./A input.txt
n : # of points
(n)
(x1) (y1)
(x2) (y2)
...
(xn) (yn)
- point must be distinct.
- n != 0
Reading main.cpp
rendering part can be helpful to know about function format.
std::vector<pdd> input
- Inputs. Order can (and will) be changed, so user should pay attention about this.
- Additionally,
input
will sorted by (y, x) order.
std::vector<pdd> vertex
Vertex
is locations of Voronoi Diagram's intersection points. It can contain same points, and the degree of each point is 3.- This code doesn't compress same points. If you want, then you should implement it.
std::vector<pii> edge
for(pii c : edge)
, Voronoi Diagram contains line fromvertex[c.first]
tovertex[c.second]
.- Voronoi can contain rays or straight line. In this case, rays are depicted as {-1, (point index)} or {(point index), -1}, and straight line are depicted as {-1, -1}. A direction of each line can calculate using
area
array.
std::vector<pii> area
- Let (a, b) := i-th element of array
area
, and (u, v) := i-th element of arrayedge
. - Then, Point
input[a]
is located CCW of an u->v line (or ray, or straight line), and pointinput[b]
is located CW of an u->v line. - u->v line is a subset of perpendicular bisector of line segment from
input[a]
toinput[b]
. - Straight line {a, b}, {-1, -1} through midpoint of
input[a]
andinput[b]
.
- Let (a, b) := i-th element of array
- Mouse whill operates to change scale.
- Mouse drag operates to move screen.
Feel free to ask questions.
And sorry for my poor English.