main
serr 2025-06-25 17:11:15 +03:00
parent ec47191a4d
commit 79cb97282d
10 changed files with 15 additions and 26 deletions

View File

Before

Width:  |  Height:  |  Size: 3.6 KiB

After

Width:  |  Height:  |  Size: 3.6 KiB

BIN
imgs/polygons/heptagon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.6 KiB

BIN
imgs/polygons/hexagon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

BIN
imgs/polygons/nonagon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

BIN
imgs/polygons/octagon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

BIN
imgs/polygons/pentagon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

BIN
imgs/polygons/square.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

BIN
imgs/polygons/triangle.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.5 KiB

View File

@ -3,43 +3,32 @@ mod turtle;
use turtle::{defaults, Turtle};
#[allow(dead_code)]
fn square() {
let mut turtle = Turtle::new();
let default_pos = defaults::position();
let steps_count = 20;
turtle.set_pos((
default_pos.0 - (turtle.step_length() * steps_count / 2) as i32,
default_pos.1 + (turtle.step_length() * steps_count / 2) as i32
));
turtle.left_forward(90.0, steps_count);
for _ in 0..3 {
turtle.right_forward(90.0, steps_count);
}
turtle.save("imgs/square.png").unwrap();
}
#[allow(dead_code)]
fn circle() {
fn polygon(corners_number: u32, steps_count: u32, output_path: &'static str) {
let mut turtle = Turtle::new();
let default_pos = defaults::position();
let circumference = (36 * 2 * defaults::STEP_LENGTH) as f64;
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);
turtle.set_pos((
default_pos.0 - (turtle.step_length() * 2 / 2) as i32,
default_pos.0 - (turtle.step_length() * steps_count / 2) as i32,
default_pos.1 - radius as i32
));
for _ in 0..36 {
turtle.forward_right(2, 10.0);
for _ in 0..corners_number {
turtle.forward_right(steps_count, degree);
}
turtle.save("imgs/circle.png").unwrap();
turtle.save(output_path).unwrap();
}
fn main() {
square();
circle();
polygon(3, 10, "imgs/polygons/triangle.png");
polygon(4, 10, "imgs/polygons/square.png");
polygon(5, 10, "imgs/polygons/pentagon.png");
polygon(6, 10, "imgs/polygons/hexagon.png");
polygon(7, 10, "imgs/polygons/heptagon.png");
polygon(8, 10, "imgs/polygons/octagon.png");
polygon(9, 5, "imgs/polygons/nonagon.png");
polygon(36, 2, "imgs/polygons/circle.png");
}