-
Notifications
You must be signed in to change notification settings - Fork 4
handles popping of empty stack #9
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
|
||
#adapted from https://github.com/jorenham/numpy_turtle/blob/master/numpy_turtle/numpy_turtle.py | ||
from typing import Tuple, Union | ||
|
||
import numpy as np | ||
|
@@ -11,23 +13,19 @@ | |
class Turtle: | ||
def __init__(self, array: np.ndarray, deg: bool = False, aa: bool = False): | ||
"""Draw on a NumPy array using turtle graphics. | ||
|
||
Starts at (0, 0) (top-left corner) with a direction of 0 (pointing | ||
down). | ||
|
||
Parameters | ||
---------- | ||
array: np.ndarray | ||
The 2D array to write to. Can be either of shape h x w (grayscale), | ||
h x w x c (e.g. rgb for c=3 channels). | ||
The dtype is used to determine the color depth of each channel: | ||
|
||
* `bool` for 2 colors. | ||
* All `np.integer` subtypes for discrete depth, ranging from 0 to | ||
its max value (e.g. `np.uint8` for values in 0 - 255). | ||
* All `np.floating` subtypes for continuous depth, ranging from 0 | ||
to 1. | ||
|
||
deg : :obj:`bool`, optional | ||
Use degrees instead of radians. | ||
aa : :obj:`bool`, optional | ||
|
@@ -97,10 +95,12 @@ def __draw_line(self, new_c, new_r): | |
for c in range(self.__channels): | ||
self.array[rr, cc, c] = val * self.__color[c] | ||
|
||
def stacklen(self): | ||
return len(self.__stack) | ||
|
||
def forward(self, distance: float): | ||
"""Move in the current direction and draw a line with Euclidian | ||
distance. | ||
|
||
Parameters | ||
---------- | ||
distance : int | ||
|
@@ -117,7 +117,6 @@ def forward(self, distance: float): | |
|
||
def rotate(self, angle: float): | ||
"""Rotate the turtle by a given angle | ||
|
||
Parameters | ||
---------- | ||
angle | ||
|
@@ -134,8 +133,10 @@ def push(self): | |
|
||
def pop(self): | ||
"""Restore the state that was last pushed. | ||
allow for handling of pops of empty stacks | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that by default, this shouldn't be allowed. However, I like the idea of allowing this by setting a boolean keyword argument, so that the method signature becomes something like |
||
""" | ||
self.__direction, self.__r, self.__c = self.__stack.pop() | ||
if len(self.__stack) != 0: | ||
self.__direction, self.__r, self.__c = self.__stack.pop() | ||
|
||
def reset(self): | ||
"""Set direction and position to 0 and empty the stack. | ||
|
@@ -172,4 +173,4 @@ def color(self, c: Color): | |
if _c < 0 or _c > self.__depth: | ||
raise ValueError('Color value out of range') | ||
|
||
self.__color = np.array(c, dtype=self.__dtype) | ||
self.__color = np.array(c, dtype=self.__dtype) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The trailing newline has been removed here, could you place it back? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be cleaner as a
@property
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also prefer a more verbose name, e.g.
stack_size