-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_plot_obj.py
More file actions
68 lines (53 loc) · 1.96 KB
/
test_plot_obj.py
File metadata and controls
68 lines (53 loc) · 1.96 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
#!/usr/bin/env python3
""" Exercises plot_obj.py module, demonstrating its use within a program.
First draws some mathematical curves and then allows the user to place circles
of radiating arrows of random colors and sizes when the clicks the mouse over
the window
"""
from plot_obj import Plotter
from math import sin
def quad(x):
""" Quadratic function (parabola) """
return 1/2 * x ** 2 + 3
def arrow_wheel(plotter, x, y, len, angle, color):
"""
Draws a collection of arrows extending radially from an (<x>, <y>) center
point.
The arrows all are <len> long, the <angle> parameter specifies the angle
between each adjacent arrow, and <color> specifies their color.
"""
from math import cos, sin, radians
COS_theta = cos(radians(angle))
SIN_theta = sin(radians(angle))
plotter.setcolor(color)
xe, ye = len, 0.0
for i in range(360 // angle):
xe, ye = xe * COS_theta - ye * SIN_theta, \
xe * SIN_theta + ye * COS_theta
plotter.draw_arrow(x, y, xe + x, ye + y)
def main():
""" Provides a simple test of the plotting object """
from math import sin
def run_test(x, y):
"""
Generate an arrow wheel centered at (<x>, <y>) width
random size and color
"""
# test_arrow(plt)
from random import randrange
colors = ['red', 'green', 'blue', 'black']
arrow_wheel(plt, x, y, randrange(10) + 1, 10, colors[randrange(4)])
# Create a plotter object
plt = Plotter(600, 600, -10, 10, -10, 10)
# Plot f(x) = 1/2 * x + 3, for -10 <= x < 100
plt.plot_function(quad, 'red')
# Plot f(x) = x, for -10 <= x < 100
plt.plot_function(lambda x: x, 'blue')
# Plot f(x) = 3 sin x, for -10 <= x < 100
plt.plot_function(lambda x: 3 * sin(x), 'green')
# Excecute the run_test function when the user clicks the mouse
plt.onclick(run_test)
# test_arrow(plt)
plt.interact()
if __name__ == '__main__':
main()