Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Leslie-Jiang-Hamster committed Jan 22, 2024
0 parents commit df68f7d
Show file tree
Hide file tree
Showing 349 changed files with 50,738 additions and 0 deletions.
7 changes: 7 additions & 0 deletions abstract_turtle/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

from .turtle_class import Turtle
from .canvas import Canvas
from .logging_canvas import LoggingCanvas
from .forwarding_canvas import ForwardingCanvas
from .pillow_canvas import PillowCanvas
from .tk_canvas import TkCanvas
95 changes: 95 additions & 0 deletions abstract_turtle/canvas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@

from abc import ABC, abstractmethod
from math import pi


class Canvas(ABC):
def __init__(self, width, height):
self.width = width
self.height = height
self._turtle = None

@property
def turtle(self):
return self._turtle

@turtle.setter
def turtle(self, turtle):
self._turtle = turtle
self.refreshed_turtle(turtle)

@abstractmethod
def draw_rectangular_line(self, start, end, color, width):
"""
Draw a 1 width line from START to END with the given color COLOR
"""
pass

def draw_line(self, start, end, color, width):
if width > 1:
self.draw_circle(start, width / 2, color, width, True, 0, 2 * pi)
self.draw_rectangular_line(start, end, color, width)
if width > 1:
self.draw_circle(end, width / 2, color, width, True, 0, 2 * pi)

@abstractmethod
def draw_circle(self, center, radius, color, width, is_filled, start, end):
"""
Draw a circle of width 1 with the given center CENTER, radius RADIUS, and color COLOR.
Only draw the portion with angle between START and END, moving counterclockwise from START to END.
RADIUS must be non-negative.
Fill the circle if IS_FILLED is true.
"""
pass

@abstractmethod
def fill_path(self, path, color):
"""
Fill the given polygon formed by the movements in PATH using fill color COLOR.
"""
pass

@abstractmethod
def axis_aligned_rectangle(self, bottom_left, width, height, color):
"""
Fill the given rectangle with bottom left corner BOTTOM_LEFT, and dimensions (WIDTH, HEIGHT),
and fill color COLOR
"""
pass

@abstractmethod
def set_bgcolor(self, color):
"""
Fill the entire background with the given COLOR
"""
pass

@abstractmethod
def clear(self):
"""
Clear everything in the foreground
"""
pass

@abstractmethod
def refreshed_turtle(self, drawn_turtle):
"""
Update the turtle to the given DrawnTurtle object, or remove the turtle if None is passed
"""
pass

@abstractmethod
def set_speed(self, speed):
"""
Set the animation speed, should be consistent with the built-in turtle module if implemented at all.
"""
pass

@abstractmethod
def exit_on_click(self):
"""
Blocks until a click, upon which the GUI is closed.
For non-gui apps, this does nothing, but GUI based apps should change the behavior.
"""
pass
143 changes: 143 additions & 0 deletions abstract_turtle/color_names.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
COLORS = {
"aliceblue": "#f0f8ff",
"antiquewhite": "#faebd7",
"aqua": "#00ffff",
"aquamarine": "#7fffd4",
"azure": "#f0ffff",
"beige": "#f5f5dc",
"bisque": "#ffe4c4",
"black": "#000000",
"blanchedalmond": "#ffebcd",
"blue": "#0000ff",
"blueviolet": "#8a2be2",
"brown": "#a52a2a",
"burlywood": "#deb887",
"cadetblue": "#5f9ea0",
"chartreuse": "#7fff00",
"chocolate": "#d2691e",
"coral": "#ff7f50",
"cornflowerblue": "#6495ed",
"cornsilk": "#fff8dc",
"crimson": "#dc143c",
"cyan": "#00ffff",
"darkblue": "#00008b",
"darkcyan": "#008b8b",
"darkgoldenrod": "#b8860b",
"darkgray": "#a9a9a9",
"darkgreen": "#006400",
"darkkhaki": "#bdb76b",
"darkmagenta": "#8b008b",
"darkolivegreen": "#556b2f",
"darkorange": "#ff8c00",
"darkorchid": "#9932cc",
"darkred": "#8b0000",
"darksalmon": "#e9967a",
"darkseagreen": "#8fbc8f",
"darkslateblue": "#483d8b",
"darkslategray": "#2f4f4f",
"darkturquoise": "#00ced1",
"darkviolet": "#9400d3",
"deeppink": "#ff1493",
"deepskyblue": "#00bfff",
"dimgray": "#696969",
"dodgerblue": "#1e90ff",
"firebrick": "#b22222",
"floralwhite": "#fffaf0",
"forestgreen": "#228b22",
"fuchsia": "#ff00ff",
"gainsboro": "#dcdcdc",
"ghostwhite": "#f8f8ff",
"gold": "#ffd700",
"goldenrod": "#daa520",
"gray": "#808080",
"green": "#008000",
"greenyellow": "#adff2f",
"honeydew": "#f0fff0",
"hotpink": "#ff69b4",
"indianred ": "#cd5c5c",
"indigo": "#4b0082",
"ivory": "#fffff0",
"khaki": "#f0e68c",
"lavender": "#e6e6fa",
"lavenderblush": "#fff0f5",
"lawngreen": "#7cfc00",
"lemonchiffon": "#fffacd",
"lightblue": "#add8e6",
"lightcoral": "#f08080",
"lightcyan": "#e0ffff",
"lightgoldenrodyellow": "#fafad2",
"lightgrey": "#d3d3d3",
"lightgreen": "#90ee90",
"lightpink": "#ffb6c1",
"lightsalmon": "#ffa07a",
"lightseagreen": "#20b2aa",
"lightskyblue": "#87cefa",
"lightslategray": "#778899",
"lightsteelblue": "#b0c4de",
"lightyellow": "#ffffe0",
"lime": "#00ff00",
"limegreen": "#32cd32",
"linen": "#faf0e6",
"magenta": "#ff00ff",
"maroon": "#800000",
"mediumaquamarine": "#66cdaa",
"mediumblue": "#0000cd",
"mediumorchid": "#ba55d3",
"mediumpurple": "#9370d8",
"mediumseagreen": "#3cb371",
"mediumslateblue": "#7b68ee",
"mediumspringgreen": "#00fa9a",
"mediumturquoise": "#48d1cc",
"mediumvioletred": "#c71585",
"midnightblue": "#191970",
"mintcream": "#f5fffa",
"mistyrose": "#ffe4e1",
"moccasin": "#ffe4b5",
"navajowhite": "#ffdead",
"navy": "#000080",
"oldlace": "#fdf5e6",
"olive": "#808000",
"olivedrab": "#6b8e23",
"orange": "#ffa500",
"orangered": "#ff4500",
"orchid": "#da70d6",
"palegoldenrod": "#eee8aa",
"palegreen": "#98fb98",
"paleturquoise": "#afeeee",
"palevioletred": "#d87093",
"papayawhip": "#ffefd5",
"peachpuff": "#ffdab9",
"peru": "#cd853f",
"pink": "#ffc0cb",
"plum": "#dda0dd",
"powderblue": "#b0e0e6",
"purple": "#800080",
"rebeccapurple": "#663399",
"red": "#ff0000",
"rosybrown": "#bc8f8f",
"royalblue": "#4169e1",
"saddlebrown": "#8b4513",
"salmon": "#fa8072",
"sandybrown": "#f4a460",
"seagreen": "#2e8b57",
"seashell": "#fff5ee",
"sienna": "#a0522d",
"silver": "#c0c0c0",
"skyblue": "#87ceeb",
"slateblue": "#6a5acd",
"slategray": "#708090",
"snow": "#fffafa",
"springgreen": "#00ff7f",
"steelblue": "#4682b4",
"tan": "#d2b48c",
"teal": "#008080",
"thistle": "#d8bfd8",
"tomato": "#ff6347",
"turquoise": "#40e0d0",
"violet": "#ee82ee",
"wheat": "#f5deb3",
"white": "#ffffff",
"whitesmoke": "#f5f5f5",
"yellow": "#ffff00",
"yellowgreen": "#9acd32"
}
41 changes: 41 additions & 0 deletions abstract_turtle/forwarding_canvas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

