-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraph_plotter.py
More file actions
440 lines (364 loc) · 13.1 KB
/
Graph_plotter.py
File metadata and controls
440 lines (364 loc) · 13.1 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import time, math, os, pickle
import turtle
class trigo:
@staticmethod
def trigo_main(q):
#gives colour in rgb mode with scale 255
turtle.colormode(255)
turtle.pencolor((197, 83, 253))
turtle.width(1.4)
turtle.penup()
turtle.goto(-250,0)
turtle.pendown()
# i have multiplied by 10 in x and y coordinate so that graph looks bigger and readable and shifted actual axis by 250 pixles to make whole graph visible
for i in range (50):
if q=="cos":
turtle.goto((i*10-250),10*math.cos(i))
if q=="sin":
turtle.goto((i*10-250),10*math.sin(i))
turtle.penup()
b = raw_input("Enter where you want to shift graph\n\t1.Nowhere\n\t2.Right\n\t3.Left\n")
trigo.trigo_shift(b,q)
@staticmethod
def trigo_axis_make():
turtle.goto(0,0)
turtle.pendown()
turtle.pencolor("black")
#making X coordinates
turtle.goto(-300,0)
turtle.goto(300,0)
turtle.goto(0,0)
#making Y coordinates
turtle.goto(0,20)
turtle.goto(0,-20)
turtle.goto(0,0)
turtle.penup()
@staticmethod
def trigo_mark_axis():
turtle.pencolor("blue")
turtle.width(1)
#marking Y coordinates
#marks 1 on yaxis
turtle.penup()
turtle.goto(-5,10)
turtle.pendown()
turtle.write("1",font=("Arial",6,"normal"))
#marks -1 on yaxis
turtle.penup()
turtle.goto(-7,-20)
turtle.pendown()
turtle.write("-1",font=("Arial",6,"normal"))
#marking x coordinate
turtle.colormode(255)
#let us obtain color in rgb color codes which has code range of each color of 255
turtle.pencolor((103, 150, 8))
turtle.width(1)
for i in range (16):
if i==8:
# to write for 0 and other as it will print 0pi then it is i=8 bcoz i have shifted graph toward left by 250 pixles to make whole graph come in window and 8*pi*10 will give value around 251
turtle.penup()
turtle.goto(2,-10)
turtle.pendown()
turtle.write("0",font=("Arial",6,"bold"))
else:
#marks a kink in graph
turtle.penup()
turtle.goto((math.pi*i*10)-(250),+4)
turtle.pendown()
turtle.goto((math.pi*i*10)-(250),-4)
turtle.penup()
#writes the value
turtle.goto((math.pi*i*10)-(250),-20)
turtle.pendown()
turtle.write((str(i-8)+("pi")) ,font=("Arial",6,"bold"))
@staticmethod
def trigo_shift(b,q):
turtle.colormode(255)
turtle.pencolor((240, 169, 108))
turtle.width(1)
c = int(input("Enter the value till you want to shift if you want to shift\nright\nleft\nnowhere"))
d = c*10
if b.lower() in ["right"]:
#to get graph lying on same axis
for i in range (-60,60):
if -251<(i*10-250+d)<251:
if q=="cos":
turtle.goto((i*10-250+d),10*math.cos(i))
if q=="sin":
turtle.goto((i*10-250+d),10*math.sin(i))
turtle.pendown()
elif b.lower() in ["left"]:
for i in range (-60,60):
if -251<(i*10-250-d)<251:
if q=="cos":
turtle.goto((i*10-250-d),10*math.cos(i))
if q=="sin":
turtle.goto((i*10-250-d),10*math.sin(i))
turtle.pendown()
elif b.lower() in ["nowhere", "no"]:
turtle.penup()
turtle.goto(0,0)
else:
turtle.exitonclick()
exit()
class circle:
turtle.colormode(255)
turtle.pencolor((240, 169, 108))
turtle.width(3.2)
@staticmethod
def drawCircle (centerpoint, radius):
(x,y) = centerpoint
turtle.up()
turtle.setpos(x,y)
turtle.down()
#r*10 so that circle looks bigger
turtle.circle(radius*10)
class cycloid:
@staticmethod
def cycloid():
turtle.penup()
turtle.goto(-300,0)
turtle.pendown()
turtle.colormode(255)
turtle.pencolor((240, 169, 108))
turtle.width(3.2)
turtle.speed(1000)
for i in range (400):
#this is equation of cycloid
turtle.goto(15*(i/10-math.sin(i/10))-300,15*(1-math.cos(i/10)))
class regular_polygon:
@staticmethod
def polygon(a):
if a>2:
turtle.pendown()
for i in range(a+1):
turtle.forward(50)
# the angle of each vertice of a regular polygon is 360 divided by the number of sides
# turtle.left rotates turtle at its position by dgrees given in argument
turtle.left(360/a)
else:
print ("invalid input")
exit()
class hypocycloid:
@staticmethod
def hypo(k):
r=50
#k is no. of cusps.........Cusp- A sharp point on a curve.........If k is a rational number, say k = p/q expressed in simplest terms, then the curve has p cusps.
#If k is an irrational number, then the curve never closes, and fills the space between the larger circle and a circle of radius R ? 2r
turtle.colormode(255)
turtle.pencolor((74, 185, 240))
turtle.width(7)
#have taken radius divided by 10 as in circle class have multiplied incoming radius into 10 to make circle bigger
circle.drawCircle((0,-r*k),r*k/10)
turtle.penup()
turtle.goto(r*k,0)
turtle.pendown()
turtle.speed(120)
turtle.colormode(255)
turtle.pencolor((75, 201, 142))
turtle.width(.8)
for i in range(0,1000):
turtle.goto(r*(k-1)*math.cos(i)+r*math.cos((k-1)*i),r*(k-1)*math.sin(i)-r*math.sin((k-1)*i))
turtle.penup()
turtle.goto(-300,-r*k-30)
turtle.pendown()
turtle.pencolor("black")
turtle.width(2)
turtle.write("Quick Fact: \nfor k=2 the curve is a straight line and the circles are called Cardano circles.\nThey are used in high-speed printing",font=("Arial",8,"normal"))
class epicycloid:
@staticmethod
def epicycloid(k):
r=50
turtle.colormode(255)
turtle.pencolor((74, 185, 240))
turtle.width(7)
circle.drawCircle((0,-r*k),r*k/10)
turtle.penup()
#as at i=0--in equation of epicycloid,turtle is at (r*k,0)
turtle.goto(r*k,0)
turtle.pendown()
turtle.speed(120)
turtle.colormode(255)
turtle.pencolor((75, 201, 142))
turtle.width(.8)
for i in range(0,1000):
turtle.goto(r*(k+1)*math.cos(i)-r*math.cos((k+1)*i),r*(k+1)*math.sin(i)-r*math.sin((k+1)*i))
class polynomial:
@staticmethod
def new_polynomial():
s=[]
a = input("Degree of polynomial\n")
for i in range (a+1):
b = input("Enter coefficient of power "+ str(i)+ "\t")
s+=[b]
return s
@staticmethod
def poly_make_axis():
turtle.colormode(255)
#let us obtain color in rgb color codes which has code range of each color of 255
turtle.pencolor((103, 150, 8))
turtle.width(1)
#x axis
turtle.goto(-300,0)
turtle.goto(300,0)
turtle.goto(0,0)
#y axis
turtle.pendown()
turtle.goto(0,250)
turtle.goto(0,-250)
turtle.penup()
turtle.goto(0,0)
@staticmethod
def poly_main(s):
turtle.colormode(255)
turtle.pencolor((240, 169, 108))
turtle.width(1.75)
for j in range(-40,40):
p=0
#s(list)is of form {2,1,2,-3}.......meaning coefficient of x^0 is 2,x^1 is 1,x^2 is 2,x^3 is -3
for i in range(len(s)):
p+=s[i]*(j**i)
#here p becomes the polynomial... -3(i^3)+2(i^2)+1(i^1)+2(i^0)
#in which i is varied to make graph
#to contain graph in window
if -290<p<290:
turtle.goto(j*10,p)
turtle.pendown()
if p==0:
#if by chance, a root comes integer in -40 to 40...it is marked
turtle.penup()
turtle.goto(j*10,0)
turtle.pendown()
#make kink
turtle.goto(j*10,-4)
turtle.goto(j*10,+4)
turtle.penup()
#write the integer root....try drawing (x-2)^3={-8,12,-6,1}..(values to be put according to order when program is run
turtle.goto(j*10,-15)
turtle.pendown()
turtle.write(j)
turtle.penup()
turtle.goto(j*10,0)
turtle.pendown()
#and graph is continued from this pt again
def new():
y=raw_input("What do you want to draw -\n1.Cos\n2.Sin\n3.Circle\n4.Regular polygon\n5.Polynomial\n6.Cycloid\n7.Hypocycloid\n8.Epicycloid\n")
if y.lower() in ["1", "cos"]:
trigo.trigo_axis_make()
trigo.trigo_mark_axis()
trigo.trigo_main("cos")
var = 0
if y.lower() in ["2", "sin"]:
trigo.trigo_axis_make()
trigo.trigo_mark_axis()
trigo.trigo_main("sin")
var = 0
if y.lower() in ["3", "circle"]:
a = input("Enter x coordinate of circle\t")
b = input("Enter y coordinate of circle\t")
r = input("Enter radius\t")
circle.drawCircle((a,b),r)
var = (a,b,r)
if y.lower() in ["4", "regular polygon"]:
a = input("Enter number of sides of polygon\t")
regular_polygon.polygon(a)
var = a
if y.lower() in ["5", "polynomial"]:
a = polynomial.new_polynomial()
polynomial.poly_make_axis()
polynomial.poly_main(a)
var = a
if y.lower() in ["6", "cycloid"]:
cycloid.cycloid()
var = 0
if y.lower() in ["7", "hypocycloid"]:
k = int(input("Enter no. of cusps\t"))
#to make cardano line--k=2
#to make deltoid---k=3
#to make asteroid--k=4
#to make star---k=5
hypocycloid.hypo(k)
var = k
if y.lower() in ["8", "epicycloid"]:
k = int(input("Enter no. of cusps\t"))
#for best result---k=2 or 3 or 5
epicycloid.epicycloid(k)
var = k
return (y,var)
def prev(y, var):
if y.lower() in ["1", "cos"]:
trigo.trigo_axis_make()
trigo.trigo_mark_axis()
trigo.trigo_main("cos")
if y.lower() in ["2", "sin"]:
trigo.trigo_axis_make()
trigo.trigo_mark_axis()
trigo.trigo_main("sin")
if y.lower() in ["3", "circle"]:
(a,b,r) = var
circle.drawCircle((a,b),r)
if y.lower() in ["4", "regular polygon"]:
a = var
regular_polygon.polygon(a)
if y.lower() in ["5", "polynomial"]:
a = var
polynomial.poly_make_axis()
polynomial.poly_main(a)
if y.lower() in ["6", "cycloid"]:
cycloid.cycloid()
if y.lower() in ["7", "hypocycloid"]:
k = var
hypocycloid.hypo(k)
if y.lower() in ["8", "epicycloid"]:
k = var
epicycloid.epicycloid(k)
while True:
# Asking to continue with the program
v = raw_input("Do you want to plot new graph or from last function plotted -\n1.New Graph\n2.Previous Graph\n3.Exit\n")
turtle.clear()
turtle.penup()
turtle.goto(0,0)
turtle.pendown()
# Allowing multiple possible inputs to be accepted
if v.lower() in ["new graph", "new", "1"]:
# y,var are the variables with the information regarding the graph needed to reproduce it
(y,var) = new()
# Checking if Graphs folder already there
if not os.path.exists(".\\Graphs"):
# Making said folder if not found
os.mkdir(".\\Graphs")
# Using a generator function to get a list of files in Graphs folder
(root,dir,files) = os.walk(".\\Graphs").next()
# Calculating the name of the new file
filenum = len(files)
# Opening a new file with .graph extension which is self created but can still be opened in binary mode
graph = file(".\\Graphs\\"+str(filenum+1)+".graph", "wb")
# Filling the file with required data
pickle.dump((y,var), graph)
# Remember to close to make sure everything functions smoothly!
graph.close()
# Again allowing multitudes of possible inputs
elif v.lower() in ["2", "previous", "previous graph"]:
# Getting the filelist
(root, dir, files) = os.walk(".\\Graphs").next()
# Sorting so as to be able to determine the last file
files.sort()
# Opening the file
previous = file(".\\Graphs\\" + files[-1], "rb")
# Getting the critical variables required to make graph
(y,var) = pickle.load(previous)
# Closing to ensure smooth functioning
previous.close()
# Deleting opened graph
os.remove(".\\Graphs\\"+files[-1])
# Sending acquired graph information for processing
prev(y, var)
elif v.lower() in ["3", "exit"]:
turtle.exitonclick()
exit()
else:
print ("Make an appropriate choice")
print ("The program now closes")
time.sleep(1)
exit()
turtle.exitonclick()