2019-07-19 14:15:47 +03:00
|
|
|
module main
|
|
|
|
|
|
|
|
import gx
|
|
|
|
import gg
|
2019-09-23 13:42:20 +03:00
|
|
|
import time
|
2019-12-09 18:53:17 +03:00
|
|
|
import math
|
2019-09-23 13:42:20 +03:00
|
|
|
|
2019-07-19 14:15:47 +03:00
|
|
|
const (
|
2020-05-22 18:36:09 +03:00
|
|
|
size = 700
|
2020-08-29 10:14:25 +03:00
|
|
|
scale = 50.0
|
2019-07-19 14:15:47 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
struct Context {
|
2020-08-29 10:14:25 +03:00
|
|
|
mut:
|
2020-06-04 21:26:18 +03:00
|
|
|
gg &gg.Context
|
2019-07-19 14:15:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2020-08-29 10:14:25 +03:00
|
|
|
mut context := &Context{
|
|
|
|
gg: 0
|
2019-07-19 14:15:47 +03:00
|
|
|
}
|
2021-02-17 08:44:01 +03:00
|
|
|
context.gg = gg.new_context(
|
2020-08-29 10:14:25 +03:00
|
|
|
width: size
|
|
|
|
height: size
|
|
|
|
font_size: 20
|
|
|
|
user_data: context
|
|
|
|
window_title: 'Graph builder'
|
|
|
|
create_window: true
|
|
|
|
frame_fn: frame
|
2020-08-30 19:11:59 +03:00
|
|
|
resizable: true
|
2020-08-29 10:14:25 +03:00
|
|
|
bg_color: gx.white
|
|
|
|
font_path: gg.system_font_path()
|
2021-02-17 08:44:01 +03:00
|
|
|
)
|
2020-08-29 10:14:25 +03:00
|
|
|
context.gg.run()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn frame(mut ctx Context) {
|
|
|
|
ctx.gg.begin()
|
|
|
|
ctx.draw()
|
|
|
|
ctx.gg.end()
|
2019-07-19 14:15:47 +03:00
|
|
|
}
|
|
|
|
|
2020-01-16 02:07:59 +03:00
|
|
|
[live]
|
2019-07-20 18:38:00 +03:00
|
|
|
fn (ctx &Context) draw() {
|
2021-02-17 08:44:01 +03:00
|
|
|
s := gg.window_size()
|
|
|
|
mut w := s.width
|
|
|
|
mut h := s.height
|
|
|
|
if gg.high_dpi() {
|
2020-08-30 19:58:09 +03:00
|
|
|
w /= 2
|
|
|
|
h /= 2
|
|
|
|
}
|
2021-02-17 08:44:01 +03:00
|
|
|
ctx.gg.draw_line(0, h / 2, w, h / 2, gx.gray) // x axis
|
|
|
|
ctx.gg.draw_line(w / 2, 0, w / 2, h, gx.gray) // y axis
|
2020-08-29 10:14:25 +03:00
|
|
|
atime := f64(time.ticks() / 10)
|
2020-08-30 19:11:59 +03:00
|
|
|
stime := math.sin(2.0 * math.pi * f64(time.ticks() % 6000) / 6000)
|
2019-09-28 20:42:29 +03:00
|
|
|
mut y := 0.0
|
2021-02-17 08:44:01 +03:00
|
|
|
blue := gx.Color{
|
|
|
|
r: 100
|
|
|
|
g: 100
|
|
|
|
b: 200
|
|
|
|
}
|
|
|
|
red := gx.Color{
|
|
|
|
r: 200
|
|
|
|
g: 100
|
|
|
|
b: 100
|
|
|
|
}
|
2019-12-09 18:53:17 +03:00
|
|
|
y = 1.0
|
2021-02-17 08:44:01 +03:00
|
|
|
max := f32(w) / (2 * scale)
|
2020-08-30 19:11:59 +03:00
|
|
|
min := -max
|
|
|
|
for x := min; x <= max; x += 0.01 {
|
2020-08-29 10:14:25 +03:00
|
|
|
// y = x*x + 2
|
2020-08-30 19:11:59 +03:00
|
|
|
// y = x * x + stime * stime
|
2020-08-29 10:14:25 +03:00
|
|
|
// y = stime
|
2020-08-30 19:11:59 +03:00
|
|
|
// y = stime * h
|
2021-04-20 17:16:35 +03:00
|
|
|
y = stime * 1.0 * math.sin(x + stime + atime / 32) * ((h / 256) + x)
|
2020-08-29 10:14:25 +03:00
|
|
|
// y = (stime * x) * x + stime
|
|
|
|
// y = (x + 3) * (x + 3) / stime + stime*2.5
|
|
|
|
// y = math.sqrt(30.0 - x * x) * stime
|
|
|
|
// y -= (stime-0.5) + stime
|
2021-12-23 14:31:25 +03:00
|
|
|
// ctx.gg.draw_rect_filled(f32((w/2) + x * scale), f32((h/2) - y * scale), 2, 2, blue)
|
|
|
|
ctx.gg.draw_rect_filled(f32((w / 2) + x * scale), f32((h / 2) - y * scale), 2,
|
|
|
|
(f32(y) * scale), blue)
|
|
|
|
ctx.gg.draw_rect_filled(f32((w / 2) + x * scale), f32((h / 2) + y * scale), 2,
|
|
|
|
(f32(y) * scale) + 32, red)
|
2019-07-19 14:15:47 +03:00
|
|
|
}
|
|
|
|
}
|