Skip to content

Instantly share code, notes, and snippets.

@xjunko
Created December 21, 2023 21:36
Show Gist options
  • Save xjunko/6732c358880a380e29d745786930c459 to your computer and use it in GitHub Desktop.
Save xjunko/6732c358880a380e29d745786930c459 to your computer and use it in GitHub Desktop.
OpenTabletDriver Reconstructor Visualization using EMA function
// Using V programming language
// https://github.com/vlang
import gg
import gx
import math
@[heap]
pub struct Window {
pub mut:
ctx &gg.Context = unsafe { nil }
done bool
last_average []f32
actual []f32
weight f32 = 0.5
}
pub fn (window &Window) begin(_ voidptr) {
}
pub fn (window &Window) draw(_ voidptr) {
window.ctx.begin()
window.ctx.draw_text(100, 50, "Reverse EMA Visualization", color: gx.white, size: 32)
window.ctx.draw_text(100, 75, "Weight: ${window.weight:.2f}", color: gx.white, size: 32)
if window.done {
window.ctx.draw_rect_filled(window.last_average[0], window.last_average[1], 32, 32, gx.Color{255, 0, 0, 255})
window.ctx.draw_rect_filled(window.actual[0], window.actual[1], 32, 32, gx.Color{0, 255, 0, 150})
}
window.ctx.end()
}
pub fn (mut window Window) mouse_move(x f32, y f32, _ voidptr) {
position := [x, y]
mut true_point := [x, y]
true_point = match window.done {
true { reverse_ema_function(position, window.last_average, window.weight) }
false { position }
}
window.last_average = position
window.actual = true_point
window.done = true
}
pub fn (mut window Window) scroll(e &gg.Event, _ voidptr) {
window.weight += 0.01 * (e.scroll_y / 4.0)
window.weight = math.min(math.max(window.weight, .0), 1.0)
}
pub fn reverse_ema_function(position []f32, last_position []f32, weight f32) []f32 {
return [
((position[0] - last_position[0]) / weight) + last_position[0],
((position[1] - last_position[1]) / weight) + last_position[1]
]
}
fn main() {
mut window := &Window{}
window.ctx = gg.new_context(
width: 1280,
height: 720,
user_data: window
// FNs
init_fn: window.begin,
frame_fn: window.draw,
move_fn: window.mouse_move
scroll_fn: window.scroll
)
window.ctx.run()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment