-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplotter.py
More file actions
272 lines (225 loc) · 7.84 KB
/
plotter.py
File metadata and controls
272 lines (225 loc) · 7.84 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
# headers
"""Function Graph Drawer
This application allows the user to print the graph(s) of chosen
functions to the pygame window in the XY coordinate system.
The app accepts user input through a list of checkboxes.
This program requires that `OpenGL.GL`, `OpenGL.GLU`, `numpy`,
`tkinter`, and `themedtkinter` be installed within the Python
environment you are running this code in.
This file contains the following functions:
* init - initializes a pygame window
* drawExp - draws exponential function
* drawLog - draws logarithmic function
* drawSin - draws sine function
* drawCos - draws cosine function
* drawTan - draws tangential function
* drawSquare - draws square function
* drawCart - draws the cartesian coordinate
* clear - clears the screen
* begin - begins OpenGL drawing
* end - ends OpenGL drawing
* warningMin - handles less than exception
* warningMax - handles more than exception
* main - the main function of the program
"""
__author__ = "Abdulkarim Getachew (Mania)"
__copyright__ = "Copyright 2022"
__date__ = "2022/04/23"
__maintainer__ = "Abdulkarim Getachew"
__email__ = "abdulkarimgmohammed@gmail.com"
__license__ = "GPLv3"
__status__ = "Production"
__version__ = "0.0.1"
# importing all the necessary modules
import pygame
from pygame.locals import *
from OpenGL.GL import * # imports all functions of OpenGL
from OpenGL.GLU import * # imports Graphics Library Utilities
import numpy as np
from tkinter import *
from tkinter.ttk import *
# global variables
# generate 100 points from -5 to 5 range
x = np.linspace(-5, 5, 500)
# Functions
def init():
pygame.init()
display = (500, 500)
pygame.display.set_caption('Graph Plotter')
# tells pygame that we will be displaying graphics
# made with OpenGL
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
glClearColor(0.0, 0.0, 0.0, 1.0) # RGBA = Solid BLACK
gluOrtho2D(-5.0, 5.0, -5.0, 5.0) # 2D x: -5 => 5 y: -5 => 5
def drawExp():
glColor3f(1.0, 0.0, 0.0) # body color = RED
y = np.exp(x)
begin()
# for every pair a,b of the numbers in x,y
for a, b in zip(x, y): # zip yields tuples
glVertex2f(a, b)
end()
def drawLog():
glColor3f(0.0, 1.0, 0.0) # body color = GREEN
# Override x
x = np.linspace(0, 5, 250)
y = np.log(x)
begin()
# for every pair a,b of the numbers in x,y
for a,b in zip(x, y):
glVertex2f(a, b)
end()
def drawSin():
glColor3f(0.0, 0.0, 1.0) # body color = BLUE
y = np.sin(x)
begin()
# for every pair a,b of the numbers in x,y
for a,b in zip(x, y):
glVertex2f(a, b)
end()
def drawCos():
glColor3f(0.0, 1.0, 1.0) # body color = CYAN
y = np.cos(x)
begin()
# for every pair a,b of the numbers in x,y
for a,b in zip(x, y):
glVertex2f(a, b)
end()
def drawTan():
glColor3f(1.0, 1.0, 0.0) # body color = YELLOW
y = np.tan(x)
begin()
# for every pair a,b of the numbers in x,y
for a,b in zip(x, y):
glVertex2f(a, b)
end()
def drawSquare():
glColor3f(1.0, 0.0, 1.0) # body color = PURPLE
y = np.power(x, 2)
begin()
# for every pair a,b of the numbers in x,y
for a,b in zip(x, y):
glVertex2f(a, b)
end()
def drawCart():
glColor3f(1.0, 1.0, 1.0) # body color = WHITE
glBegin(GL_LINES)
glVertex2f(5.0, 0.0)
glVertex2f(-5.0, 0.0)
end()
glBegin(GL_LINES)
glVertex2f(0.0, 5.0)
glVertex2f(0.0, -5.0)
end()
def clear():
"""clears the window with the colors in glClearColor."""
glClear(GL_COLOR_BUFFER_BIT)
def begin():
glBegin(GL_LINE_STRIP)
def end():
glEnd()
glFlush()
def warningMin():
warning = Tk()
warning.geometry('350x100')
warning.title('Warning')
Label(text = 'LESS THAN REQUIRED FUNCTIONS CHOSEN!\nPlease choose at least two functions to proceed').pack(fill = BOTH)
ok = Button(warning, text = 'OK', command = warning.destroy)
ok.pack(side = 'bottom', pady = 10)
warning.mainloop()
pygame.quit()
main() # main function call #2
def warningMax():
warning = Tk()
warning.geometry('400x150')
warning.title('Warning')
Label(text = 'MORE THAN REQUIRED FUNCTIONS CHOSEN!\nPlease choose no more than six functions to proceed\n').pack(fill = BOTH)
Label(text = "This may be because you have chosen 'Draw All functions'\nin combination with one or more of the remaining function(s)").pack(fill = BOTH)
ok = Button(warning, text = 'OK', command = warning.destroy)
ok.pack(side = 'bottom', pady = 5)
warning.mainloop()
pygame.quit()
main() # main function call #3
def main():
# create a tkinter window to accept user input
root = Tk()
root.geometry('320x320')
root.title('Function Graph Drawer')
# Displaying description text
label = Label(text = 'Select at least two of any functions \nyou want to draw from the list below:')
label.pack(side = 'top', fill = BOTH, padx = 5, pady = 5)
# Accepting user input using checkboxes
numDraw = 0
isExp = IntVar()
exp = Checkbutton(root, text = 'Exponential function (RED)', variable = isExp, onvalue = 1, offvalue = 0, )
exp.pack(side = 'top', fill = BOTH, padx = 5, pady = 5)
isLog = IntVar()
log = Checkbutton(root, text = 'Logarithmic function (GREEN)', variable = isLog, onvalue = 1, offvalue = 0, )
log.pack(side = 'top', fill = BOTH, padx = 5, pady = 5, )
isSin = IntVar()
sin = Checkbutton(root, text = 'Sine function (BLUE)', variable = isSin, onvalue = 1, offvalue = 0)
sin.pack(side = 'top', fill = BOTH, padx = 5, pady = 5)
isCos = IntVar()
cos = Checkbutton(root, text = 'Cosine function (CYAN)', variable = isCos, onvalue = 1, offvalue = 0, )
cos.pack(side = 'top', fill = BOTH, padx = 5, pady = 5)
isTan = IntVar()
tan = Checkbutton(root, text = 'Tangential function (YELLOW)', variable = isTan, onvalue = 1, offvalue = 0, )
tan.pack(side = 'top', fill = BOTH, padx = 5, pady = 5)
isSqr = IntVar()
sqr = Checkbutton(root, text = 'Square function (PURPLE)', variable = isSqr, onvalue = 1, offvalue = 0, )
sqr.pack(side = 'top', fill = BOTH, padx = 5, pady = 5)
isAll = IntVar()
all = Checkbutton(root, text = 'Draw All functions', variable = isAll, onvalue = 1, offvalue = 0)
all.pack(side = 'top', fill = BOTH, padx = 5, pady = 5)
# Submit button
submit = Button(root, text = 'Draw', command = root.destroy)
submit.pack(fill = Y, padx = 5, pady = 15, side = 'bottom')
# Making the app user friendly so that a user can
# force stop the application using the close[X] button
root.protocol('WM_DELETE_WINDOW', quit)
root.mainloop()
init() # init function call
while True:
# ensuring that the infinite loop is eventually broken
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
clear()
drawCart()
# Processing user input
numDraw = 0
if isAll.get() == 1:
drawCos()
drawExp()
drawLog()
drawSin()
drawTan()
drawSquare()
numDraw += 6
if isExp.get() == 1:
drawExp()
numDraw += 1
if isLog.get() == 1:
drawLog()
numDraw += 1
if isSin.get() == 1:
drawSin()
numDraw += 1
if isCos.get() == 1:
drawCos()
numDraw += 1
if isTan.get() == 1:
drawTan()
numDraw += 1
if isSqr.get() == 1:
drawSquare()
numDraw += 1
# Exceptions handling
if numDraw < 2:
warningMin()
elif numDraw > 6:
warningMax()
pygame.display.flip()
pygame.time.wait(10)
main() # main function call #1