Advertisement
trishLEX

fill function

Apr 19th, 2017
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. def fill(n): #n - count of points
  2.     global smooth
  3.     global height
  4.     global width
  5.     clear()
  6.  
  7.     print("FILL")
  8.  
  9.     color = [1.0, 1.0, 1.0]
  10.     global points
  11.    
  12.     #step1 - outline rasterization
  13.  
  14.     for i in range(n):
  15.         k = (i + n - 1) % n
  16.         j = (i + 1) % n
  17.         x1 = points[i][0]
  18.         y1 = points[i][1]
  19.         x2 = points[j][0]
  20.         y2 = points[j][1]
  21.  
  22.         ex = (y1 >= y2) ^ (y1 <= points[k][1])
  23.  
  24.         dx = abs(x2 - x1)
  25.         dy = abs(y2 - y1)
  26.  
  27.         sx = sign(x2 - x1)
  28.         sy = sign(y2 - y1)
  29.  
  30.         er = dx - dy
  31.  
  32.         back = [0.0, 0.0, 0.0]
  33.         tooglePixel(x2, y2, color, back)
  34.  
  35.         if not ex:
  36.             tooglePixel(x1, y1, color, back)
  37.  
  38.         while x1 != x2 or y1 != y2:
  39.             er2 = er * 2
  40.             if er2 > -dy:
  41.                 er -= dy
  42.                 x1 += sx
  43.             if er2 < dx:
  44.                 er += dx
  45.                 y1 += sy
  46.                 tooglePixel(x1, y1, color, back)
  47.  
  48.     #step2 - filling shape
  49.  
  50.     for j in range(height):
  51.         flag = False
  52.         for i in range(width):
  53.             if getPixel(i, j) == color:
  54.                 flag = not flag
  55.             if flag:
  56.                 setPixel(i, j, color)
  57.    
  58.     #step3 - drawing outline
  59.  
  60.     drawShape(smooth)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement