Dilation

Dilation (scaling) of a quadrilateral by 2x.
Dilation (scaling) of a quadrilateral by 2x.

A quick program that animates scaling (dilation) of shapes by scaling the coordinates. You type in the dilation factor.

dilation.py

from visual import *

#axes
xmin = -10.
xmax = 10.
ymin = -10.
ymax = 10.
xaxis = curve(pos=[(xmin,0),(xmax,0)])
yaxis = curve(pos=[(0,ymin),(0,ymax)])

#tick marks
tic_dx = 1.0
tic_h = .5
for i in arange(xmin,xmax+tic_dx,tic_dx):
    tic = curve(pos=[(i,-0.5*tic_h),(i,0.5*tic_h)])
for i in arange(ymin,ymax+tic_dx,tic_dx):
    tic = curve(pos=[(-0.5*tic_h,i),(0.5*tic_h,i)])

#stop scene from zooming out too far when the curve is drawn
scene.autoscale = False

# define curve here
shape = curve(pos=[(-1,2), (5,3), (4,-1), (-1,-1)])

shape.append(pos=shape.pos[0])
shape.color = color.yellow
shape.radius = 0.1
shape.visible = True

#dilated shape
dshape = curve(color=color.green, radius=shape.radius*0.9)
for i in shape.pos:
    dshape.append(pos=i)

#label
note = label(pos=(5,-8),text="Dilation: 1.0", box=False)
intext = label(pos=(5,-9),text="> x", box=False)

#scaling lines
l_scaling = False
slines = []
for i in range(len(shape.pos)):
    slines.append(curve(radius=shape.radius*.5,color=color.red, pos=[shape.pos[i],shape.pos[i],shape.pos[i]]))


#animation parameters
animation_time = 1. #seconds
animation_smootheness = 30
animation_rate = animation_smootheness / animation_time

x = ""
while 1:
    #x = raw_input("Enter Dilation: ")
    if scene.kb.keys: # event waiting to be processed?
        s = scene.kb.getkey() # get keyboard info

        #print s
        if s <> '\n':
            x += s
            intext.text = "> x "+x
        else:
            try:
                xfloat = float(x)
                note.text = "Dilation: " + x

                endpoints = []
                dp = []
                for i in shape.pos:
                    endpoints.append(float(x) * i)
                    dp.append((endpoints[-1]-i)/animation_smootheness)
                #print "endpoints: ", endpoints
                #print "dp:        ", dp
                for i in range(animation_smootheness):
                    for j in range(len(dshape.pos)):
                        dshape.pos[j] = i*dp[j]+shape.pos[j]
                    rate(animation_smootheness)
                    if slines:
                        for i in range(len(shape.pos)):
                            slines[i].pos[1] = vector(0,0)
                            slines[i].pos[-1] = dshape.pos[i]

                for i in range(len(shape.pos)):
                    dshape.pos[i] = endpoints[i]
                    slines[i].pos[-1] = dshape.pos[i]

                for i in range(len(shape.pos)-1):
                    print shape.pos[i], "--->", dshape.pos[i]
                    
            except:
                #print "FAIL"
                failed = True
                intext.text = "> x "
            x = ""
            
                   

Leave a Reply