From 79cb6caee1513c6b70a1f2201b73c222af3ec007 Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Tue, 10 Mar 2020 10:15:01 -0500 Subject: plots *a waveform*, not timed correctly --- src/tom.rs | 61 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 30 insertions(+), 31 deletions(-) (limited to 'src/tom.rs') diff --git a/src/tom.rs b/src/tom.rs index b899b08..a8fd4a4 100644 --- a/src/tom.rs +++ b/src/tom.rs @@ -1,5 +1,3 @@ -use std::f32::consts::PI; - use luminance::context::GraphicsContext; use luminance::framebuffer::Framebuffer; use luminance::pipeline::PipelineState; @@ -55,50 +53,30 @@ fn relative(x: f32, in_min: f32, in_max: f32, out_min: f32, out_max: f32) -> f32 (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min } -fn gen_sin(start: f32) -> Vec { - let mut vertices: Vec = Vec::new(); - - let end = start + (2.0 * PI); - let mut x = start; - while x < end { - vertices.push(Vertex { - position: VertexPosition::new([relative(x, start, end, -0.9, 0.9), x.sin() * 0.9]), - color: VertexRGB::new(constants::C64_GREEN), - }); - - x += 0.4; - } - - vertices -} - #[derive(Default)] pub struct Tom { border: Vec>, wave: Vec, - last_input: f32, + last_x: usize, + samples: Vec, tessalations: Vec, } impl Tom { - pub fn new() -> Tom { - let border = gen_border(); - let wave = gen_sin(0.0); - let tessalations = Vec::new(); - + pub fn new(samples: Vec) -> Tom { Tom { - border, - wave, - last_input: 0.0, - tessalations, + samples, + border: gen_border(), + wave: Vec::new(), + last_x: 0, + tessalations: Vec::new(), } } pub fn update(&mut self, mut surface: T) -> T { self.tessalations.clear(); - self.last_input += 0.1; - self.wave = gen_sin(self.last_input); + self.update_wave(); for vertices in self.border.iter() { self.tessalations.push( @@ -144,4 +122,25 @@ impl Tom { surface } + + fn update_wave(&mut self) { + self.wave.clear(); + + let start = self.last_x; + let end = start + 10; + + let max_y = 200.0; + let min_y = -200.0; + for x in start..end { + self.wave.push(Vertex { + position: VertexPosition::new([ + relative(x as f32, start as f32, end as f32, -0.9, 0.9), + relative(self.samples[x] as f32, min_y, max_y, -0.9, 0.9), + ]), + color: VertexRGB::new(constants::C64_GREEN), + }); + } + + self.last_x += 1; + } } -- cgit v1.2.3