Tuesday, May 19, 2009

Assignment 01B


import maya.cmds as cmds
import math
import maya.mel as mm



def getDistance(start, end):
#start and end must be lists xyz
v = [start[0]-end[0], start[1]-end[1], start[2]-end[2]] #list format x,y,z
vector = "<<" + str(v[0]) + "," + str(v[1]) + "," + str(v[2]) + ">>"
mag = mm.eval("mag " + vector + ";")

return mag


#########THIS IS THE NON_RECURSIVE PART ------ JUST SKIP#######################
'''
mySquare = cmds.nurbsSquare(sl1=10, sl2=15)

isSides = cmds.filterExpand (sm=9)

pos = []

for i in range (len(isSides)):
posa = cmds.pointPosition(isSides[i]+".cv[0]")
pos.append (posa)



diag = getDistance(pos[0],pos[2])
a = getDistance(pos[2],pos[3])
b = getDistance(pos[3],pos[0])

h = math.sqrt(a*a - ((a*diag)/(b+a)) * ((a*diag)/(b+a)))


mySquare2 = cmds.nurbsSquare(sl1=h, sl2=diag)
isSides2 = cmds.filterExpand (sm=9)
pos2 = []

for i in range (len(isSides2)):
posb = cmds.pointPosition(isSides2[i]+".cv[0]")
pos2.append (posb)

#get diference between actual location and move
dist = getDistance(pos[2],pos2[2])
p1 = pos[2]
p2 = pos2[2]
x=p2[0] - p1[0]
print x
y=p2[0] - p1[0]
cmds.move(x,-y,0,mySquare)



#get angle of rotation
sinF = getDistance(pos[0],pos[1])
alpha = math.asin(sinF/diag)
Ld= math.degrees (alpha)

cmds.rotate(0,0,Ld,mySquare2, pivot = pos2[2])
'''



#create initial rectangle
mySquare = cmds.nurbsSquare(sl1=a, sl2=b)


#define recursive function
def MyRecursive(iterations,a,b):

isSides = cmds.filterExpand (sm=9)

pos = []
for i in range (len(isSides)):
posa = cmds.pointPosition(isSides[i]+".cv[0]")
pos.append (posa)


#define length of diagonal in order to use it for next rectangle
diag = getDistance(pos[0],pos[2])
#define length of 2 sides to get height
a = getDistance(pos[2],pos[3])
b = getDistance(pos[3],pos[0])
#get height of triangle (diagonal and the two sides)
h = math.sqrt(a*a - ((a*diag)/(b+a)) * ((a*diag)/(b+a)))

#create new rectangle with l2=diagonal length and l1= height of triangle
mySquare2 = cmds.nurbsSquare(sl1=h, sl2=diag)
isSides2 = cmds.filterExpand (sm=9)

pos2 = []

for i in range (len(isSides2)):
posb = cmds.pointPosition(isSides2[i]+".cv[0]")
pos2.append (posb)

#get diference between actual location and move to future pivot point
dist = getDistance(pos[2],pos2[2])
p1 = pos[2]
p2 = pos2[2]
x=p2[0] - p1[0]
y=p2[0] - p1[0]
cmds.move(x,-y,0,mySquare)



#get angle of rotation from diagonal
sinF = getDistance(pos[0],pos[1])
alpha = math.asin(sinF/diag)
Ld= math.degrees (alpha)

#rotate on diagonal
cmds.rotate(0,0,Ld,mySquare2, pivot = pos2[2])

#recursion part
#if else conditions
if iterations == 0:
return "Done"
else:
iterations -= 1
MyRecursive(iterations, a,b)

#call function
MyRecursive(10,10,15)



The problems i have are first of all that the height of the triangle i am calculating is not corresponding to the actual height.

Second would be that the recursion is not working as it should.

What it should do is first create a rectangle, create another one(with length=diagonal, and width=height of the triangle formed by diagonal and two sides). Rotate the last created rectangle on the diagonal, so that one of the sides "becomes" the diagonal. And then repeat everything for the next one.

1 comment:

  1. Hi!
    Clearly something is going on wrong here. I mean, the whole things works and returns no errors, and lots of squares are created (look in the outliner...), but not the way you want, right? So something is wrong with the values you are getting via the mathematical calculations. You should print them out and analyze them closely. A diagram of this idea is also missing, and trust me, it helps to solve strange problems like this.

    So recheck the way you get your values (side lengths, diagonals, triangle heights), because you problem lies there. Otherwise it would not even create all the squares. The way you are calling the recursion is working fine...

    ReplyDelete