Skip to content

Commit

Permalink
turtle docs!
Browse files Browse the repository at this point in the history
  • Loading branch information
ejkaplan committed Nov 29, 2023
1 parent f3a6b13 commit 6c868b2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
36 changes: 30 additions & 6 deletions docs/api_ref/turtle.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Turtle Graphics

## Explanation & Example

ElkPlot includes a recreation of [Logo's](https://en.wikipedia.org/wiki/Logo_(programming_language)) turtle graphics. This is intended as a quick and easy way for learners to start making art without having to go too deep on the Python programming language. (Yet)

Getting started is super easy - just make a turtle and give it some instructions.
Expand All @@ -9,14 +11,36 @@ import elkplot

w, h = elkplot.sizes.LETTER

# the turtle starts at (0, 0) facing right
gamera = elkplot.Turtle(use_degrees=True)
gamera.forward(6) # move forward 6 inches
gamera.turn_right(90) # turn right 90 degrees
gamera.forward(4) # move forward 4 inches

# draw the left eye
gamera.turn_right(90)
gamera.forward(2)
gamera.raise_pen()

# draw the right eye
gamera.goto(2, 0)
gamera.lower_pen()
gamera.forward(2)
gamera.raise_pen()

# draw the mouth
gamera.goto(-1.5, 2.5)
gamera.lower_pen()
gamera.turn_left(45)
gamera.forward(1.5)
gamera.turn_left(45)
gamera.forward(3)
gamera.turn_left(45)
gamera.forward(1.5)

# render the drawing, center it on a letter size sheet, and draw
d = gamera.drawing()
d = elkplot.center(d, w, h) # center the drawing on a letter size sheet of paper
elkplot.draw(d, w, h, plot=False)
d = elkplot.center(d, w, h)
elkplot.draw(d, w, h, plot=True)
```

:::
![Turtle Plot Preivew](turtle_preview.png)

:::elkplot.turtle
Binary file added docs/api_ref/turtle_preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 9 additions & 5 deletions elkplot/turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def __init__(
x (float, optional): x-coordinate of the turtle's starting position. Defaults to 0.
y (float, optional): y-coordinate of the turtle's starting position. Defaults to 0.
heading (float, optional): Direction the turtle is pointing. Defaults to 0.
use_degrees (bool, optional): Should angles be given in radians or degrees? Defaults to False.
"""
self._state = TurtleState(shapely.Point(x, y), heading, True)
self._stack: list[TurtleState] = []
Expand All @@ -42,7 +43,7 @@ def heading(self) -> float:
if self._use_degrees:
return self._state.heading * RAD_TO_DEG
return self._state.heading

@property
def heading_rad(self) -> float:
return self._state.heading
Expand Down Expand Up @@ -89,7 +90,9 @@ def forward(self, distance: float) -> Turtle:
Returns:
Turtle: Return self so that commands can be chained
"""
dx, dy = distance * np.cos(self.heading_rad), distance * np.sin(self.heading_rad)
dx, dy = distance * np.cos(self.heading_rad), distance * np.sin(
self.heading_rad
)
return self.goto(self.x + dx, self.y + dy)

def backward(self, distance: float) -> Turtle:
Expand Down Expand Up @@ -123,7 +126,9 @@ def turn_right(self, angle: float) -> Turtle:
Returns:
Turtle: Return self so that commands can be chained
"""
new_heading = self.heading_rad + angle * (DEG_TO_RAD if self._use_degrees else 1)
new_heading = self.heading_rad + angle * (
DEG_TO_RAD if self._use_degrees else 1
)
self._state = dataclasses.replace(self._state, heading=new_heading)
return self

Expand Down Expand Up @@ -202,8 +207,7 @@ def drawing(self) -> shapely.MultiLineString:
can be further modified or plotted.
Returns:
shapely.MultiLineString: The MultiLineString composed from all the lines
the turtle drew.
shapely.MultiLineString: The MultiLineString composed from all the lines the turtle drew.
"""
if self.pen_down:
self.raise_pen()
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ nav:
- Command Line Interface: api_ref/cli.md
- Drawing Text: api_ref/text.md
- Easing: api_ref/easing.md
- Turtle: api_ref/turtle.md
- Tips and Tricks: tips_and_tricks.md
- Alternative Libraries: alternatives.md
- Examples: examples.md
Expand Down

0 comments on commit 6c868b2

Please sign in to comment.