написал функцию рисования полигона по центру холста

main
serr 2025-06-25 18:16:39 +03:00
parent 79cb97282d
commit 3e02e439f1
8 changed files with 7 additions and 6 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.6 KiB

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.2 KiB

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.2 KiB

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.5 KiB

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.2 KiB

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.5 KiB

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

After

Width:  |  Height:  |  Size: 2.9 KiB

View File

@ -7,17 +7,18 @@ fn polygon(corners_number: u32, steps_count: u32, output_path: &'static str) {
let mut turtle = Turtle::new();
let default_pos = defaults::position();
let degree = 360.0 / corners_number as f64;
let circumference = (corners_number * steps_count * defaults::STEP_LENGTH) as f64;
let radius = circumference / (2.0 * std::f64::consts::PI);
let side_length = turtle.step_length() * steps_count;
let pi = std::f64::consts::PI;
let circumscribed_circle_radius = side_length as f64 / (2.0 * (pi / corners_number as f64).sin());
let vertical_offset = circumscribed_circle_radius * (pi / corners_number as f64).cos();
turtle.set_pos((
default_pos.0 - (turtle.step_length() * steps_count / 2) as i32,
default_pos.1 - radius as i32
default_pos.0 - (side_length / 2) as i32,
default_pos.1 - vertical_offset as i32,
));
for _ in 0..corners_number {
turtle.forward_right(steps_count, degree);
turtle.forward_right(steps_count, 360.0 / corners_number as f64);
}
turtle.save(output_path).unwrap();
}