-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.py
More file actions
333 lines (243 loc) · 9 KB
/
Copy pathGraph.py
File metadata and controls
333 lines (243 loc) · 9 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
from Calculator import Calculator
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt, QPointF, QLineF
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QFont
import sys
import PyQt5
from math import isinf
import logging
from functools import cache
if hasattr(QtCore.Qt, 'AA_EnableHighDpiScaling'):
PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
if hasattr(QtCore.Qt, 'AA_UseHighDpiPixmaps'):
PyQt5.QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
class Graph(QWidget):
def __init__(self, functions=[], zoom=1, z='0', scale_factor=0.1):
super().__init__()
self.setWindowTitle('Graph')
self.z = '0'
self.minor_grid = True
self.zoom = self.__get_zoom(zoom)
self.scaleFactor = scale_factor
self.opacities = dict()
if type(functions) is str:
self.functions = [functions]
else:
self.functions = functions
for function in self.functions:
self.opacities[function] = 250
self.setGeometry(500, 100, 500, 500)
self.setWindowTitle("Graph")
def __get_zoom(self, zoom):
if zoom < 0:
self.minor_grid = True
return 50
elif zoom == 0:
self.minor_grid = True
return 50
elif zoom == 1:
self.minor_grid = True
return 25
elif zoom == 2:
self.minor_grid = True
return 10
elif zoom == 3:
self.minor_grid = False
return 5
elif zoom == 4:
self.minor_grid = False
return 1
elif zoom > 4:
self.minor_grid = False
return 1
def change_zoom(self, zoom):
self.zoom = self.__get_zoom(zoom)
self.update()
def paintEvent(self, event):
qp = QPainter()
qp.begin(self)
self.draw_axes(qp)
self.draw_grid(qp)
qp.scale(self.scaleFactor, self.scaleFactor)
rgb_values = [[255, 0, 0], [0, 0, 250], [0, 250, 0], [0, 100, 100], [255, 0, 255]]
colors = dict()
for i, function in enumerate(self.functions):
colors[function] = QColor(*rgb_values[i], self.opacities[function])
for function in self.functions:
self.draw_function(qp, function, colors[function], self.z)
# Resets the scale
qp.scale(1 / self.scaleFactor, 1 / self.scaleFactor)
self.draw_axes_labels(qp, self.zoom)
qp.end()
@cache
def draw_axes(self, qp):
horizontal_spacing: int
vertical_spacing: int
axes = QColor(0, 0, 0, 150)
axes_pen = QPen(axes, 2, Qt.SolidLine)
qp.setPen(axes_pen)
qp.drawLine(0, 250, 500, 250)
qp.drawLine(250, 0, 250, 500)
def draw_axes_labels(self, qp, zoom):
# if the class instance zoom is not greater than 25, set it to 10
# this makes sure the labels don't get too clumped together when the graph is very zoomed out
if zoom < 25:
zoom = 10
axes = QColor(0, 0, 0, 150)
axes_pen = QPen(axes, 2, Qt.SolidLine)
qp.setPen(axes_pen)
font = QFont()
font.setBold(True)
font.setFixedPitch(True)
font.setPointSize(self.__get_point_size())
qp.setFont(font)
# draws the zero label for both the x-axis and y-axis
qp.drawText(242, 260, '0')
# Draws the x-coordinate labels every major line
for x in range(-250 + zoom * 5, 249, zoom * 5):
x_label = int(x / self.zoom)
horizontal_spacing = self.__get_horizontal_spacing(x_label)
vertical_spacing = 5 - self.__get_point_size()
x_label = str(x_label)
if not x == 0:
qp.drawText(x + 252 - horizontal_spacing, 260 - vertical_spacing, x_label)
# Draws the y-coordinate labels every major line
for y in range(-250 + zoom * 5, 250, zoom * 5):
y_label = int(-y / self.zoom)
horizontal_spacing = self.__get_horizontal_spacing(y_label)
y_label = str(y_label)
if y != 0:
qp.drawText(245 - horizontal_spacing, y + 253, y_label)
def __get_point_size(self):
match self.zoom:
case 50:
return 8
case 25:
return 7
case 10:
return 6
case 5:
return 5
case 1:
return 5
@cache
def __get_horizontal_spacing(self, coordinate):
spacing: int
length = len(str(coordinate))
spacing = self.__get_point_size() * 0.65 * length
return int(spacing)
def draw_grid(self, qp):
if self.zoom >= 25:
zoom = self.zoom
else:
zoom = 10
major_grid = QColor(0, 0, 0, 50)
minor_grid = QColor(0, 0, 0, 20)
major_grid_pen = QPen(major_grid, 1, Qt.SolidLine)
minor_grid_pen = QPen(minor_grid, 1, Qt.SolidLine)
qp.setPen(major_grid_pen)
# Draws a major horizontal lines every few minor lines
for x in range(0, 501, zoom * 5):
qp.drawLine(x, 0, x, 500)
# Draws a major horizontal lines every few minor lines
for y in range(0, 501, zoom * 5):
qp.drawLine(0, y, 500, y)
# Only draws the minor lines if the zoom level is below 2
if self.minor_grid:
qp.setPen(minor_grid_pen)
# Draws minor horizontal lines in increments of the zoom instance variable
for x in range(0, 501, self.zoom):
qp.drawLine(x, 0, x, 500)
# Draws minor horizontal lines in increments of the zoom instance variable
for y in range(0, 501, self.zoom):
qp.drawLine(0, y, 500, y)
def draw_function(self, qp, function, color, z):
if self.opacities[function] != 0:
pen = QPen(color, 7, Qt.SolidLine, Qt.RoundCap)
pen.setJoinStyle(Qt.RoundJoin)
pen.setCosmetic(True)
qp.setPen(pen)
qp.setRenderHint(QPainter.SmoothPixmapTransform, True)
qp.setRenderHint(QPainter.Antialiasing, True)
lines, points = self.get_lines(function, self.zoom, z)
qp.drawPoints(points)
qp.drawLines(lines)
@cache
def get_lines(self, function: str, zoom: int, z: str):
if 'z' in function:
function = function.replace('z', z)
continuous = False
point1: QPointF
point2: QPointF
points: list = []
lines: list = []
line: QLineF
calculator = Calculator()
get_point = self.get_point(function)
start = int(-250 / self.scaleFactor)
end = int(250 / self.scaleFactor)
for x in range(start, end):
try:
point2 = get_point(x)
if not continuous:
points.append(point2)
elif continuous:
line = QLineF(point1, point2)
lines.append(line)
point1 = point2
continuous = True
except ValueError:
continuous = False
return (lines, points)
def get_point(self, function):
calculator = Calculator()
def get_point_inner(x):
y: float
# adjusts x for the scale and zoom of the graph
x = round(x * self.scaleFactor
/ self.zoom, 1)
# Calculates y-value
y = float(
calculator.PEMDAS
(function.replace('x', ' ( ' + str(x) + ' ) ')))
logging.info(f'x: {x} \n y: {y}')
# checks if y is a complex number or infinite
self.check_complex(y)
#self.check_infinity(y)
# map y on to the graph
y = y / self.scaleFactor \
* -1\
* self.zoom \
+ 250 / self.scaleFactor
# map x onto the graph
x = (x / self.scaleFactor
* self.zoom
+ int(250 / self.scaleFactor))
point = QPointF(x, y)
return point
return get_point_inner
def change_opacity(self, function, opacity_level):
self.opacities[function] = opacity_level
self.update()
def change_z_axis(self, value):
self.z = value
self.scaleFactor = 1
self.update()
def check_complex(self, y_float):
if 'j' in str(y_float):
# 'e' in str(y_float) or 'j' in str(y_float):
print('imaginary: ' + y_float)
raise ValueError
def check_infinity(self, y_float):
if isinf(y_float):
print('infinity')
raise ValueError
if __name__ == '__main__':
#logging.basicConfig(level=logging.INFO)
app = QApplication(sys.argv)
functions = ['x + 245']
graph = Graph(functions, 4)
graph.show()
sys.exit(app.exec_())