-
Notifications
You must be signed in to change notification settings - Fork 0
/
gopi.go
163 lines (133 loc) · 4.04 KB
/
gopi.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
package main
import (
"fmt"
"math"
"math/rand"
"unicode"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/text"
"golang.org/x/image/font/basicfont"
)
const MAX_GENERATIONS = 1337
const WINDOW_WIDTH = 1024
const WINDOW_HEIGHT = 768
const SQUARE_SIZE = 400
const CIRCLE_RADIUS = SQUARE_SIZE / 2
const MARKER_SIZE float64 = 2
type Vector struct {
X float64
Y float64
}
func main() {
pixelgl.Run(run)
}
func run() {
cfg := pixelgl.WindowConfig {
Bounds: pixel.R(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT),
VSync: true,
}
window, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
atlas := text.NewAtlas(
basicfont.Face7x13,
text.ASCII,
text.RangeTable(unicode.Latin),
)
drawRenderer := imdraw.New(nil)
textRenderer := text.New(pixel.V(20, 40), atlas)
generations := 0
vectors := []Vector {}
midPoint := Vector {
X: WINDOW_WIDTH / 2,
Y: WINDOW_HEIGHT / 2,
}
renderBoundingBox(midPoint, drawRenderer)
renderInnerCircle(midPoint, drawRenderer)
for !window.Closed() {
qualifiedPoints := 0
window.Clear(pixel.RGB(0, 0, 0))
textRenderer.Clear()
if generations < MAX_GENERATIONS {
generations += 1
vectors = append(vectors, makeVector())
}
for _, vector := range vectors {
if (inCircle(vector)) {
qualifiedPoints += 1
drawMarker(vector, true, midPoint, drawRenderer)
} else {
drawMarker(vector, false, midPoint, drawRenderer)
}
}
pi := (float64(qualifiedPoints) / float64(generations)) * 4
renderOutput(pi, generations, textRenderer)
textRenderer.Draw(window, pixel.IM.Scaled(textRenderer.Orig, 2))
drawRenderer.Draw(window)
window.Update()
}
}
func inCircle(vector Vector) bool {
circleMidPoint := float64(SQUARE_SIZE / 2)
distance := math.Sqrt(math.Pow(vector.X - circleMidPoint, 2) + math.Pow(vector.Y - circleMidPoint, 2))
return distance <= CIRCLE_RADIUS
}
func renderBoundingBox(midPoint Vector, drawRenderer *imdraw.IMDraw) {
boundingBoxMidPoint := float64(SQUARE_SIZE / 2)
drawRenderer.Color = pixel.RGB(0, 0, 0.5)
drawRenderer.Push(pixel.V(midPoint.X - boundingBoxMidPoint, midPoint.Y - boundingBoxMidPoint))
drawRenderer.Push(pixel.V(midPoint.X + boundingBoxMidPoint, midPoint.Y - boundingBoxMidPoint))
drawRenderer.Push(pixel.V(midPoint.X + boundingBoxMidPoint, midPoint.Y + boundingBoxMidPoint))
drawRenderer.Push(pixel.V(midPoint.X - boundingBoxMidPoint, midPoint.Y + boundingBoxMidPoint))
drawRenderer.Polygon(2)
}
func renderInnerCircle(midPoint Vector, imd *imdraw.IMDraw) {
imd.Color = pixel.RGB(1, 1, 1)
imd.Push(pixel.V(midPoint.X, midPoint.Y))
imd.Circle(CIRCLE_RADIUS, 1)
}
func renderOutput(pi float64, generations int, textRenderer *text.Text) {
fmt.Fprintln(textRenderer, "Pi:", pi)
fmt.Fprintln(textRenderer, "Gen:", generations, "/", MAX_GENERATIONS)
}
func drawMarker(vector Vector, inCircle bool, midPoint Vector, imd *imdraw.IMDraw) {
boundingBoxMidPoint := float64(SQUARE_SIZE / 2)
if (inCircle) {
imd.Color = pixel.RGB(0, 1, 0)
} else {
imd.Color = pixel.RGB(1, 0, 0)
}
vec1TopLeft := pixel.V(
midPoint.X - boundingBoxMidPoint + vector.X - MARKER_SIZE,
midPoint.Y - boundingBoxMidPoint + vector.Y - MARKER_SIZE,
)
vec1BottomRight := pixel.V(
midPoint.X - boundingBoxMidPoint + vector.X + MARKER_SIZE,
midPoint.Y - boundingBoxMidPoint + vector.Y + MARKER_SIZE,
)
vec2TopRight := pixel.V(
midPoint.X - boundingBoxMidPoint + vector.X - MARKER_SIZE,
midPoint.Y - boundingBoxMidPoint + vector.Y + MARKER_SIZE,
)
vec2BottomLeft := pixel.V(
midPoint.X - boundingBoxMidPoint + vector.X + MARKER_SIZE,
midPoint.Y - boundingBoxMidPoint + vector.Y - MARKER_SIZE,
)
imd.Push(vec1TopLeft)
imd.Push(vec1BottomRight)
imd.Line(1)
imd.Push(vec2TopRight)
imd.Push(vec2BottomLeft)
imd.Line(1)
}
func makeVector() Vector {
x := SQUARE_SIZE * rand.Float64()
y := SQUARE_SIZE * rand.Float64()
return Vector {
X: x,
Y: y,
}
}