-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathoverlapping_circles.go
191 lines (167 loc) · 3.55 KB
/
overlapping_circles.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
package main
import (
"fmt"
"math"
"os"
"sort"
)
const (
// D = math.Pi
S = 2
)
type Point struct {
X, Y float64
}
func (a Point) Lerp(b Point, t float64) Point {
x := a.X + (b.X-a.X)*t
y := a.Y + (b.Y-a.Y)*t
return Point{x, y}
}
type Segment struct {
P0, P1 Point
}
type Circle struct {
X, Y, R float64
}
func (c Circle) ContainsPoint(p Point) bool {
return math.Hypot(p.X-c.X, p.Y-c.Y) < c.R
}
func (c Circle) Discretize(n int) []Point {
points := make([]Point, n)
for i := range points {
t := float64(i) / float64(n-1)
a := 2 * math.Pi * t
x := math.Cos(a)*c.R + c.X
y := math.Sin(a)*c.R + c.Y
points[i] = Point{x, y}
}
return points
}
func (c Circle) IntersectLine(p0, p1 Point) (float64, float64, bool) {
dx := p1.X - p0.X
dy := p1.Y - p0.Y
A := dx*dx + dy*dy
B := 2 * (dx*(p0.X-c.X) + dy*(p0.Y-c.Y))
C := (p0.X-c.X)*(p0.X-c.X) + (p0.Y-c.Y)*(p0.Y-c.Y) - c.R*c.R
det := B*B - 4*A*C
if A <= 0 || det <= 0 {
return 0, 0, false
}
t0 := (-B + math.Sqrt(det)) / (2 * A)
t1 := (-B - math.Sqrt(det)) / (2 * A)
return t0, t1, true
}
func makeCircles(circleRadius, visibleRadius float64) []Circle {
var circles []Circle
a := int(math.Ceil(circleRadius + visibleRadius))
for y := -a; y <= a; y++ {
for x := -a; x <= a; x++ {
cx := float64(x)
cy := float64(y)
if math.Hypot(cx, cy) <= circleRadius+visibleRadius {
circles = append(circles, Circle{cx, cy, circleRadius})
}
}
}
return circles
}
func count(circles []Circle, p Point) int {
var result int
for _, c := range circles {
if c.ContainsPoint(p) {
result++
}
}
return result
}
type splitFunc func(Point) bool
func split(circles []Circle, p0, p1 Point, f splitFunc) []Segment {
var ts []float64
for _, c := range circles {
t0, t1, ok := c.IntersectLine(p0, p1)
if ok {
ts = append(ts, t0)
ts = append(ts, t1)
}
}
sort.Float64s(ts)
var segments []Segment
for i := 1; i < len(ts); i++ {
t0 := ts[i-1]
t1 := ts[i]
if t1 < 0 || t0 > 1 {
continue
}
t0 = math.Max(t0, 0)
t1 = math.Min(t1, 1)
t := (t0 + t1) / 2
p := p0.Lerp(p1, t)
if f(p) {
q0 := p0.Lerp(p1, t0)
q1 := p0.Lerp(p1, t1)
segments = append(segments, Segment{q0, q1})
}
}
return segments
}
func run(path string, d, s float64) error {
file, err := os.Create(path)
if err != nil {
return err
}
circles := makeCircles(d/2, s*math.Sqrt(2))
x0 := -s
x1 := s
y0 := -s
y1 := s
f := func(p Point) bool {
return count(circles, p)%2 == 1
}
g := func(p Point) bool {
return math.Hypot(p.X, p.Y) <= s
}
outer := []Circle{Circle{0, 0, s}}
const n = 200
for i := 0; i <= n; i++ {
t := float64(i) / float64(n)
y := y0 + (y1-y0)*t
p0 := Point{x0, y}
p1 := Point{x1, y}
segments := split(circles, p0, p1, f)
for _, s := range segments {
clipped := split(outer, s.P0, s.P1, g)
for _, cs := range clipped {
fmt.Fprintf(file, "%g,%g %g,%g\n", cs.P0.X, cs.P0.Y, cs.P1.X, cs.P1.Y)
}
}
}
for _, c := range circles {
points := c.Discretize(360)
for i := 1; i < len(points); i++ {
p0 := points[i-1]
p1 := points[i]
clipped := split(outer, p0, p1, g)
for _, cs := range clipped {
fmt.Fprintf(file, "%g,%g %g,%g\n", cs.P0.X, cs.P0.Y, cs.P1.X, cs.P1.Y)
}
}
}
points := outer[0].Discretize(360)
for _, p := range points {
fmt.Fprintf(file, "%g,%g ", p.X, p.Y)
}
fmt.Fprintf(file, "\n")
return nil
}
func main() {
d0 := 1.0
d1 := math.Pi
n := 48
for i := 0; i < n; i++ {
t := float64(i) / float64(n-1)
d := d0 + (d1-d0)*t
path := fmt.Sprintf("overlapping_circles/%.8f.axi", d)
fmt.Println(path)
run(path, d, S)
}
}