fix snowflake

This commit is contained in:
nek0 2024-10-13 05:30:29 +02:00
parent 223d589f84
commit f77dbda66c

View file

@ -48,14 +48,14 @@ pub fn main() {
let mut flip = true; let mut flip = true;
let length = width / 2; let length = width / 2;
let triangle_height = (PI / 3 as f32).sin() * length as f32; let triangle_side = (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 * 3 - triangle_height as u32}} , end: Position{x: width / 2, y: height / 4 * 3 - triangle_side as u32}}
, Line{start: Position{x: width / 2, y: height / 4 * 3 - triangle_height as u32} , Line{start: Position{x: width / 2, y: height / 4 * 3 - triangle_side as u32}
, end: Position{x: width / 4, y: 3 * height / 4}} , end: Position{x: width / 4, y: 3 * height / 4}}
]}; ]};
@ -98,16 +98,18 @@ 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 segment_length = (((dx ^ 2) as f32) + ((dy ^ 2) as f32)).sqrt() / 3 as f32; let segment_length = (((dx.pow(2)) as f32) + ((dy.pow(2)) as f32)).sqrt() / 3 as f32;
let angle = (dy as f32 / dx as f32).atan() + PI / 3 as f32; let angle = atan(dy as f32, dx as f32) + PI / 3 as f32;
println!("{}", angle * 180 as f32 / PI); //println!("{}, {}, {}", segment_length, angle.sin(), angle.cos());
//println!("{}, {}, {}", segment_length, dx.pow(2), dy);
if segment_length > 1.0 {
let second = Position { let second = Position {
x : (line.start.x as i32 + dx / 3) as u32, x : (line.start.x as i32 + dx / 3) as u32,
y : (line.start.y as i32 + dy / 3) as u32, y : (line.start.y as i32 + dy / 3) as u32,
}; };
let third = Position { let third = Position {
x : (second.clone().x as i32 + ((segment_length * angle.sin())) as i32) as u32, x : (second.clone().x as i32 + ((segment_length * angle.cos())) as i32) as u32,
y : (second.clone().y as i32 + ((segment_length * angle.cos())) as i32) as u32, y : (second.clone().y as i32 + ((segment_length * angle.sin())) as i32) as u32,
}; };
let fourth = Position { let fourth = Position {
x : (line.start.x as i32 + 2 * (dx / 3)) as u32, x : (line.start.x as i32 + 2 * (dx / 3)) as u32,
@ -134,6 +136,10 @@ fn break_line (mut acc: Vec<Line>, line: & Line) -> Vec<Line> {
acc.append(& mut new_lines); acc.append(& mut new_lines);
acc.to_vec() acc.to_vec()
} else {
acc.append(& mut vec![line.clone()]);
acc.to_vec()
}
} }
fn draw_lines (canvas: & Canvas<Window>, color: Color, lines: & Vec<Line>) { fn draw_lines (canvas: & Canvas<Window>, color: Color, lines: & Vec<Line>) {
@ -147,3 +153,13 @@ fn draw_lines (canvas: & Canvas<Window>, color: Color, lines: & Vec<Line>) {
).unwrap(); ).unwrap();
} }
} }
fn atan(a: f32, b: f32) -> f32 {
if b == 0.0 {
0.0
} else if b < 0.0 {
(a / b).atan() + PI
} else {
(a / b).atan()
}
}