the line shrinks after me
also 1d or 2d graphs are possible
This commit is contained in:
122
src/main.rs
122
src/main.rs
@@ -5,6 +5,7 @@ use macroquad::prelude::*;
|
||||
|
||||
const DOT_RADIUS: f32 = 1.0;
|
||||
|
||||
#[derive(Clone,Copy)]
|
||||
struct Dot {
|
||||
x: f32,
|
||||
y: f32,
|
||||
@@ -48,46 +49,119 @@ impl DebugWindow {
|
||||
}
|
||||
|
||||
|
||||
enum Axis {
|
||||
One,
|
||||
Two
|
||||
}
|
||||
|
||||
struct Tail {
|
||||
points: [Dot; 500],
|
||||
head: usize,
|
||||
axis: Axis,
|
||||
x_offset: f32,
|
||||
y_offset: f32,
|
||||
px_per_s: f32,
|
||||
}
|
||||
impl Tail {
|
||||
fn new(axis: Axis) -> Self {
|
||||
|
||||
let (x_offset, y_offset) = match axis {
|
||||
Axis::One => (0., screen_height() /2.),
|
||||
Axis::Two => (screen_width() /2., screen_height() /2.),
|
||||
};
|
||||
|
||||
Self {
|
||||
// start all the dots off screen
|
||||
points: [Dot::new(-1.,-1.); 500],
|
||||
head: 0,
|
||||
axis,
|
||||
x_offset,
|
||||
y_offset,
|
||||
px_per_s: 500.,
|
||||
}
|
||||
}
|
||||
fn place_next(&mut self, delta_time: f32, x_displace: f32, y_displace: f32) {
|
||||
match self.axis {
|
||||
Axis::One => {
|
||||
// x will scroll while y gets displaced
|
||||
if self.x_offset >= screen_width() {
|
||||
self.x_offset = 0.;
|
||||
} else {
|
||||
self.x_offset = self.x_offset + (delta_time * self.px_per_s)
|
||||
}
|
||||
self.push(Dot::new(self.x_offset, self.y_offset+y_displace));
|
||||
},
|
||||
Axis::Two => {
|
||||
// both x and y will get displaced
|
||||
self.push(Dot::new(self.x_offset+x_displace,self.y_offset+y_displace))
|
||||
},
|
||||
}
|
||||
}
|
||||
fn push(&mut self, dot: Dot) {
|
||||
if self.head >= self.points.len() -1 {
|
||||
self.head = 0
|
||||
} else {
|
||||
self.head += 1
|
||||
}
|
||||
|
||||
self.points[self.head] = dot;
|
||||
}
|
||||
fn draw(&self) {
|
||||
// TODO fadding alpha?
|
||||
self.points.iter().for_each(|d| d.draw());
|
||||
}
|
||||
fn dots(&self) -> usize {
|
||||
self.points.len()
|
||||
}
|
||||
}
|
||||
|
||||
#[macroquad::main("Graph")]
|
||||
async fn main() {
|
||||
// Dot params
|
||||
let y_offset = screen_height() / 2.0;
|
||||
let mut x_offset = screen_width();
|
||||
let mut trail: Vec<Dot> = Vec::new();
|
||||
let mut y_displacement = 0.;
|
||||
let mut x_displacement = 0.;
|
||||
let mut tail = Tail::new(Axis::Two);
|
||||
|
||||
// Selection box
|
||||
let mut inital_x_pos = 0.;
|
||||
let mut inital_y_pos = 0.;
|
||||
|
||||
// Tooling
|
||||
let mut show_debug = false;
|
||||
let mut px_per_s: f32 = 100.;
|
||||
|
||||
let mut show_debug = true;
|
||||
let mut px_per_s: f32 = 1000.;
|
||||
|
||||
let stdin = stdin();
|
||||
let handle= stdin.lock();
|
||||
|
||||
loop {
|
||||
clear_background(BLACK);
|
||||
|
||||
// handle.
|
||||
let delta_time = get_frame_time();
|
||||
|
||||
if x_offset >= screen_width() {
|
||||
// wrap once you've hit the end of the line
|
||||
x_offset = 0.;
|
||||
trail.clear();
|
||||
} else {
|
||||
x_offset = x_offset + (delta_time * px_per_s);
|
||||
tail.place_next(delta_time, x_displacement, y_displacement);
|
||||
tail.draw();
|
||||
|
||||
if is_key_down(KeyCode::S) {
|
||||
if y_displacement < screen_height()/2. {
|
||||
y_displacement += 10.;
|
||||
}
|
||||
|
||||
let x = 0.+x_offset;
|
||||
let y = 0.+y_offset;
|
||||
|
||||
let dot = Dot::new(x,y);
|
||||
trail.push(dot);
|
||||
trail.iter().for_each(|d| d.draw());
|
||||
|
||||
if is_key_pressed(KeyCode::D) {
|
||||
}
|
||||
if is_key_down(KeyCode::W) {
|
||||
if y_displacement > -(screen_height()/2.) {
|
||||
y_displacement -= 10.;
|
||||
}
|
||||
}
|
||||
if is_key_down(KeyCode::D) {
|
||||
if x_displacement < screen_width()/2. {
|
||||
x_displacement += 10.;
|
||||
}
|
||||
}
|
||||
if is_key_down(KeyCode::A) {
|
||||
if x_displacement > -(screen_width()/2.) {
|
||||
x_displacement -= 10.;
|
||||
}
|
||||
}
|
||||
if is_key_pressed(KeyCode::F3) {
|
||||
show_debug = !show_debug;
|
||||
}
|
||||
if is_key_pressed(KeyCode::Up) {
|
||||
@@ -112,9 +186,9 @@ async fn main() {
|
||||
if show_debug {
|
||||
let mut debug = DebugWindow::new();
|
||||
debug.add_line(format!("FPS {}, Latency {:.2}ms", get_fps(), delta_time * 1000.0));
|
||||
debug.add_line(format!("px/s {}", px_per_s));
|
||||
debug.add_line(format!("Dots {}", trail.len()));
|
||||
debug.add_line(format!("Dots {}", tail.dots()));
|
||||
debug.add_line(format!("Cursor Pos {:?}", mouse_position()));
|
||||
debug.add_line(format!("px/s {}", px_per_s));
|
||||
debug.draw();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user