Created
September 1, 2016 20:18
-
-
Save Krewn/0cfaa0876cac172801cbff74dc302588 to your computer and use it in GitHub Desktop.
This script draws some triangles.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# . .____ | |
# -:/ .\ | |
# :/ ..\ | |
# ( ## ##.) | |
# :\ | ./ kpie314@gmail.com | |
# -:\ __./ Kevin Nelson | |
# --:\ ./ (c)2016 | |
# .:. ++ | |
# Doodle Utilities. | |
def degreesToRadians(degrees): | |
degrees=float(degrees) | |
return((degrees/180.)*np.pi) | |
def radiansToDegrees(radians): | |
radians=float(radians) | |
return((radians/np.pi)*180.) | |
def vectorByMatrix(vector,matrix): | |
if(len(vector)!=len(matrix)): | |
print "Matricies do Not Multiply" | |
raise Exception('Bad Matrix or Vector', 'Woops') | |
ret = [0.]*len(matrix) | |
for n,k in enumerate(vector): | |
for n2,k2 in enumerate(matrix[0]): | |
ret[n] += vector[n2]*matrix[n2][n] | |
return(ret) | |
def difference(a,b): | |
return([k-k2 for k,k2 in zip(a,b)]) | |
def vectorSum(a,b): | |
return([k+k2 for k,k2 in zip(a,b)]) | |
import numpy as np | |
def rotate(point, about, degrees): | |
tempVector = difference(point,about) | |
angle = degreesToRadians(degrees) | |
rotationMatrix = [[np.cos(angle),-1.*np.sin(angle)],[np.sin(angle),np.cos(angle)]] | |
return(vectorSum(vectorByMatrix(tempVector,rotationMatrix),about)) | |
def testBank1(): | |
print "degreesToRadians:" | |
print "\t0.5*pi == " + str(degreesToRadians(90)) | |
print "\t"+str(0.5*np.pi == degreesToRadians(90)) | |
print "radiansToDegrees:" | |
print "\t90.0 == " + str(radiansToDegrees(0.5*np.pi)) | |
print "\t"+str(90. == radiansToDegrees(0.5*np.pi)) | |
print "vectorByMatrix:" | |
print "\t[11.,17.] == " + str(vectorByMatrix([1.,2.],[[3.,5.],[4.,6.]])) | |
print "\t"+str([11.,17.] == vectorByMatrix([1.,2.],[[3.,5.],[4.,6.]])) | |
print "rotate:" | |
print "\t[0.,-1.] == " + str(rotate([1.,0.],[0.,0.],90.)) | |
print "\t"+str(sum(difference([0.,-1.],rotate([1.,0.],[0.,0.],90.)))<0.000000001) | |
#testBank1() # passed | |
import PIL | |
from PIL import Image, ImageDraw | |
myCanvas = Image.new("RGB", (500,500),"white") | |
myPen = ImageDraw.Draw(myCanvas) | |
def inflate(point,about,by): | |
tempVector = difference(point,about) | |
scaled = [k*by for k in tempVector] | |
return([j+k for j,k in zip(scaled,about)]) | |
class triangle: | |
def __init__(self, location = (100,100), length = 100): | |
f = [location[0],location[1]+length*np.sin(np.pi/3.)] | |
s = [location[0]+length/2,location[1]] | |
t = [location[0]+length,location[1]+length*np.sin(np.pi/3.)] | |
self.points=[f,s,t] | |
self.color = [0,0,0] | |
def center(self): | |
ret = [0.]*len(self.points[0]) | |
for k in self.points: | |
for n,k2 in enumerate(k): | |
ret[n]+=k2 | |
l = float(len(self.points)) | |
return([k/l for k in ret]) | |
def scale(self,factor): | |
center = self.center() | |
for n,k in enumerate(self.points): | |
self.points[n] = inflate(self.points[n],center,factor) | |
def rotate(self, degrees): | |
center = self.center() | |
for n,k in enumerate(self.points): | |
self.points[n] = rotate(k, center, degrees) | |
def copy(self): | |
ret = triangle() | |
ret.points = self.points | |
return(ret) | |
def translate(self,x,y): | |
for n,k in enumerate(self.points): | |
self.points[n] = [self.points[n][0]+x,self.points[n][1]+y] | |
def pointsTUPLE(self): | |
return tuple([tuple(k) for k in self.points]) | |
def colorTUPLE(self): | |
return tuple(self.color) | |
class canvas: | |
def __init__(self,width,height): | |
self.canvas = Image.new("RGB", (width,height),"white") | |
self.pen = ImageDraw.Draw(self.canvas) | |
self.width = width | |
self.height = height | |
def clear(self, color = (60,150,60)): | |
self.pen.polygon(((0,0),(self.width,0),(self.width,self.height),(0,self.height)), fill = color) | |
def draw(self,obj):# Drawable Interface has .pointsTUPLE() and .colorTUPLE() | |
self.pen.polygon(obj.pointsTUPLE(),obj.colorTUPLE()) | |
img = canvas(500,800) | |
img.clear() | |
tri = triangle(location = [100,100], length = 250) | |
img.draw(tri) | |
#img.canvas.show() | |
def step(obj,steps,steped=0): | |
obj.rotate(180) | |
obj.color = [254-int((254-k)/1.2) for k in obj.color] | |
length = ((obj.points[0][0]-obj.points[1][0])**2+(obj.points[0][1]-obj.points[1][1])**2)**0.5 | |
displacement = [length/2,length/2] | |
displacement = rotate(displacement, [0,0], -1.*60.*steped) | |
obj.translate(displacement[0],displacement[1]) | |
obj.scale(0.715) | |
img.draw(obj) | |
steped+=1 | |
if steped >= steps: | |
img.canvas.show() | |
else: | |
step(obj,steps,steped) | |
step(tri,11) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment