Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding arc() method to Turtle #100

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions examples/beats_by_dre.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
extern crate turtle;
use turtle::{Point, Turtle};

fn main() {
let mut turtle = Turtle::new();
let init_pos: Point = turtle.position();
turtle.drawing_mut().set_title("Beats!");

let _radius_outer = 250.0;
turtle.set_pen_color("#fe0000");
turtle.pen_up();
turtle.go_to((init_pos.x - (_radius_outer), init_pos.y));
turtle.pen_down();
turtle.set_fill_color("#fe0000");
turtle.begin_fill();
turtle.arc(_radius_outer, None);
turtle.end_fill();

let _radius_mid = 115.0;
turtle.pen_up();
turtle.go_to((init_pos.x.round() - (_radius_mid), init_pos.y));
turtle.pen_down();
turtle.set_fill_color("white");
turtle.set_pen_color("white");
turtle.begin_fill();
turtle.arc(_radius_mid, None);
turtle.end_fill();

let _radius_inner = 60.0;
turtle.pen_up();
turtle.go_to((init_pos.x - (_radius_inner), init_pos.y));
turtle.pen_down();
turtle.set_fill_color("#fe0000");
turtle.begin_fill();
turtle.arc(_radius_inner, None);
turtle.end_fill();

let _size = 25.0;
turtle.pen_up();
turtle.go_to((init_pos.x - (_radius_mid) + _size - 2.0, init_pos.y));
turtle.pen_down();
turtle.set_pen_size(_size);
turtle.forward(250.0);
}
46 changes: 45 additions & 1 deletion src/turtle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt::Debug;
use std::rc::Rc;
use std::thread;
use std::time::Duration;

use std::f64::consts::PI;
#[cfg(not(target_arch = "wasm32"))]
use event::{Event, MouseButton};
use radians::{self, Radians};
Expand Down Expand Up @@ -237,6 +237,50 @@ impl Turtle {
thread::sleep(Duration::from_millis((secs * 1000.0) as u64));
}

/// Instructs the turtle to draw an arc by the given angle for a given radius from the
/// current position of the turtle.
///
/// The `radius` parameter is a floation point number that represents the radius of the arc
/// that you want to draw. If a negative `radius` is passed its absolute value is
/// considered for drawing the arc.
///
/// The `extent` parameter is a floating point number that represents how much you want the
/// turtle to rotate. If the `extent` parameter is `None` then it is defaulted to 360. If
/// a negative `extent` is passed the arc is drawn in the opposite direction.
///
/// #Example
///
/// ```rust
/// # use turtle::*;
/// # let mut turtle = Turtle::new();
/// //Turtle will draw an arc of 90 in a clockwise direction
/// turtle.arc(100.0, Some(90.0));
///
/// //Turtle will draw an arc of 90 in a anti-clockwise direction
/// turtle.arc(-100.0, Some(90.0));
///
/// //Turtle will draw an arc of 90 in a clockwise direction
/// turtle.arc(-100.0, Some(-90.0));
///
/// //Turtle will draw an arc of 90 in a anti-clockwise direction
/// turtle.arc(-100.0, Some(90.0));
/// ```
pub fn arc(&mut self, radius:Distance, extent:Option<Angle>) {
let angle = extent.unwrap_or(360.0);
let arc_length = 2.0 * PI * radius.abs() * angle.abs() / 360.0;
let step_count = (arc_length/4.0).round() + 1.0;
let step_length = arc_length / step_count;
let step_angle = angle / step_count;
for _ in 0..step_count as i32 {
self.forward(step_length);
if radius < 0.0 {
self.left(step_angle);
} else {
self.right(step_angle);
}
}
}

/// Retrieve a read-only reference to the drawing.
///
/// See the documentation for the [`Drawing` struct](struct.Drawing.html) for a complete
Expand Down