Advertisement
Guest User

Untitled

a guest
Apr 26th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.95 KB | None | 0 0
  1. import pyglet
  2. import pyglet.gl as pgl
  3. from pyglet.window import key
  4. import math
  5.  
  6.  
  7. WINDOW = 1000
  8. n = 1000
  9. m = 1000
  10.  
  11.  
  12. win = pyglet.window.Window(n, m, resizable=True)
  13.  
  14.  
  15. class myStack:
  16. def __init__(self):
  17. self.container = []
  18.  
  19. def isEmpty(self):
  20. return self.size() == 0
  21.  
  22. def push(self, item):
  23. self.container.append(item)
  24.  
  25. def pop(self):
  26. return self.container.pop()
  27.  
  28. def peek(self):
  29. if self.isEmpty():
  30. raise Exception("Stack empty!")
  31. return self.container[len(self.container) - 1]
  32.  
  33. def size(self):
  34. return len(self.container)
  35.  
  36. def show(self):
  37. return self.container
  38.  
  39.  
  40. width = win.width
  41. height = win.height
  42.  
  43.  
  44. kw = width/n
  45. kh = height/m
  46.  
  47. array = [0 for _ in range(0, n * m * 3)]
  48. arr = [[[0] * 3 for j in range(m)] for i in range(n)]
  49. mas = [[0] * 2 for i in range(100)]
  50.  
  51. finish = False
  52. border = False
  53. fill = False
  54. filter = False
  55. count = 0
  56. Xstart =0
  57. Ystart =0
  58.  
  59.  
  60. def bres(x1, y1, x2, y2):
  61. y = y1
  62. x = x1
  63. if (y2 > y1):
  64. stepY = 1
  65. else:
  66. stepY = -1
  67. if (x2 > x1):
  68. stepX = 1
  69. else:
  70. stepX = -1
  71. dx = math.fabs(x2 - x1)
  72. dy = math.fabs(y2 - y1)
  73. if(dy > dx):
  74. de = 2*dx
  75. e = -dy
  76. while y != y2:
  77. arr[x][y][0] = 255
  78. arr[x][y][1] = 255
  79. array[int(y * kh) * n * 3 + int(x * kw) * 3 + 0] = 255
  80. array[int(y * kh) * n * 3 + int(x * kw) * 3 + 1] = 255
  81. array[int(y * kh) * n * 3 + int(x * kw) * 3 + 2] = 255
  82. e += de
  83. if e >= 0:
  84. e -= 2*dy
  85. x += stepX
  86. y +=stepY
  87. else:
  88. de = 2 * dy
  89. e = -dx
  90. while x != x2:
  91. arr[x][y][0] = 255
  92. arr[x][y][1] = 255
  93. array[int(y * kh) * n * 3 + int(x * kw) * 3 + 0] = 255
  94. array[int(y * kh) * n * 3 + int(x * kw) * 3 + 1] = 255
  95. array[int(y * kh) * n * 3 + int(x * kw) * 3 + 2] = 255
  96. e += de
  97. if e >= 0:
  98. e -= 2 * dx
  99. y += stepY
  100. x += stepX
  101.  
  102.  
  103. def paint(x0, y0):
  104. S = myStack()
  105. S.push([x0,y0])
  106. while not S.isEmpty():
  107. t = S.pop()
  108. x = t[0]
  109. y = t[1]
  110. if arr[x][y][0] == 0:
  111. arr[x][y][0] = 254
  112. arr[x][y][1] = 255
  113. array[int(y * kh) * n * 3 + int(x * kw) * 3 + 0] = 255
  114. array[int(y * kh) * n * 3 + int(x * kw) * 3 + 1] = 255
  115. array[int(y * kh) * n * 3 + int(x * kw) * 3 + 2] = 255
  116. xLeft = x
  117. xRight = x
  118. while True:
  119. xLeft -=1
  120. if arr[xLeft][y][0] == 0:
  121. arr[xLeft][y][0] = 254
  122. arr[xLeft][y][1] = 255
  123. array[int(y * kh) * n * 3 + int(xLeft * kw) * 3 + 0] = 255
  124. array[int(y * kh) * n * 3 + int(xLeft * kw) * 3 + 1] = 255
  125. array[int(y * kh) * n * 3 + int(xLeft * kw) * 3 + 2] = 255
  126. else:
  127. Xleft = xLeft + 1
  128. break
  129. while True:
  130. xRight +=1
  131. if arr[xRight][y][0] == 0:
  132. arr[xRight][y][0] = 254
  133. arr[xRight][y][1] = 255
  134. array[int(y * kh) * n * 3 + int(xRight * kw) * 3 + 0] = 255
  135. array[int(y * kh) * n * 3 + int(xRight * kw) * 3 + 1] = 255
  136. array[int(y * kh) * n * 3 + int(xRight * kw) * 3 + 2] = 255
  137. else:
  138. break
  139. for i in range (xLeft + 1, xRight):
  140. if arr[i][y+1][0] == 0 and (arr[i+1][y+1][0] == 255 or arr[i+1][y][0] == 255):
  141. S.push([i, y+1])
  142. if arr[i][y-1][0] == 0 and (arr[i+1][y-1][0] == 255 or arr[i+1][y][0] == 255):
  143. S.push([i, y-1])
  144.  
  145.  
  146.  
  147.  
  148. @win.event
  149. def on_draw():
  150.  
  151. win.clear()
  152. pgl.glClear(pgl.GL_COLOR_BUFFER_BIT | pgl.GL_DEPTH_BUFFER_BIT)
  153.  
  154. pgl.glLoadIdentity()
  155.  
  156. pgl.glMatrixMode(pgl.GL_MODELVIEW)
  157.  
  158. if not finish:
  159. pgl.glBegin(pgl.GL_LINE_STRIP)
  160. pgl.glColor3ub(255, 255, 255)
  161. for i in range (0, count):
  162. pgl.glVertex2f(mas[i][0] * kw, mas[i][1] * kh)
  163. pgl.glEnd()
  164. elif finish and not border:
  165. pgl.glBegin(pgl.GL_LINE_LOOP)
  166. pgl.glColor3ub(255, 255, 255)
  167. for i in range(0, count):
  168. pgl.glVertex2f(mas[i][0] * kw, mas[i][1] * kh)
  169. pgl.glEnd()
  170. else:
  171. pgl.glDrawPixels(n, m, pgl.GL_RGB, pgl.GL_UNSIGNED_BYTE, (pgl.GLubyte * len(array))(*array))
  172.  
  173.  
  174.  
  175. @win.event
  176. def on_key_press(symbol, modifiers):
  177. global finish, border, mas, count, filter, arr, n, m, array
  178. if symbol == key.S:
  179. finish = True
  180. elif symbol == key.ESCAPE:
  181. pyglet.app.exit()
  182. elif symbol == key.B:
  183. for i in range(0, count):
  184. bres(int(mas[i][0]), int(mas[i][1]), int(mas[i+1][0]), int(mas[i+1][1]))
  185. border = True
  186. elif symbol == key.F:
  187. filter = not filter
  188. if filter:
  189. for i in range (1, n-1):
  190. for j in range (1, m-1):
  191. k = int((arr[i][j][1] + arr[i+1][j][1] + arr[i+1][j+1][1] + arr[i][j+1][1] + arr[i-1][j+1][1] + arr[i-1][j][1] + arr[i-1][j-1][1] + arr[i][j-1][1] + arr[i+1][j-1][1])/9)
  192. arr[i][j][1] = k
  193. array[j * n * 3 + i * 3 + 0] = k
  194. array[j * n * 3 + i * 3 + 1] = k
  195. array[j * n * 3 + i * 3 + 2] = k
  196. elif symbol == key.C:
  197. finish = False
  198. border = False
  199. filter = False
  200. count = 0
  201. array = [0 for _ in range(0, n * m * 3)]
  202. for i in range(0, count+2):
  203. mas[i][0]=0
  204. mas[i][1]=0
  205. for i in range(0, n):
  206. for j in range(0,m):
  207. arr[i][j][0]=0
  208. arr[i][j][1]=0
  209.  
  210.  
  211. @win.event
  212. def on_mouse_press(x, y, button, modifiers):
  213. global count, arr, mas, finish, border, fill, Xstart, Ystart, kw, kh
  214. if button == pyglet.window.mouse.LEFT and not finish:
  215. print(x)
  216. print(y)
  217. array[int(y / kh)*n*3 + int(x/kw)*3 + 0] = 255
  218. array[int(y / kh)*n*3 + int(x/kw)*3 + 1] = 255
  219. array[int(y / kh)*n*3 + int(x/kw)*3 + 2] = 255
  220. arr[int(x / kw)][int(y / kh)][0] = 255
  221. mas[count][0] = x / kw
  222. mas[count][1] = y / kh
  223. mas[count + 1][0] = mas[0][0]
  224. mas[count + 1][1] = mas[0][1]
  225. count+=1
  226. if button == pyglet.window.mouse.RIGHT and border:
  227. fill = True
  228. Xstart =x / kw
  229. Ystart =y / kh
  230. paint(int(Xstart), int(Ystart))
  231.  
  232.  
  233. @win.event
  234. def on_resize(w, h):
  235. global width, height, kw, kh, n, m, array, border, Xstart, Ystart
  236. width = w
  237. height = h
  238. kw = width/n
  239. kh = height/m
  240. print('The window was resized to %dx%d' % (w, h))
  241.  
  242.  
  243. pyglet.app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement