Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 21st, 2010 | Syntax: None | Size: 2.23 KB | Hits: 59 | Expires: Never
This paste has a previous version, view the difference. Copy text to clipboard
  1.     def ccw(self, p):
  2.         p1x,p1y,p2x,p2y,p3x,p3y = p ##basic math
  3.         return (p2x - p1x)*(p3y - p1y) - (p2y - p1y)*(p3x - p1x)
  4.  
  5.     def Evaluate(self,event):
  6.         blist = []## our list of points
  7.         l= ly = lx = 0 ## our lowest x and y value and it's index in the list
  8.         for x in range(self.Image.Width): ## loop through image
  9.             for y in range(self.Image.Height):
  10.                 if self.Image.GetRed(x,y) == 0: ## if there's no red(should be black as there's only black and white)
  11.                     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
  12.                     blist.append([x,y])## add it to the list
  13.  
  14.         for a in range(blist.count([lx,ly])): ## remove our lowest point
  15.             blist.remove([lx,ly])
  16.         N = len(blist) + 2 ## the lenght of our list when we can start the algorithm
  17.         i = 0 ## current index
  18.         for x,y in blist:
  19.             ## a.b = |a||b|cos alpha, basic math
  20.  
  21.             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]
  22.             i = i + 1 ## our next index
  23.         blist.sort() ## will sort or list by angle (first element of each element)
  24.         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.....
  25.         blist.insert(0,blist[len(blist)-1])## our first element should be the same as our last
  26.         blist = map(lambda x: x[1:],blist)## after sorting we only need our coordinates, so discard the angle element of each element
  27.         M = 2 ## M has to be two
  28.         for i in range(3, N):## loop through the list
  29.             while self.ccw(blist[M-1] + blist[M] + blist[i]) <= 0:##while the three points are counterclockwise
  30.                 M = M - 1
  31.             M += 1
  32.             t = blist[i]## swam M and i
  33.             blist[i] = blist[M]
  34.             blist[M] = t
  35.         blist = blist[:M]## we only need the list untill element M
  36.         blist.append(blist[0])## to draw it we need our last and first element to be the same
  37.         self.DrawHere.AddPoly(blist)
  38.         self.DrawHere.UpdateDrawing()