/
arcs_and_slices.v
166 lines (149 loc) · 4.43 KB
/
arcs_and_slices.v
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
module main
import gg
import gx
import math
const win_width = 700
const win_height = 800
const bg_color = gx.white
// A transparent color is used to aid in verifying that
// rendering is precise on each of the the arc types (e.g. no overlapping or double rendered slices)
const colour = gx.rgba(100, 100, 0, 100)
enum Selection {
segs = 0
len
}
struct App {
mut:
gg &gg.Context = unsafe { nil }
mouse struct {
mut:
x f32
y f32
}
sel Selection
segs int = 8
}
fn main() {
mut app := &App{}
app.gg = gg.new_context(
width: win_width
height: win_height
create_window: true
window_title: 'Arcs and Slices'
user_data: app
bg_color: bg_color
frame_fn: on_frame
event_fn: on_event
)
app.gg.run()
}
fn on_frame(mut app App) {
app.gg.begin()
start := math.tau * app.mouse.y / (win_width * app.gg.scale)
end := math.tau * app.mouse.x / (win_width * app.gg.scale)
segs := if app.sel == .segs { '[${app.segs}]' } else { '${app.segs}' }
app.gg.draw_text_def(10, 10, 'Segments: ${segs}')
app.gg.draw_text_def(250, 10, 'Drawing Angles (radians)')
app.gg.draw_text_def(200, 26, 'Start: ${start}°')
app.gg.draw_text_def(350, 26, 'End: ${end}°')
mut x, mut y := 0, -80
y += 150
x = 20
app.gg.draw_text_def(10, y + 40, 'slice')
x += 150
app.gg.draw_slice_empty(x, y + 60, 50, start, end, app.segs, colour)
app.gg.draw_text_def(x - 50, y + 120, 'r=50 empty')
x += 150
app.gg.draw_slice_empty(x, y + 60, 0, start, end, app.segs, colour)
app.gg.draw_text_def(x - 50, y + 120, 'r=0 empty')
x += 150
app.gg.draw_slice_filled(x, y + 60, 50, start, end, app.segs, colour)
app.gg.draw_text_def(x - 50, y + 120, 'r=50 filled')
x += 150
app.gg.draw_slice_filled(x, y + 60, 0, start, end, app.segs, colour)
app.gg.draw_text_def(x - 50, y + 120, 'r=0 filled')
y += 150
x = 20
app.gg.draw_text_def(10, y + 40, 'arc_empty')
x += 150
app.gg.draw_arc_empty(x, y + 60, 30, 20, start, end, app.segs, colour)
app.gg.draw_text_def(x - 50, y + 120, 'r=[30,50]')
x += 150
app.gg.draw_arc_empty(x, y + 60, -10, 60, start, end, app.segs, colour)
app.gg.draw_text_def(x - 50, y + 120, 'r=[-10,50]')
x += 150
app.gg.draw_arc_empty(x, y + 60, 50, 0, start, end, app.segs, colour)
app.gg.draw_text_def(x - 50, y + 120, 'r=[50,50]')
x += 150
app.gg.draw_arc_empty(x, y + 60, 0, 0, start, end, app.segs, colour)
app.gg.draw_text_def(x - 50, y + 120, 'r=[0,0]')
y += 150
x = 20
app.gg.draw_text_def(10, y + 40, 'arc_filled')
x += 150
app.gg.draw_arc_filled(x, y + 60, 30, 20, start, end, app.segs, colour)
app.gg.draw_text_def(x - 50, y + 120, 'r=[30,50]')
x += 150
app.gg.draw_arc_filled(x, y + 60, -10, 60, start, end, app.segs, colour)
app.gg.draw_text_def(x - 50, y + 120, 'r=[-10,50]')
x += 150
app.gg.draw_arc_filled(x, y + 60, 50, 0, start, end, app.segs, colour)
app.gg.draw_text_def(x - 50, y + 120, 'r=[50,50]')
x += 150
app.gg.draw_arc_filled(x, y + 60, 0, 0, start, end, app.segs, colour)
app.gg.draw_text_def(x - 50, y + 120, 'r=[0,0]')
y += 150
x = 20
app.gg.draw_text_def(10, y + 40, 'arc_line')
x += 150
app.gg.draw_arc_line(x, y + 60, 50, start, end, app.segs, colour)
app.gg.draw_text_def(x - 50, y + 120, 'r=50')
x += 150
app.gg.draw_arc_line(x, y + 60, 0, start, end, app.segs, colour)
app.gg.draw_text_def(x - 50, y + 120, 'r=0')
y += 150
app.gg.draw_text_def(10, y + 20, 'Use arrow keys to increase/decrease number of segments.')
app.gg.draw_text_def(10, y + 36, 'Use the mouse to adjust the start/end angles, in radians. Mouse position (0,0) is at the top-left of the window.')
app.gg.draw_text_def(10, y + 52, 'Note: because y=0 is at the top of the screen and not the bottom, angle=0 is at the bottom of an arc, not the top!')
app.gg.draw_text_def(10, y + 68, 'Compared to a graph, where y=0 is at the bottom, arcs therefore appear y-flipped.')
app.gg.end()
}
fn on_event(e &gg.Event, mut app App) {
match e.typ {
.key_down {
match e.key_code {
.escape {
app.gg.quit()
}
.up {
app.sel = unsafe { Selection(math.max(0, int(app.sel) - 1)) }
}
.down {
app.sel = unsafe { Selection(math.min(int(Selection.len) - 1, int(app.sel) + 1)) }
}
.left {
match app.sel {
.segs {
app.segs = math.max(1, app.segs / 2)
}
else {}
}
}
.right {
match app.sel {
.segs {
app.segs = math.min(64, app.segs * 2)
}
else {}
}
}
else {}
}
}
.mouse_move {
app.mouse.x = e.mouse_x
app.mouse.y = e.mouse_y
}
else {}
}
}