-
Notifications
You must be signed in to change notification settings - Fork 53
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
Add arc method to Turtle #82
Comments
This comment has been minimized.
This comment has been minimized.
@sunjay I'd like to help with this, I think it should be a good way to get familiar with the implementation details. |
@PaulDance Great idea! I am not very available this week, but if you could ping me on or after Thursday next week I'd be happy to discuss it. I think I mainly just need to rewrite the mentoring instructions. If you want to poke around at it in the meantime, you may be able to figure out what to do by looking at my comments in #189. |
@PaulDance The instructions are updated now. 🎉 |
Goal: Provide some utility for drawing arcs. This is currently approximated in many turtle programs by many short line segments with small angles between them. For example:
turtle/examples/snowman.rs
Lines 191 to 201 in cc40975
The
circle
function in the Python implementation is an example of a potential API for this that works well in Python. Unfortunately, this is a bit tricky to copy directly into turtle because Rust doesn't have optional function arguments. Even if it did, I think we can do better in terms of API design in several ways:circle
which makes sense in Python because the callcircle(radius)
reads very naturally with all the optional arguments omittedarc
so it reads naturally when bothradius
andextent
are provided, e.g.arc(radius, extent)
steps
parameter because it approximates the curve with straight linesarc
method onpathfinder_canvas
arc_left
andarc_right
methods instead that are consistent with theleft
andright
methodsleft
andright
support passing in negative values soarc_left
andarc_right
would as wellARC
command in the LOGO programming language (We are not bound by the design decisions of LOGO or Python, so it makes sense to try to do better here)Aside: The Builder Pattern
Usually, when an API needs optional arguments in Rust, we turn to the builder pattern. (We could use
Option
to simulate optional arguments, but that is unidiomatic and difficult to read.) For example, we could have anArc
struct with an API that looks like:This definitely makes sense when the struct being initialized has lots of options and reasonable defaults. In this case we only have two arguments (maybe 3 if you include Python's
steps
). A default of 360° forextent
only makes sense if the method is calledcircle
. We could rename the method and the builder, butturtle.circle(Circle::new(5.0))
doesn't really improve anything and is far too verbose.Mentoring Instructions
To make this issue easier to implement and easier to review, I've split it into two parts: 1) adding
arc_left
andarc_right
methods that we simulate usingforward
andright
and 2) implementing the full circular arc animation system. That way we can get the method implemented and documented without needing you to master the math required for the animation right away.Part 1: Adding the methods
Read through all the instructions first before getting started.
arc_left(radius: Distance, extent: Angle)
method andarc_right(radius: f64, extent: f64)
method toTurtle
arc_left
method turns the turtle left (counterclockwise) as it draws and thearc_right
method turns the turtle right (clockwise) as it drawsradius
argument causes the center of the arc to extend out to the left of the turtle inarc_left
and to the right of the turtle inarc_right
radius
argument is negative, the center of the arc flips to the opposite side of the turtleextent
argument dictates how much of a circle to drawextent
argument is negative, the turtle moves backwards instead of forwardsextent
can be more than 360 degrees (in that case, the turtle would draw a complete circle and then keep drawing around the same circle until it reaches the desired angle)Turtle
calledarc_left
andarc_right
that block on corresponding methods onAsyncTurtle
(add them underneath thewait
method)turtle/src/turtle.rs
Lines 191 to 193 in cc40975
arc_left
andarc_right
async methods onAsyncTurtle
thatawait
on a newcircular_arc
method onProtocolClient
(add them underneath thewait
method)turtle/src/async_turtle.rs
Lines 88 to 91 in cc40975
ProtocolClient
with signaturecircular_arc(radius: Distance, extent: Radians, direction: RotationDirection)
(add the method underneathrotate_in_place
)Radians
type to disambiguate the unit of the angle andRotationDirection
to disambiguate between calls toarc_left
andarc_right
move_forward
androtate_in_place
methods onProtocolClient
Radians
as neededTurtle::arc_left
andTurtle::arc_right
)forward
,backward
,left
, andright
for some examples how this could be structuredcircle
function in the Python implementation for how they described itexamples/
directory that draws something cool using this featureProtocolClient
to see if the extent or radius are zero/infturtle/src/ipc_protocol/protocol.rs
Lines 355 to 357 in cc40975
Turtle
as unstable since we'll want people to try out the API before we decide to keep itturtle/src/drawing.rs
Lines 396 to 397 in de9fa48
CONTRIBUTING.md
Part 2: Arc Animation
TODO: Please ask for these mentoring instructions when Part 1 has been completed.
Notes:
arc
inpathfinder_canvas
As always, please don't hesitate to questions if anything is unclear or needs more explanation!
The text was updated successfully, but these errors were encountered: