From 4b1c0785a145b35affaf6ec4d6e35363e8fce383 Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Mon, 27 Jul 2020 13:09:36 +0200 Subject: spider web --- src/main.rs | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 120 insertions(+), 1 deletion(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index e7a11a9..20b9c9c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,122 @@ +use std::thread; +use std::time::Duration; + +extern crate rand; +use rand::seq::SliceRandom; +use rand::Rng; + +extern crate sdl2; +use sdl2::event::Event; +use sdl2::gfx::primitives::DrawRenderer; +use sdl2::pixels::Color; + +fn find_origin() -> (i16, i16) { + let mut rng = rand::thread_rng(); + (rng.gen_range(100, 400), rng.gen_range(100, 400)) +} + +struct Line { + x1: i16, + y1: i16, + x2: i16, + y2: i16, +} + +struct Pixel { + x: i16, + y: i16, +} + fn main() { - println!("Hello, world!"); + let context = sdl2::init().unwrap(); + let video = context.video().unwrap(); + let (height, width) = (500, 500); + let window = video.window("web", height, width).opengl().build().unwrap(); + let mut canvas = window.into_canvas().build().unwrap(); + let mut events = context.event_pump().unwrap(); + + canvas.set_draw_color(Color::RGB(0, 0, 0)); + canvas.clear(); + canvas.present(); + + let (x, y) = find_origin(); + let delay = 50; + let mut lines = Vec::new(); + for i in (0..(height as i16 + 1)).step_by(50) { + lines.push(Line { + x1: i, + y1: 0, + x2: x, + y2: y, + }); + lines.push(Line { + x1: i, + y1: height as i16, + x2: x, + y2: y, + }); + lines.push(Line { + x1: 0, + y1: i, + x2: x, + y2: y, + }); + lines.push(Line { + x1: width as i16, + y1: i, + x2: x, + y2: y, + }); + } + + let mut rng = rand::thread_rng(); + lines.shuffle(&mut rng); + + let a = rng.gen_range(-10.0, 10.0); + let b = rng.gen_range(-10.0, 10.0); + + let mut pixels = Vec::new(); + for i in (1000..200000).step_by(10) { + let i = i as f64 * 0.001; + let x = (a + b * i) * i.cos() + x as f64; + let y = (a + b * i) * i.sin() + y as f64; + pixels.push(Pixel { + x: x as i16, + y: y as i16, + }); + } + + let mut i = 0; + let mut run = true; + while run { + for event in events.poll_iter() { + if let Event::Quit { .. } = event { + run = false + }; + } + + if i < lines.len() { + let line = &lines[i]; + canvas + .line( + line.x1, + line.y1, + line.x2, + line.y2, + Color::RGB(255, 255, 255), + ) + .unwrap(); + canvas.present(); + thread::sleep(Duration::from_millis(delay)); + } + if i < pixels.len() { + let pixel = &pixels[i]; + canvas + .pixel(pixel.x, pixel.y, Color::RGB(255, 255, 255)) + .unwrap(); + canvas.present(); + } + + i += 1; + } } -- cgit v1.2.3