Untitled
By: a guest | Mar 21st, 2010 | Syntax:
None | Size: 2.23 KB | Hits: 59 | Expires: Never
def ccw(self, p):
p1x,p1y,p2x,p2y,p3x,p3y = p ##basic math
return (p2x - p1x)*(p3y - p1y) - (p2y - p1y)*(p3x - p1x)
def Evaluate(self,event):
blist = []## our list of points
l= ly = lx = 0 ## our lowest x and y value and it's index in the list
for x in range(self.Image.Width): ## loop through image
for y in range(self.Image.Height):
if self.Image.GetRed(x,y) == 0: ## if there's no red(should be black as there's only black and white)
if y > ly: lx, ly, l = x, y, len(blist)-1 ## if this y is lower(lower location on screen means higher value) then our current ly, make this our new ly
blist.append([x,y])## add it to the list
for a in range(blist.count([lx,ly])): ## remove our lowest point
blist.remove([lx,ly])
N = len(blist) + 2 ## the lenght of our list when we can start the algorithm
i = 0 ## current index
for x,y in blist:
## a.b = |a||b|cos alpha, basic math
blist[i] = [acos( (lx-x)/sqrt((lx-x)**2 + (ly-y)**2)),x,y]## the format of each element is [angle with lowest x and y, x, y]
i = i + 1 ## our next index
blist.sort() ## will sort or list by angle (first element of each element)
blist.insert(0,[0,lx,ly]) ## insert our lowest x and y, would've thrown an devide by zero if we would calculate the angle.....
blist.insert(0,blist[len(blist)-1])## our first element should be the same as our last
blist = map(lambda x: x[1:],blist)## after sorting we only need our coordinates, so discard the angle element of each element
M = 2 ## M has to be two
for i in range(3, N):## loop through the list
while self.ccw(blist[M-1] + blist[M] + blist[i]) <= 0:##while the three points are counterclockwise
M = M - 1
M += 1
t = blist[i]## swam M and i
blist[i] = blist[M]
blist[M] = t
blist = blist[:M]## we only need the list untill element M
blist.append(blist[0])## to draw it we need our last and first element to be the same
self.DrawHere.AddPoly(blist)
self.DrawHere.UpdateDrawing()