Skip to content

Commit e7f1815

Browse files
PaulDancesunjay
authored andcommitted
Add documentation to arc methods
It should provide good explanations of both methods with dedicated and thourough examples. Signed-off-by: Paul Mabileau <[email protected]>
1 parent a32d75f commit e7f1815

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

src/turtle.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,122 @@ impl Turtle {
206206
block_on(self.turtle.wait(secs))
207207
}
208208

209+
/// Draw a circular arc starting at the current position and going to the left of the turtle,
210+
/// thus globally turning counterclockwise.
211+
///
212+
/// It is configured through the `radius` and `extent` arguments:
213+
/// * `radius` places the center of the arc itself `Distance` units away from the left of the
214+
/// turtle, with respect to its current orientation. When negative, it does so to the right.
215+
/// * `extent` controls how much of the arc is to be drawn, that is to say the `Angle` that
216+
/// forms the circular portion of the given `radius`. When negative, the turtle moves
217+
/// backwards instead, but it still goes to its left. It can also be greater than the
218+
/// turtle's current angle unit domain limit, i.e. 360° when using degrees or 2π when using
219+
/// radians: the turtle will simply continue to draw until the complete angle is reached.
220+
///
221+
/// This method does *nothing* if either one of the provided arguments is not "normal" in the
222+
/// sense of [floating-point numbers' definition](f64::is_normal) of it.
223+
///
224+
/// # Example
225+
///
226+
/// ```rust
227+
/// # use turtle::Turtle;
228+
/// let mut turtle = Turtle::new();
229+
///
230+
/// // No movement when anormal.
231+
/// turtle.arc_left(0.0, 1.0);
232+
/// turtle.arc_left(f64::NAN, -f64::INFINITY);
233+
/// assert_eq!(turtle.position(), [0.0, 0.0].into());
234+
/// assert_eq!(turtle.heading(), 90.0);
235+
///
236+
/// // Quarter of a circle.
237+
/// turtle.arc_left(100.0, 90.0);
238+
/// assert!((turtle.position() - [-100.0, 100.0].into()).len() <= 0.5);
239+
/// assert!((turtle.heading() - 180.0).abs() <= 0.1);
240+
///
241+
/// // Full circle.
242+
/// turtle.reset();
243+
/// turtle.arc_left(100.0, 360.0);
244+
/// assert!((turtle.position() - [0.0, 0.0].into()).len() <= 0.5);
245+
/// assert!((turtle.heading() - 90.0).abs() <= 0.1);
246+
///
247+
/// // Full + quarter circle.
248+
/// turtle.reset();
249+
/// turtle.arc_left(100.0, 360.0 + 90.0);
250+
/// assert!((turtle.position() - [-100.0, 100.0].into()).len() <= 2.5);
251+
/// assert!((turtle.heading() - 180.0).abs() <= 0.1);
252+
///
253+
/// // Negative radius: flip center to the right.
254+
/// turtle.reset();
255+
/// turtle.arc_left(-100.0, 90.0);
256+
/// assert!((turtle.position() - [100.0, 100.0].into()).len() <= 0.5);
257+
/// assert!(turtle.heading().abs().min((turtle.heading() - 360.0).abs()) <= 0.1);
258+
///
259+
/// // Negative extent: go backwards to the left.
260+
/// turtle.reset();
261+
/// turtle.arc_left(100.0, -90.0);
262+
/// assert!((turtle.position() - [-100.0, -100.0].into()).len() <= 0.5);
263+
/// assert!(turtle.heading().abs().min((turtle.heading() - 360.0).abs()) <= 0.1);
264+
/// ```
209265
pub fn arc_left(&mut self, radius: Distance, extent: Angle) {
210266
block_on(self.turtle.arc_left(radius, extent))
211267
}
212268

269+
/// Draw a circular arc starting at the current position and going to the right of the turtle,
270+
/// thus globally turning clockwise.
271+
///
272+
/// It is configured through the `radius` and `extent` arguments:
273+
/// * `radius` places the center of the arc itself `Distance` units away from the right of the
274+
/// turtle, with respect to its current orientation. When negative, it does so to the left.
275+
/// * `extent` controls how much of the arc is to be drawn, that is to say the `Angle` that
276+
/// forms the circular portion of the given `radius`. When negative, the turtle moves
277+
/// backwards instead, but it still goes to its right. It can also be greater than the
278+
/// turtle's current angle unit domain limit, i.e. 360° when using degrees or 2π when using
279+
/// radians: the turtle will simply continue to draw until the complete angle is reached.
280+
///
281+
/// This method does *nothing* if either one of the provided arguments is not "normal" in the
282+
/// sense of [floating-point numbers' definition](f64::is_normal) of it.
283+
///
284+
/// # Example
285+
///
286+
/// ```rust
287+
/// # use turtle::Turtle;
288+
/// let mut turtle = Turtle::new();
289+
///
290+
/// // No movement when anormal.
291+
/// turtle.arc_right(0.0, 1.0);
292+
/// turtle.arc_right(f64::NAN, -f64::INFINITY);
293+
/// assert_eq!(turtle.position(), [0.0, 0.0].into());
294+
/// assert_eq!(turtle.heading(), 90.0);
295+
///
296+
/// // Quarter of a circle.
297+
/// turtle.arc_right(100.0, 90.0);
298+
/// assert!((turtle.position() - [100.0, 100.0].into()).len() <= 0.5);
299+
/// assert!(turtle.heading().abs().min((turtle.heading() - 360.0).abs()) <= 0.1);
300+
///
301+
/// // Full circle.
302+
/// turtle.reset();
303+
/// turtle.arc_right(100.0, 360.0);
304+
/// assert!((turtle.position() - [0.0, 0.0].into()).len() <= 0.5);
305+
/// assert!((turtle.heading() - 90.0).abs() <= 0.1);
306+
///
307+
/// // Full + quarter circle.
308+
/// turtle.reset();
309+
/// turtle.arc_right(100.0, 360.0 + 90.0);
310+
/// assert!((turtle.position() - [100.0, 100.0].into()).len() <= 2.5);
311+
/// assert!(turtle.heading().abs().min((turtle.heading() - 360.0).abs()) <= 0.1);
312+
///
313+
/// // Negative radius: flip center to the left.
314+
/// turtle.reset();
315+
/// turtle.arc_right(-100.0, 90.0);
316+
/// assert!((turtle.position() - [-100.0, 100.0].into()).len() <= 0.5);
317+
/// assert!((turtle.heading() - 180.0).abs() <= 0.1);
318+
///
319+
/// // Negative extent: go backwards to the right.
320+
/// turtle.reset();
321+
/// turtle.arc_right(100.0, -90.0);
322+
/// assert!((turtle.position() - [100.0, -100.0].into()).len() <= 0.5);
323+
/// assert!((turtle.heading() - 180.0).abs() <= 0.1);
324+
/// ```
213325
pub fn arc_right(&mut self, radius: Distance, extent: Angle) {
214326
block_on(self.turtle.arc_right(radius, extent))
215327
}

0 commit comments

Comments
 (0)