forked from stathissideris/ditaa
-
Notifications
You must be signed in to change notification settings - Fork 5
/
sepedges.go
260 lines (224 loc) · 5.02 KB
/
sepedges.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package main
import (
"fmt"
"github.com/akavel/polyclip-go"
"github.com/akavel/ditaa/graphical"
)
type edgeType int
const (
edgeHorizontal edgeType = iota
edgeVertical
edgeSloped
)
type edge struct {
start, end *graphical.Point
owner *graphical.Shape
}
func (e edge) String() string {
return fmt.Sprintf("(%v, %v) -> (%v, %v)", e.start.X, e.start.Y, e.end.X, e.end.Y)
}
func separateCommonEdges(gg graphical.Grid, shapes []graphical.Shape) []graphical.Shape {
offset := gg.MinimumOfCellDimensions() / 5
edges := []edge{}
// get all edges
for i := range shapes {
s := &shapes[i]
n := len(s.Points)
if n == 1 {
continue
}
for j := 0; j < n-1; j++ {
edges = append(edges, edge{
start: &s.Points[j],
end: &s.Points[j+1],
owner: s,
})
}
if s.Closed {
edges = append(edges, edge{
start: &s.Points[n-1],
end: &s.Points[0],
owner: s,
})
}
}
// group edges into pairs of touching edges
pairs := [][2]edge{}
// all-against-all touching test for the edges
startIndex := 1 //skip some to avoid duplicate comparisons and self-to-self comparisons
for _, edge1 := range edges {
for _, edge2 := range edges[startIndex:] {
if edge1.TouchesWith(edge2) {
pairs = append(pairs, [2]edge{edge1, edge2})
if DEBUG {
fmt.Println(edge1, "touches with", edge2)
}
}
}
startIndex++
}
moved := []edge{}
// move equivalent edges inwards
for _, p := range pairs {
edges:
for _, e := range p[:] {
for _, m := range moved {
if m.Equals(e) {
continue edges
}
}
// e not_in moved
e.MoveInwardsBy(offset)
moved = append(moved, e)
}
}
return shapes
}
func (e1 edge) TouchesWith(e2 edge) bool {
switch {
case e1.Equals(e2):
return true
case e1.Horizontal() && e2.Vertical():
return false
case e1.Vertical() && e2.Horizontal():
return false
case e1.DistanceFromOrigin() != e2.DistanceFromOrigin():
return false
}
//covering this corner case (should produce false):
// ---------
// ---------
first, second := e1, e2
if first.Vertical() {
first.ChangeAxis()
second.ChangeAxis()
}
first.FixDirection()
second.FixDirection()
if first.start.X > second.start.X {
first, second = second, first
}
if *first.end == *second.start {
return false
}
// case 1:
// ----------
// -----------
// case 2:
// ------
// -----------------
switch {
case e2.PointWithin(e1.start) || e2.PointWithin(e1.end):
return true
case e1.PointWithin(e2.start) || e1.PointWithin(e2.end):
return true
}
return false
}
func (e1 edge) Equals(e2 edge) bool {
switch {
case *e1.start == *e2.start && *e1.end == *e2.end:
return true
case *e1.start == *e2.end && *e1.end == *e2.start:
return true
}
return false
}
func (e edge) Horizontal() bool { return e.start.Y == e.end.Y }
func (e edge) Vertical() bool { return e.start.X == e.end.X }
func (e edge) DistanceFromOrigin() float64 {
switch e.Type() {
case edgeSloped:
panic("Cannot calculate distance of sloped edge from origin")
case edgeHorizontal:
return e.start.Y
default: // edgeVertical
return e.start.X
}
}
func (e *edge) ChangeAxis() {
tmp := e.start
e.start = &graphical.Point{X: e.end.Y, Y: e.end.X}
e.end = &graphical.Point{X: tmp.Y, Y: tmp.X}
}
func (e *edge) FixDirection() {
switch {
case e.Horizontal():
if e.start.X > e.end.X {
e.FlipDirection()
}
case e.Vertical():
if e.start.Y > e.end.Y {
e.FlipDirection()
}
default:
panic("Cannot fix direction of sloped egde")
}
}
func (e edge) PointWithin(p *graphical.Point) bool {
switch {
case e.Horizontal():
return (p.X >= e.start.X && p.X <= e.end.X) ||
(p.X >= e.end.X && p.X <= e.start.X)
case e.Vertical():
return (p.Y >= e.start.Y && p.Y <= e.end.Y) ||
(p.Y >= e.end.Y && p.Y <= e.start.Y)
default:
panic("Cannot calculate is ShapePoint is within sloped edge")
}
}
func (e edge) Type() edgeType {
switch {
case e.Horizontal():
return edgeHorizontal
case e.Vertical():
return edgeVertical
default:
return edgeSloped
}
}
func (e *edge) FlipDirection() {
e.start, e.end = e.end, e.start
}
func (e *edge) MoveInwardsBy(offset float64) {
t := e.Type()
if t == edgeSloped {
panic(fmt.Sprint("Cannot move a sloped egde inwards: ", *e))
}
var xoff, yoff float64
middle := graphical.Point{
X: (e.start.X + e.end.X) / 2,
Y: (e.start.Y + e.end.Y) / 2,
}
path := e.owner.MakeIntoPath()
if path == nil {
return
}
switch t {
case edgeHorizontal:
up := polyclip.Point{X: middle.X, Y: middle.Y - 0.05}
down := polyclip.Point{X: middle.X, Y: middle.Y + 0.05}
switch {
case path.Contains(up):
yoff = -offset
case path.Contains(down):
yoff = offset
}
case edgeVertical:
left := polyclip.Point{X: middle.X - 0.05, Y: middle.Y}
right := polyclip.Point{X: middle.X + 0.05, Y: middle.Y}
switch {
case path.Contains(left):
xoff = -offset
case path.Contains(right):
xoff = offset
}
}
if DEBUG {
fmt.Printf("Moved edge %v by %v, %v\n", e, xoff, yoff)
}
e.start.X += xoff
e.start.Y += yoff
e.end.X += xoff
e.end.Y += yoff
}