I have a class called rectangle with two parametres (SX and SZ). I need to do a function that will devide me a first rectangle in two parts with a line in a random place on the wider dimension, then these two new rectangles will be divided in two, then four etc. I use a simple tree structure and a function like this:
def devide(self):
if (self.SX>self.SZ): #check which dimension is bigger
temp=randint (int(self.SX/4), int(self.SX*3/4)) #generate random from x/4,3x/4
left=rectangle(temp, self.SZ) #create nodes
right=rectangle(self.SX-temp, self.SZ)
self.addChild(left)
self.addChild(right)
else:
temp=randint (int(self.SZ/4), int(self.SZ*3/4))
up=rectangle(self.SX, temp)
down=rectangle(self.SX, self.SZ-temp)
self.addChild(up)
self.addChild(down)
for c in self.getChilds():
while (c.level()<3): ####why doesn't it work?:(
c.devide()
And the function level()— it should (but it doesn't) return a value of how many levels are between the root and the actual node:
def level(self):
root=self
a=0
while root.isRoot()==False:
a+=1
root=root.getParent()
return a
Important things for me are:
how to limit those divisions (for example, to have only 1+2+4+8 nodes)? I mean, there are no static variables in Python and my function
level()doesn't work properly.how to get the access to the youngest children (for example, to have them in array)
I am new in programming, especially in Python. Could somebody help me, please?
dividefunction via a keyword like so:def divide( rectangle, Max_levels=5 )then each iteration in the recursive call do it such that ` Max_level - 1` and stop refusing oncemax_levelreaches 0.