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