summaryrefslogtreecommitdiff
path: root/src/main.rs
blob: bd7e1a2c8d10625d0a8d697788905bfd417f1005 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
mod poly;
mod number;
mod lerp;

use number::Number;
use lerp::Lerp;

use std::thread;
use std::time::Duration;

use sdl2::pixels::Color;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::rect::Rect;

fn main() {
    let a = Lerp::new(vec![2.0, -2.0, 2.0]);
    let b = Lerp::new(vec![-1.0, 1.0, 2.0]);
    let pa = lerp::lp(a);
    let pb = lerp::lp(b);
    let p = poly::gcd(&pa, &pb);

    let sdl_context = sdl2::init().unwrap();
    let video_subsystem = sdl_context.video().unwrap();

    let window = video_subsystem.window("rust-sdl2 demo", 800, 600)
        .position_centered()
        .build()
        .unwrap();

    let mut canvas = window.into_canvas().build().unwrap();

    let mut event_pump = sdl_context.event_pump().unwrap();
    'running: loop {
        canvas.set_draw_color(Color::RGB(20, 20, 20));
        canvas.clear();
        for event in event_pump.poll_iter() {
            match event {
                Event::Quit {..} |
                    Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
                        break 'running
                    },
                _ => {}
            }
        }

        let s = -600.0 / 10.0;
        let k = 600.0 / 2.0;
        for t in -800..800 {
            let x = k + s * pa.eval(t as Number / 100.0);
            let y = k + s * pb.eval(t as Number / 100.0);
            let z = k + s * p.eval(t as Number / 100.0);
            canvas.set_draw_color(Color::RGB(180, 20, 20));
            canvas.fill_rect(Rect::new(400 + t, x as i32, 5, 5)).unwrap();
            canvas.set_draw_color(Color::RGB(20, 180, 20));
            canvas.fill_rect(Rect::new(400 + t, y as i32, 5, 5)).unwrap();
            canvas.set_draw_color(Color::RGB(20, 20, 180));
            canvas.fill_rect(Rect::new(400 + t, z as i32, 2, 2)).unwrap();
            canvas.set_draw_color(Color::RGB(255, 0, 0));
            canvas.fill_rect(Rect::new(400 + t, k as i32, 2, 2)).unwrap();
        }

        canvas.present();
        thread::sleep(Duration::new(0, 1_000_000_000u32 / 60));
    }
}