-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
153 lines (126 loc) · 5.23 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
from PyQt5.QtGui import QImage, QPixmap, qRgb, QIcon, QFont
from PyQt5.QtCore import Qt
from ctypes import *
import sys
import os
# Carrega a biblioteca em C++ levando em conta o SO
if sys.platform == "win32":
lib_file = "\\shared.dll"
else:
lib_file = "/shared.so"
path = os.getcwd() + lib_file
shared = CDLL(path)
#
# Define os tipos dos argumentos e retornos das funções da biblioteca, para conversão automática
shared.createArray.argtypes = [POINTER((POINTER(c_int))), c_int]
shared.createArray.restype = None
shared.calcArrayMandelbrot.argtypes = [POINTER(c_int), c_double, c_double, c_int, c_double]
shared.calcArrayMandelbrot.restype = None
shared.release.argtypes = [POINTER(c_int)]
shared.release.restype = None
#
# Definindo uma tela principal extendendo a classe padrão
class TelaPrincipal(QMainWindow):
def __init__(self):
super().__init__()
# Definição dos elementos básicos da tela
x_pos, y_pos, width, height = 300, 300, 600, 600 # manter width = height
self.setGeometry(x_pos, y_pos, width, height + 100)
self.setWindowTitle("Fractal de Mandelbrot")
self.setWindowIcon(QIcon("icon.png"))
#
# Condição inicial do fractal
self.limite_superior, self.limite_inferior = 2, -2
self.offset_x, self.offset_y = 0, 0
#
# Criação do elemento de imagem(vazio)
self.imageDisplay = QLabel(self)
self.imageDisplay.setGeometry(0, 0, width, height)
self.img = QImage(width, height, QImage.Format_RGB32)
#
# Criação e definição das labels da legenda
self.label_1 = QLabel("W: mover para cima | A: mover para esquerda | D: mover para direta | S: mover para baixo", self)
self.label_2 = QLabel("Z: zoom-in | X: zoom-out | C: zoom-in grande | V: zoom-out grande | R: reset",self)
self.label_1.setFont(QFont('Arial', 10))
self.label_2.setFont(QFont('Arial', 10))
self.label_1.setGeometry(0, height + 10, width, 40)
self.label_2.setGeometry(0, height + 50, width, 40)
self.label_1.setAlignment(Qt.AlignCenter)
self.label_2.setAlignment(Qt.AlignCenter)
#
# Criação e alocação do array que é compartilhado entre C++ e Python
self.arrayMandelbrot = POINTER(c_int)()
shared.createArray(self.arrayMandelbrot, width * height)
#
# Função que coloca pixel por pixel da imagem
def draw(self):
largura_total = self.limite_superior - self.limite_inferior
base_x = self.limite_inferior + self.offset_x
base_y = self.limite_superior + self.offset_y
size = self.img.height() # largura === altura
# Atualiza o array compartilhado com os parâmetros atuais
shared.calcArrayMandelbrot(self.arrayMandelbrot, base_x, base_y, size, largura_total)
# Percore o array e coloca o valor em RGB na matriz de pixeis
for x in range(size):
for y in range(size):
val = self.arrayMandelbrot[x * size + y]
self.img.setPixel(x, y, qRgb(val, val, val))
# Passa essa matriz para o elemento de display de imagem
pixmap = QPixmap.fromImage(self.img)
self.imageDisplay.setPixmap(pixmap)
# Trata os comandos do teclado (calcula novo parâmetro e redesenha)
def keyPressEvent(self, e):
# Mover para cima
if e.key() == Qt.Key_W:
self.offset_y += self.limite_superior * 0.1
self.draw()
# Mover para esquerda
elif e.key() == Qt.Key_A:
self.offset_x -= self.limite_superior * 0.1
self.draw()
# Mover para baixo
elif e.key() == Qt.Key_S:
self.offset_y -= self.limite_superior * 0.1
self.draw()
# Mover para direita
elif e.key() == Qt.Key_D:
self.offset_x += self.limite_superior * 0.1
self.draw()
# Zoom-in
elif e.key() == Qt.Key_Z:
self.limite_inferior -= self.limite_inferior * 0.05
self.limite_superior -= self.limite_superior * 0.05
self.draw()
# Zoom-out
elif e.key() == Qt.Key_X:
self.limite_inferior += self.limite_inferior * 0.05
self.limite_superior += self.limite_superior * 0.05
self.draw()
# Zoom-in grande
elif e.key() == Qt.Key_C:
self.limite_inferior -= self.limite_inferior * 0.8
self.limite_superior -= self.limite_superior * 0.8
self.draw()
# Zoom-out grande
elif e.key() == Qt.Key_V:
self.limite_inferior += self.limite_inferior * 0.8
self.limite_superior += self.limite_superior * 0.8
self.draw()
# Reset
elif e.key() == Qt.Key_R:
self.offset_x, self.offset_y = 0, 0
self.limite_superior, self.limite_inferior = 2, -2
self.draw()
# Ao fechar a janela, desalocar array compartilhado
def closeEvent(self, event):
shared.release(self.arrayMandelbrot)
event.accept()
# Criação da tela
def main():
app = QApplication(sys.argv)
win = TelaPrincipal()
win.draw()
win.show()
sys.exit(app.exec_())
main()