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 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:
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 * 3 - triangle_height as u32}}
, Line{start: 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_side as u32}
, end: Position{x: width / 4, y: 3 * height / 4}}
]};
@ -98,42 +98,48 @@ 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 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 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 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 mut new_lines = vec![
Line {
start: line.start.clone(),
end: second.clone(),
},
Line {
start: second.clone(),
end: third.clone(),
},
Line {
start: third.clone(),
end: fourth.clone(),
},
Line {
start: fourth.clone(),
end: line.end.clone(),
},
];
let segment_length = (((dx.pow(2)) as f32) + ((dy.pow(2)) as f32)).sqrt() / 3 as f32;
let angle = atan(dy as f32, dx as f32) + PI / 3 as f32;
//println!("{}, {}, {}", segment_length, angle.sin(), angle.cos());
//println!("{}, {}, {}", segment_length, dx.pow(2), dy);
if segment_length > 1.0 {
let second = Position {
x : (line.start.x as i32 + dx / 3) as u32,
y : (line.start.y as i32 + dy / 3) as u32,
};
let third = Position {
x : (second.clone().x 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 {
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(),
},
Line {
start: second.clone(),
end: third.clone(),
},
Line {
start: third.clone(),
end: fourth.clone(),
},
Line {
start: fourth.clone(),
end: line.end.clone(),
},
];
acc.append(& mut new_lines);
acc.to_vec()
acc.append(& mut new_lines);
acc.to_vec()
} else {
acc.append(& mut vec![line.clone()]);
acc.to_vec()
}
}
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();
}
}
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()
}
}