One Byte Wealth

Thursday, November 24, 2005

Spiral Polygons

In last assignent we used hard coded three values for generating triangle.Here I extended the same script and the number of vertices kept variable values.





from Tkinter import *
import math

ee = 0.08
OuterDia = 250
Cpx = 200
Cpy = 150
vertices = 8
angle = 2*math.pi/vertices

# this stores point x,y values

points=[]

def point(x,y,i):
p = 5
return(x-p,y-p,x+p,y+p)

for i in range(0,vertices):
X = math.cos(angle*i)*OuterDia/2 + Cpx
Y = math.sin(angle*i)*OuterDia/2 + Cpy
pair = [X,Y]
points.append(pair)

root = Tk()
root.title("Trigno fun")
canvas = Canvas(width=400,height=300,bg='black')
fromx = points[vertices-1][0]
fromy = points[vertices-1][1]
for pt in points:
canvas.create_line(pt[0],pt[1], \
fromx,fromy,fill='white')
fromx = pt[0]
fromy = pt[1]


for i in range(0,100):
lastx = ee*points[vertices-2][0] \
+ (1-ee)*points[vertices-1][0]
lasty = ee*points[vertices-2][1] \
+ (1-ee)*points[vertices-1][1]
if(i%4==0):
color='yellow'
if(i%4==1):
color='green'
if(i%4==2):
color='red'
if(i%4==3):
color='blue'
canvas.create_line(lastx,lasty,points[0][0],\
points[0][1],fill=color)
for i in range(vertices-1,0,-1):
points[i][0]=points[i-1][0]
points[i][1]=points[i-1][1]
points[0][0]=lastx
points[0][1]=lasty

canvas.pack()
mainloop()

Labels: ,

Tuesday, November 22, 2005

Spiral Triangles

while surfing through web, one day I came across a nice web page titled ideas.I picked up the ideas over there to implement in the pythonic way and rewrite all code in python.
Todays one is to draw a simple spiral triangles.The basic idea is to start with a triangle, interpolate to find a point a fraction of the way down a side, draw a line to this point, and use that point as the new corner for the triangle. This will make a picture that looks like this:








Complete source code will be as following :



from Tkinter import *
import math

## constant for slope
## and fixed 3 points

ee = 0.04
x1=y1=y2=0
x2=10
x3=5
y3=5*math.sqrt(3)

PrevX = 0
PrevY = 150

## Scales the Line

def scale(x,y,xx,yy):
x=x*20+100
y=y*20+100
xx=xx*20+100
yy=yy*20+100
return(x,y,xx,yy)

root = Tk()
root.title("Trigno fun 1")
canvas = Canvas(width=400,height=300,bg='white')
canvas.create_line(scale(x1,y1,x2,y2),fill='red')
canvas.create_line(scale(x2,y2,x3,y3),fill='red')
canvas.create_line(scale(x3,y3,x1,y1),fill='red')

## positions shifted/swapped till 100th point
## using 3 fixed points

for i in range(0,100):
x4 = ee*x2 + (1-ee)*x3
y4 = ee*y2 + (1-ee)*y3
canvas.create_line(scale(x4,y4,x1,y1),fill='red')
x3=x2
y3=y2
x2=x1
y2=y1
x1=x4
y1=y4

canvas.pack()
mainloop()


I made few changes in color combination and turn it into following one :



Labels: ,