v / examples / hot_reload / graph.v
81 lines · 74 sloc · 1.65 KB · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw
1module main
2
3import gg
4import time
5import math
6
7const size = 700
8const scale = 50.0
9
10struct Context {
11mut:
12 gg &gg.Context = unsafe { nil }
13}
14
15fn main() {
16 mut context := &Context{}
17 context.gg = gg.new_context(
18 width: size
19 height: size
20 font_size: 20
21 user_data: context
22 window_title: 'Graph builder'
23 create_window: true
24 frame_fn: frame
25 resizable: true
26 bg_color: gg.white
27 )
28 context.gg.run()
29}
30
31fn frame(mut ctx Context) {
32 ctx.gg.begin()
33 ctx.draw()
34 ctx.gg.end()
35}
36
37@[live]
38fn (ctx &Context) draw() {
39 s := gg.window_size()
40 mut w := s.width
41 mut h := s.height
42 if gg.high_dpi() {
43 w /= 2
44 h /= 2
45 }
46 ctx.gg.draw_line(0, h / 2, w, h / 2, gg.gray) // x axis
47 ctx.gg.draw_line(w / 2, 0, w / 2, h, gg.gray) // y axis
48 atime := f64(time.ticks() / 10)
49 stime := math.sin(2.0 * math.pi * f64(time.ticks() % 6000) / 6000)
50 mut y := 0.0
51 blue := gg.Color{
52 r: 100
53 g: 100
54 b: 200
55 }
56 red := gg.Color{
57 r: 200
58 g: 100
59 b: 100
60 }
61 y = 1.0
62 max := f32(w) / (2 * scale)
63 min := -max
64 for x := min; x <= max; x += 0.01 {
65 // y = x*x + 2
66 // y = x * x + stime * stime
67 // y = stime
68 // y = stime * h
69 y = stime * 1.0 * math.sin(x + stime + atime / 32) * ((h / 256) + x)
70 // y = (stime * x) * x + stime
71 // y = (x + 3) * (x + 3) / stime + stime*2.5
72 // y = math.sqrt(30.0 - x * x) * stime
73 // y -= (stime-0.5) + stime
74 // ctx.gg.draw_rect_filled(f32((w/2) + x * scale), f32((h/2) - y * scale), 2, 2, blue)
75 ctx.gg.draw_rect_filled(f32((w / 2) + x * scale), f32((h / 2) - y * scale), 2,
76 (f32(y) * scale), blue)
77 ctx.gg.draw_rect_filled(f32((w / 2) + x * scale), f32((h / 2) + y * scale), 2,
78
79 (f32(y) * scale) + 32, red)
80 }
81}
82