Skip to content

Instantly share code, notes, and snippets.

@techieji
Last active October 5, 2021 22:50
Show Gist options
  • Save techieji/a52a96c9fd4cf2d6d712cb43308bc27e to your computer and use it in GitHub Desktop.
Save techieji/a52a96c9fd4cf2d6d712cb43308bc27e to your computer and use it in GitHub Desktop.
A 3d drawer
from turtle import Turtle
from sys import exit
class Point:
scale = 25
def __init__(self, x, y, z):
self.x, self.y, self.z, self._x, self._y, self._z = x, y, z, x*Point.scale, y*Point.scale, z*Point.scale
class Drawer(Turtle):
def go_home(self):
self.pu()
self.home()
self.pd()
def go_to_point(self, p):
self.home()
self.seth(180) # x direction
self.fd(p._x)
self.seth(45) # y direction
self.fd(p._y)
self.seth(90) # z direction
self.fd(p._z)
def draw_line(self, p1, p2):
self.pu()
self.go_to_point(p2)
position = self.pos()
self.go_to_point(p1)
self.pd()
self.goto(position)
def draw_shape(self, *l):
for _x in range(len(l)):
x = _x - 1
self.draw_line(l[x], l[x + 1])
def get_fn(t, i):
if i == 'pu':
return t.pu
elif i == 'pd':
return t.pd
elif i == 'gp':
return t.go_to_point
elif i == 'dl':
return t.draw_line
elif i == 'ds':
return t.draw_shape
elif i == 'ex':
return exit
elif i == 'cl':
return t.reset
else:
return (lambda *a: print('?'))
def run(t, i):
if len(i) == 2:
get_fn(t, i[:2])()
else:
get_fn(t, i[:2])(*[Point(*[float(y.strip()) for y in x.split(',')]) for x in i[2:].split(';')])
def run_lines(s):
t = Drawer()
t.speed(0) # Speed adjustment
for x in s.split('\n'):
if x.strip():
run(t, x.strip())
t.go_home()
def shell():
t = Drawer()
while True:
i = input(' > ')
run(t, i)
# shell()
def prob21():
run_lines("""
ds0,0,0;3,0,0;3,0,3
ds0,2,0;3,2,0;3,2,3
dl3,0,0;3,2,0
ds0,0,0;0,1,3;0,2,0
ds3,0,3;0,1,3;3,2,3
""")
def prob23():
run_lines("""
ds0,0,0;3,0,0;3,0,2;2,0,2;2,0,1;0,0,1
ds0,2,0;3,2,0;3,2,2;0,2,2
dl0,0,0;0,2,0
dl3,0,0;3,2,0
dl3,0,2;3,2,2
dl0,0,1;0,2,2
dl2,0,1;0,2,2
dl2,0,2;0,2,2
""")
prob23()
@techieji
Copy link
Author

This is in draft form

@techieji
Copy link
Author

Added some complex shapes.

@techieji
Copy link
Author

techieji commented Oct 5, 2021

Now, it's minimized. For the big, actual version with the shell and everything, just look at the previous commit.

To run a sample model, just uncomment one of bottom two lines. These were given to me as an assignment in class to draw.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment