forked from pzsz/voronoi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeometry.go
142 lines (117 loc) · 3.23 KB
/
geometry.go
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// MIT License: See https://github.com/pzsz/voronoi/LICENSE.md
// Author: Przemyslaw Szczepaniak (przeszczep@gmail.com)
// Port of Raymond Hill's (rhill@raymondhill.net) javascript implementation
// of Steven Forune's algorithm to compute Voronoi diagrams
package voronoi
import (
"math"
)
// Vertex on 2D plane
type Vertex struct {
X float64
Y float64
}
type SiteVertex struct {
Vertex
Data interface{}
}
// Vertex representing lack of vertex (or bad vertex)
var NO_VERTEX = Vertex{math.Inf(1), math.Inf(1)}
// For sort interface
type Vertices []SiteVertex
func (s Vertices) Len() int { return len(s) }
func (s Vertices) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// Used for sorting vertices along the Y axis
type VerticesByY struct{ Vertices }
func (s VerticesByY) Less(i, j int) bool { return s.Vertices[i].Y < s.Vertices[j].Y }
type EdgeVertex struct {
Vertex
Edges []*Edge
}
// Edge structure
type Edge struct {
// Cell on the left
LeftCell *Cell
// Cell on the right
RightCell *Cell
// Start Vertex
Va EdgeVertex
// End Vertex
Vb EdgeVertex
}
func (e *Edge) GetOtherCell(cell *Cell) *Cell {
if cell == e.LeftCell {
return e.RightCell
} else if cell == e.RightCell {
return e.LeftCell
}
return nil
}
func (e *Edge) GetOtherEdgeVertex(v Vertex) EdgeVertex {
if v == e.Va.Vertex {
return e.Vb
} else if v == e.Vb.Vertex {
return e.Va
}
return EdgeVertex{NO_VERTEX, nil}
}
func newEdge(LeftCell, RightCell *Cell) *Edge {
return &Edge{
LeftCell: LeftCell,
RightCell: RightCell,
Va: EdgeVertex{NO_VERTEX, nil},
Vb: EdgeVertex{NO_VERTEX, nil},
}
}
// Halfedge (directed edge)
type Halfedge struct {
Cell *Cell
Edge *Edge
Angle float64
}
// Sort interface for halfedges
type Halfedges []*Halfedge
func (s Halfedges) Len() int { return len(s) }
func (s Halfedges) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
// For sorting by angle
type halfedgesByAngle struct{ Halfedges }
func (s halfedgesByAngle) Less(i, j int) bool { return s.Halfedges[i].Angle > s.Halfedges[j].Angle }
func newHalfedge(edge *Edge, LeftCell, RightCell *Cell) *Halfedge {
ret := &Halfedge{
Cell: LeftCell,
Edge: edge,
}
// 'angle' is a value to be used for properly sorting the
// halfsegments counterclockwise. By convention, we will
// use the angle of the line defined by the 'site to the left'
// to the 'site to the right'.
// However, border edges have no 'site to the right': thus we
// use the angle of line perpendicular to the halfsegment (the
// edge should have both end points defined in such case.)
if RightCell != nil {
ret.Angle = math.Atan2(RightCell.Site.Y-LeftCell.Site.Y, RightCell.Site.X-LeftCell.Site.X)
} else {
va := edge.Va
vb := edge.Vb
// rhill 2011-05-31: used to call GetStartpoint()/GetEndpoint(),
// but for performance purpose, these are expanded in place here.
if edge.LeftCell == LeftCell {
ret.Angle = math.Atan2(vb.X-va.X, va.Y-vb.Y)
} else {
ret.Angle = math.Atan2(va.X-vb.X, vb.Y-va.Y)
}
}
return ret
}
func (h *Halfedge) GetStartpoint() Vertex {
if h.Edge.LeftCell == h.Cell {
return h.Edge.Va.Vertex
}
return h.Edge.Vb.Vertex
}
func (h *Halfedge) GetEndpoint() Vertex {
if h.Edge.LeftCell == h.Cell {
return h.Edge.Vb.Vertex
}
return h.Edge.Va.Vertex
}