from .canvas import Canvas

def _forward(method):
def func(self, *args, **kwargs):
# pylint: disable=W0212
return getattr(self._canvas, method)(*args, **kwargs)
func.name = method
return func

class ForwardingCanvas(Canvas):
"""
Canvas that dispatches all calls to a contained canvas
"""
def __init__(self, canvas):
super().__init__(canvas.width, canvas.height)
self._canvas = canvas

@property
def turtle(self):
return self._canvas.turtle

@turtle.setter
def turtle(self, turtle):
self._canvas.turtle = turtle

def set_canvas(self, canvas):
canvas.turtle = self._canvas.turtle
self._canvas = canvas
self.width = canvas.width
self.height = canvas.height

draw_rectangular_line = _forward("draw_rectangular_line")
draw_circle = _forward("draw_circle")
fill_path = _forward("fill_path")
axis_aligned_rectangle = _forward("axis_aligned_rectangle")
set_bgcolor = _forward("set_bgcolor")
clear = _forward("clear")
refreshed_turtle = _forward("refreshed_turtle")
set_speed = _forward("set_speed")
exit_on_click = _forward("exit_on_click")
53 changes: 53 additions & 0 deletions abstract_turtle/logging_canvas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@

from abc import abstractmethod
from math import pi

from .canvas import Canvas

class AbstractLoggingCanvas(Canvas):

def __init__(self, width, height):
super().__init__(width, height)
self.drawn_turtle = None

@abstractmethod
def on_action(self, log_line):
pass

def draw_rectangular_line(self, start, end, color, width):
self.on_action(['draw_rectangular_line', [start.x, start.y, end.x, end.y], color, width])

def draw_circle(self, center, radius, color, width, is_filled, start, end):
self.on_action(['draw_circle', [center.x, center.y, radius], color, width, is_filled, start, end])

def fill_path(self, path, color):
self.on_action(['fill_path', [edge.json_friendly for edge in path], color])

def axis_aligned_rectangle(self, bottom_left, width, height, color):
self.on_action(['axis_aligned_rectangle', [bottom_left.x, bottom_left.y], width, height, color])

def set_bgcolor(self, color):
self.on_action(['set_bgcolor', color])

def clear(self):
self.on_action(['clear'])

def refreshed_turtle(self, drawn_turtle):
self.on_action([
'refreshed_turtle',
drawn_turtle.json_friendly if drawn_turtle is not None else None
])
self.drawn_turtle = drawn_turtle

def set_speed(self, speed):
self.on_action(['set_speed', speed])

def exit_on_click(self):
self.on_action(['exit_on_click'])

class LoggingCanvas(AbstractLoggingCanvas):
def __init__(self, width, height):
super().__init__(width, height)
self.log = []
def on_action(self, log_line):
self.log.append(log_line)
Loading

0 comments on commit df68f7d

Please sign in to comment.