Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pyglet
- import pyglet.gl as pgl
- from pyglet.window import key
- import math
- WINDOW = 1000
- n = 1000
- m = 1000
- win = pyglet.window.Window(n, m, resizable=True)
- class myStack:
- def __init__(self):
- self.container = []
- def isEmpty(self):
- return self.size() == 0
- def push(self, item):
- self.container.append(item)
- def pop(self):
- return self.container.pop()
- def peek(self):
- if self.isEmpty():
- raise Exception("Stack empty!")
- return self.container[len(self.container) - 1]
- def size(self):
- return len(self.container)
- def show(self):
- return self.container
- width = win.width
- height = win.height
- kw = width/n
- kh = height/m
- array = [0 for _ in range(0, n * m * 3)]
- arr = [[[0] * 3 for j in range(m)] for i in range(n)]
- mas = [[0] * 2 for i in range(100)]
- finish = False
- border = False
- fill = False
- filter = False
- count = 0
- Xstart =0
- Ystart =0
- def bres(x1, y1, x2, y2):
- y = y1
- x = x1
- if (y2 > y1):
- stepY = 1
- else:
- stepY = -1
- if (x2 > x1):
- stepX = 1
- else:
- stepX = -1
- dx = math.fabs(x2 - x1)
- dy = math.fabs(y2 - y1)
- if(dy > dx):
- de = 2*dx
- e = -dy
- while y != y2:
- arr[x][y][0] = 255
- arr[x][y][1] = 255
- array[int(y * kh) * n * 3 + int(x * kw) * 3 + 0] = 255
- array[int(y * kh) * n * 3 + int(x * kw) * 3 + 1] = 255
- array[int(y * kh) * n * 3 + int(x * kw) * 3 + 2] = 255
- e += de
- if e >= 0:
- e -= 2*dy
- x += stepX
- y +=stepY
- else:
- de = 2 * dy
- e = -dx
- while x != x2:
- arr[x][y][0] = 255
- arr[x][y][1] = 255
- array[int(y * kh) * n * 3 + int(x * kw) * 3 + 0] = 255
- array[int(y * kh) * n * 3 + int(x * kw) * 3 + 1] = 255
- array[int(y * kh) * n * 3 + int(x * kw) * 3 + 2] = 255
- e += de
- if e >= 0:
- e -= 2 * dx
- y += stepY
- x += stepX
- def paint(x0, y0):
- S = myStack()
- S.push([x0,y0])
- while not S.isEmpty():
- t = S.pop()
- x = t[0]
- y = t[1]
- if arr[x][y][0] == 0:
- arr[x][y][0] = 254
- arr[x][y][1] = 255
- array[int(y * kh) * n * 3 + int(x * kw) * 3 + 0] = 255
- array[int(y * kh) * n * 3 + int(x * kw) * 3 + 1] = 255
- array[int(y * kh) * n * 3 + int(x * kw) * 3 + 2] = 255
- xLeft = x
- xRight = x
- while True:
- xLeft -=1
- if arr[xLeft][y][0] == 0:
- arr[xLeft][y][0] = 254
- arr[xLeft][y][1] = 255
- array[int(y * kh) * n * 3 + int(xLeft * kw) * 3 + 0] = 255
- array[int(y * kh) * n * 3 + int(xLeft * kw) * 3 + 1] = 255
- array[int(y * kh) * n * 3 + int(xLeft * kw) * 3 + 2] = 255
- else:
- Xleft = xLeft + 1
- break
- while True:
- xRight +=1
- if arr[xRight][y][0] == 0:
- arr[xRight][y][0] = 254
- arr[xRight][y][1] = 255
- array[int(y * kh) * n * 3 + int(xRight * kw) * 3 + 0] = 255
- array[int(y * kh) * n * 3 + int(xRight * kw) * 3 + 1] = 255
- array[int(y * kh) * n * 3 + int(xRight * kw) * 3 + 2] = 255
- else:
- break
- for i in range (xLeft + 1, xRight):
- if arr[i][y+1][0] == 0 and (arr[i+1][y+1][0] == 255 or arr[i+1][y][0] == 255):
- S.push([i, y+1])
- if arr[i][y-1][0] == 0 and (arr[i+1][y-1][0] == 255 or arr[i+1][y][0] == 255):
- S.push([i, y-1])
- @win.event
- def on_draw():
- win.clear()
- pgl.glClear(pgl.GL_COLOR_BUFFER_BIT | pgl.GL_DEPTH_BUFFER_BIT)
- pgl.glLoadIdentity()
- pgl.glMatrixMode(pgl.GL_MODELVIEW)
- if not finish:
- pgl.glBegin(pgl.GL_LINE_STRIP)
- pgl.glColor3ub(255, 255, 255)
- for i in range (0, count):
- pgl.glVertex2f(mas[i][0] * kw, mas[i][1] * kh)
- pgl.glEnd()
- elif finish and not border:
- pgl.glBegin(pgl.GL_LINE_LOOP)
- pgl.glColor3ub(255, 255, 255)
- for i in range(0, count):
- pgl.glVertex2f(mas[i][0] * kw, mas[i][1] * kh)
- pgl.glEnd()
- else:
- pgl.glDrawPixels(n, m, pgl.GL_RGB, pgl.GL_UNSIGNED_BYTE, (pgl.GLubyte * len(array))(*array))
- @win.event
- def on_key_press(symbol, modifiers):
- global finish, border, mas, count, filter, arr, n, m, array
- if symbol == key.S:
- finish = True
- elif symbol == key.ESCAPE:
- pyglet.app.exit()
- elif symbol == key.B:
- for i in range(0, count):
- bres(int(mas[i][0]), int(mas[i][1]), int(mas[i+1][0]), int(mas[i+1][1]))
- border = True
- elif symbol == key.F:
- filter = not filter
- if filter:
- for i in range (1, n-1):
- for j in range (1, m-1):
- 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)
- arr[i][j][1] = k
- array[j * n * 3 + i * 3 + 0] = k
- array[j * n * 3 + i * 3 + 1] = k
- array[j * n * 3 + i * 3 + 2] = k
- elif symbol == key.C:
- finish = False
- border = False
- filter = False
- count = 0
- array = [0 for _ in range(0, n * m * 3)]
- for i in range(0, count+2):
- mas[i][0]=0
- mas[i][1]=0
- for i in range(0, n):
- for j in range(0,m):
- arr[i][j][0]=0
- arr[i][j][1]=0
- @win.event
- def on_mouse_press(x, y, button, modifiers):
- global count, arr, mas, finish, border, fill, Xstart, Ystart, kw, kh
- if button == pyglet.window.mouse.LEFT and not finish:
- print(x)
- print(y)
- array[int(y / kh)*n*3 + int(x/kw)*3 + 0] = 255
- array[int(y / kh)*n*3 + int(x/kw)*3 + 1] = 255
- array[int(y / kh)*n*3 + int(x/kw)*3 + 2] = 255
- arr[int(x / kw)][int(y / kh)][0] = 255
- mas[count][0] = x / kw
- mas[count][1] = y / kh
- mas[count + 1][0] = mas[0][0]
- mas[count + 1][1] = mas[0][1]
- count+=1
- if button == pyglet.window.mouse.RIGHT and border:
- fill = True
- Xstart =x / kw
- Ystart =y / kh
- paint(int(Xstart), int(Ystart))
- @win.event
- def on_resize(w, h):
- global width, height, kw, kh, n, m, array, border, Xstart, Ystart
- width = w
- height = h
- kw = width/n
- kh = height/m
- print('The window was resized to %dx%d' % (w, h))
- pyglet.app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement