-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
69 lines (56 loc) · 1.36 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
package main
import (
rl "github.com/gen2brain/raylib-go/raylib"
)
const (
width, height = 840, 600
title = "Convex hull in 2D"
)
var points, hull PointList
func main() {
// Initialize window
rl.InitWindow(width, height, title)
defer rl.CloseWindow()
rl.SetTargetFPS(60)
points = make(PointList, 0)
for !rl.WindowShouldClose() {
// Update
if rl.IsMouseButtonPressed(rl.MouseLeftButton) {
mousePos := rl.GetMousePosition()
// Convert to normalized coordinates (-1 to 1)
x := (float64(mousePos.X)/float64(width)*2 - 1)
y := -(float64(mousePos.Y)/float64(height)*2 - 1)
points = append(points, makePoint(x, y))
hull, _ = points.Compute()
}
if rl.IsKeyPressed(rl.KeyC) {
points, hull = nil, nil
points = make(PointList, 0)
hull = make(PointList, 0)
}
// Draw
rl.BeginDrawing()
rl.ClearBackground(rl.White)
drawScene()
rl.EndDrawing()
}
}
func drawScene() {
// Draw cartesian coordinates
rl.DrawLineEx(
rl.Vector2{X: 0, Y: float32(height / 2)},
rl.Vector2{X: float32(width), Y: float32(height / 2)},
3,
rl.Gray,
)
rl.DrawLineEx(
rl.Vector2{X: float32(width / 2), Y: 0},
rl.Vector2{X: float32(width / 2), Y: float32(height)},
3,
rl.Gray,
)
// Your existing point drawing methods will need to be updated to use raylib drawing functions
points.DrawPoints()
points.DrawLowestPoint()
hull.DrawLines()
}