diff --git a/imgs/circle.png b/imgs/polygons/circle.png similarity index 100% rename from imgs/circle.png rename to imgs/polygons/circle.png diff --git a/imgs/polygons/heptagon.png b/imgs/polygons/heptagon.png new file mode 100644 index 0000000..8757a5c Binary files /dev/null and b/imgs/polygons/heptagon.png differ diff --git a/imgs/polygons/hexagon.png b/imgs/polygons/hexagon.png new file mode 100644 index 0000000..6c4f0d7 Binary files /dev/null and b/imgs/polygons/hexagon.png differ diff --git a/imgs/polygons/nonagon.png b/imgs/polygons/nonagon.png new file mode 100644 index 0000000..13dca03 Binary files /dev/null and b/imgs/polygons/nonagon.png differ diff --git a/imgs/polygons/octagon.png b/imgs/polygons/octagon.png new file mode 100644 index 0000000..3170b15 Binary files /dev/null and b/imgs/polygons/octagon.png differ diff --git a/imgs/polygons/pentagon.png b/imgs/polygons/pentagon.png new file mode 100644 index 0000000..547f054 Binary files /dev/null and b/imgs/polygons/pentagon.png differ diff --git a/imgs/polygons/square.png b/imgs/polygons/square.png new file mode 100644 index 0000000..c8d6163 Binary files /dev/null and b/imgs/polygons/square.png differ diff --git a/imgs/polygons/triangle.png b/imgs/polygons/triangle.png new file mode 100644 index 0000000..b7e2e67 Binary files /dev/null and b/imgs/polygons/triangle.png differ diff --git a/imgs/square.png b/imgs/square.png deleted file mode 100644 index 8eb356f..0000000 Binary files a/imgs/square.png and /dev/null differ diff --git a/src/main.rs b/src/main.rs index d8f458f..8073743 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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"); }