2019-07-19 14:15:47 +03:00
|
|
|
module main
|
|
|
|
|
|
|
|
import gx
|
|
|
|
import gg
|
2019-09-23 13:42:20 +03:00
|
|
|
import time
|
|
|
|
import glfw
|
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 (
|
2019-07-23 01:19:20 +03:00
|
|
|
Size = 700
|
2019-12-09 18:53:17 +03:00
|
|
|
Scale = 50.0
|
|
|
|
pi = math.pi
|
2019-07-19 14:15:47 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
struct Context {
|
2019-09-04 15:10:18 +03:00
|
|
|
gg &gg.GG
|
2019-07-19 14:15:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2019-10-12 06:03:15 +03:00
|
|
|
glfw.init_glfw()
|
2020-05-01 14:44:22 +03:00
|
|
|
gconfig := gg.Cfg {
|
2020-01-16 02:07:59 +03:00
|
|
|
width: Size
|
|
|
|
height: Size
|
|
|
|
use_ortho: true
|
|
|
|
create_window: true
|
|
|
|
window_title: 'Graph builder'
|
2019-07-20 18:38:00 +03:00
|
|
|
always_on_top: true
|
2020-01-16 02:07:59 +03:00
|
|
|
}
|
2020-05-01 14:44:22 +03:00
|
|
|
ctx := &Context{ gg: gg.new_context(gconfig) }
|
|
|
|
ctx.gg.window.set_user_ptr( ctx )
|
2020-01-16 02:07:59 +03:00
|
|
|
go update() // update the scene in the background in case the window isn't focused
|
2020-01-14 20:58:46 +03:00
|
|
|
for {
|
|
|
|
if ctx.gg.window.should_close() {
|
|
|
|
break
|
|
|
|
}
|
2020-01-16 02:07:59 +03:00
|
|
|
gg.clear(gx.White)
|
2019-07-19 14:15:47 +03:00
|
|
|
ctx.draw()
|
2020-01-16 02:07:59 +03:00
|
|
|
ctx.gg.render()
|
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() {
|
2019-09-28 20:42:29 +03:00
|
|
|
center := f64(Size / 2)
|
2020-01-16 02:07:59 +03:00
|
|
|
ctx.gg.draw_line(0, center, Size, center, gx.gray) // x axis
|
|
|
|
ctx.gg.draw_line(center, 0, center, Size, gx.gray) // y axis
|
2019-12-09 18:53:17 +03:00
|
|
|
atime := f64( time.ticks() / 10 )
|
|
|
|
stime := math.sin( 2.0 * pi * f64( time.ticks() % 6000 ) / 6000 )
|
2019-09-28 20:42:29 +03:00
|
|
|
mut y := 0.0
|
2019-12-09 18:53:17 +03:00
|
|
|
y = 1.0
|
|
|
|
for x := -10.0; x <= 10.0; x += 0.02 {
|
|
|
|
//y = x*x + 2
|
|
|
|
y = x*x + stime*stime
|
|
|
|
//y = stime
|
|
|
|
//y = stime * x
|
|
|
|
y = stime*1.0*math.sin(x + stime+atime/50) * x
|
|
|
|
//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
|
|
|
|
ctx.gg.draw_rect(center + x * Scale, center - y * Scale, 1, 1, gx.Blue)
|
2020-01-16 02:07:59 +03:00
|
|
|
ctx.gg.draw_rect(center + x * Scale, center + y * Scale, 1, 1, gx.Red)
|
2019-07-19 14:15:47 +03:00
|
|
|
}
|
|
|
|
}
|
2019-07-23 01:19:20 +03:00
|
|
|
|
|
|
|
fn update() {
|
2020-01-16 02:07:59 +03:00
|
|
|
for {
|
|
|
|
gg.post_empty_event()
|
2019-12-09 18:53:17 +03:00
|
|
|
time.sleep_ms(16) // 60 fps
|
2020-01-16 02:07:59 +03:00
|
|
|
}
|
|
|
|
}
|