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: ,

Wednesday, October 26, 2005

Regular Polygon

When all edges of polygon are having same lengths and meeting at same interior angle, that is being known as regular polygon. Radius and number of vertices(edges) are the known factors based on which the polygon is constrcuted.

First interior angle is calculated by:
angle = 2*math.pi/vertices

Now allocate each point by formula

>>> for i in range(0,vertices):
. . . X = math.cos(angle*i)*radius
. . . Y = math.sin(angle*i)*radius

Following image shows the simple polygon drawn on my screen



By adding code for GUI and lines between the allocated points of polygon, final program will be as following,

from Tkinter import *
import math

# Known Polygon Params
# Outer dia, Center point and vertices
OuterDia = 200
Cpx = 200
Cpy = 150
vertices = 6

# First point
fromX = Cpx + OuterDia/2
fromY = Cpy

# Calculate angle between edges
angle = 2*math.pi/vertices

root = Tk()
root.title("Polygon")
canvas = Canvas(width=400,height=300,background =‘white‘)

# Core logic goes here
# I made changes to draw lines
# rather than finding points

for i in range(0,vertices+1):
toX = math.cos(angle*i)*OuterDia/2 + Cpx
toY = math.sin(angle*i)*OuterDia/2 + Cpy
canvas.create_line(toX,toY,fromX,fromY,fill=‘brown‘)
fromX = toX
fromY = toY
canvas.pack()
mainloop()

Labels: ,

Sunday, October 23, 2005

Simple Sine

Implementing sine function, is the very primary and simple assignment.Here I used Tkinter interface for GUI, cause it takes very few lines to demonstrate/show the requirements.I used math library and small lines are drawn to form curve.




Complete code will be like this,




from Tkinter import *
import math
PrevX = 0
PrevY = 150


## Created a small segment of 1.0 length

def point(x,y):
return(x-0.5,y-0.5,x+0.5,y+0.5)

## Create Canvas Window and draw base line markups

root = Tk()
root.title("SinWave")
canvas = Canvas(width=400,height=300,bg='white')
canvas.create_line(0,150,400,150,fill='red')
canvas.create_text(10,160,text='0')
canvas.create_text(100,160,text='90')
canvas.create_text(200,160,text='180')
canvas.create_text(300,160,text='270')
canvas.create_text(30,290,text='SinWave')


## Implement core logic

for i in range(0,1000):
x = ((i*400)/100)
y = -(math.sin(math.pi*2*i/100)*100)+152
canvas.create_line(PrevX,PrevY,x,y,fill='blue')
PrevX=x
PrevY=y

canvas.pack()
mainloop()



following is just more interesting ..





Here is another for loop with some changes, X multiplication factor is decreased and combined result is two sine waves, code will be something like this,




for i in range(0,1000):
x = (i*400)/100
y = (math.sin(math.pi*2*i/100)*100)+148
canvas.create_line(PrevX,PrevY,x,y,fill='green')
PrevX=x
PrevY=y

PrevX = 0
PrevY = 150
for i in range(0,1000):
x = (i*200)/100
y = (math.sin(math.pi*2*i/100)*50)+148
canvas.create_line(PrevX,PrevY,x,y,fill='green')
PrevX=x
PrevY=y

Labels: ,

Wednesday, July 27, 2005

GUI - The Pythonic Way !

There is great impact of GUI-programming-RAD tools on programmers, specially on students and newbies that they couldn't accept the language as Programming language until-n-unless there is provision of GUI programming.(Definitely credit goes to great RAD products which make people to forget difference between RAD tool and programming Language :) )

Whenever I talked on Python in front of professionals, students or newbies, everywhere they asked me about RAD for Python as well as another question was weather it is too easy as Delphi, kylix or VB... and I answered 'yes it is !' and now its time to see 'how it is ?'

Labels: , , , , ,