-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (62 loc) · 1.2 KB
/
main.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
package main
import (
"fmt"
"math"
"math/rand"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
"golang.org/x/image/colornames"
)
type dot struct {
X int
Y int
}
const width = 1000
const height = 1000
var in, out int = 0, 0
func setup(imd *imdraw.IMDraw) {
imd.Color = colornames.Gray
imd.Push(pixel.V(0, 0))
imd.Circle(width, 5)
}
func render(imd *imdraw.IMDraw) {
newDot := dot{
X: rand.Intn(width + 1),
Y: rand.Intn(height + 1),
}
if math.Sqrt(math.Pow(float64(newDot.X), 2)+math.Pow(float64(newDot.Y), 2)) < width {
imd.Color = colornames.Green
in++
} else {
imd.Color = colornames.Red
out++
}
imd.Push(pixel.V(float64(newDot.X), float64(newDot.Y)))
imd.Circle(1, 5)
total := in + out
pi := (float64(in) / float64(total)) * 4
fmt.Printf("pi = %v\n", pi)
}
func run() {
cfg := pixelgl.WindowConfig{
Title: "PiCulate",
Bounds: pixel.R(0, 0, width, height),
VSync: true,
}
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
imd := imdraw.New(nil)
win.Clear(colornames.Skyblue)
setup(imd)
for !win.Closed() {
render(imd)
imd.Draw(win)
win.Update()
}
}
func main() {
pixelgl.Run(run)
}