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