twiddling

This commit is contained in:
nek0 2024-10-12 05:59:49 +02:00
parent 0953497a79
commit 223d589f84

View file

@ -6,6 +6,7 @@ use sdl2::keyboard::Keycode;
use sdl2::render::Canvas; use sdl2::render::Canvas;
use sdl2::video::Window; use sdl2::video::Window;
use sdl2::gfx::primitives::DrawRenderer; use sdl2::gfx::primitives::DrawRenderer;
use std::f32::consts::PI;
use std::time::Duration; use std::time::Duration;
struct Base { struct Base {
@ -46,12 +47,15 @@ pub fn main() {
let mut i = 0; let mut i = 0;
let mut flip = true; let mut flip = true;
let length = width / 2;
let triangle_height = (PI / 3 as f32).sin() * length as f32;
let mut base = Base{lines: let mut base = Base{lines:
vec![ Line{start: Position{x: width / 4, y: 3 * height / 4} vec![ Line{start: Position{x: width / 4, y: 3 * height / 4}
, end: Position{x: 3 * width / 4, y: 3 * height / 4}} , end: Position{x: 3 * width / 4, y: 3 * height / 4}}
, Line{start: Position{x: 3* width / 4, y: 3 * height / 4} , Line{start: Position{x: 3* width / 4, y: 3 * height / 4}
, end: Position{x: width / 2, y: height / 4}} , end: Position{x: width / 2, y: height / 4 * 3 - triangle_height as u32}}
, Line{start: Position{x: width / 2, y: height / 4} , Line{start: Position{x: width / 2, y: height / 4 * 3 - triangle_height as u32}
, end: Position{x: width / 4, y: 3 * height / 4}} , end: Position{x: width / 4, y: 3 * height / 4}}
]}; ]};
@ -94,47 +98,41 @@ fn step (lines: Vec<Line>) -> Vec<Line> {
fn break_line (mut acc: Vec<Line>, line: & Line) -> Vec<Line> { fn break_line (mut acc: Vec<Line>, line: & Line) -> Vec<Line> {
let dx = line.end.x as i32 - line.start.x as i32; let dx = line.end.x as i32 - line.start.x as i32;
let dy = line.end.y as i32 - line.start.y as i32; let dy = line.end.y as i32 - line.start.y as i32;
let first = Line{ let segment_length = (((dx ^ 2) as f32) + ((dy ^ 2) as f32)).sqrt() / 3 as f32;
start: Position { let angle = (dy as f32 / dx as f32).atan() + PI / 3 as f32;
x : line.start.x, println!("{}", angle * 180 as f32 / PI);
y : line.start.y, let second = Position {
}, x : (line.start.x as i32 + dx / 3) as u32,
end: Position { y : (line.start.y as i32 + dy / 3) as u32,
x : (line.start.x as i32 + dx / 3) as u32,
y : (line.start.y as i32 + dy / 3) as u32,
},
}; };
let second = Line{ let third = Position {
start: Position { x : (second.clone().x as i32 + ((segment_length * angle.sin())) as i32) as u32,
x : (line.start.x as i32 + dx / 3) as u32, y : (second.clone().y as i32 + ((segment_length * angle.cos())) as i32) as u32,
y : (line.start.y as i32 + dy / 3) as u32,
},
end: Position {
x : line.start.x,
y : (line.start.y as i32 + 2 * (dy / 3)) as u32,
},
}; };
let third = Line{ let fourth = Position {
start: Position { x : (line.start.x as i32 + 2 * (dx / 3)) as u32,
x : line.start.x, y : (line.start.y as i32 + 2 * (dy / 3)) as u32,
y : (line.start.y as i32 + 2 * (dy / 3)) as u32,
},
end: Position {
x : (line.start.x as i32 + 2 * (dx / 3)) as u32,
y : (line.start.y as i32 + 2 * (dy / 3)) as u32,
},
}; };
let fourth = Line{ let mut new_lines = vec![
start: Position { Line {
x : (line.start.x as i32 + 2 * (dx / 3)) as u32, start: line.start.clone(),
y : (line.start.y as i32 + 2 * (dy / 3)) as u32, end: second.clone(),
}, },
end: Position { Line {
x : (line.start.x as i32 + dx) as u32, start: second.clone(),
y : (line.start.y as i32 + dy) as u32, end: third.clone(),
}, },
}; Line {
acc.append(& mut vec![first, second, third, fourth]); start: third.clone(),
end: fourth.clone(),
},
Line {
start: fourth.clone(),
end: line.end.clone(),
},
];
acc.append(& mut new_lines);
acc.to_vec() acc.to_vec()
} }