This repository was archived by the owner on Feb 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinner_square.py
More file actions
125 lines (106 loc) · 3.9 KB
/
inner_square.py
File metadata and controls
125 lines (106 loc) · 3.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
import matplotlib as matplotlib
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider
from matplotlib import patches
class square_inside_square:
# declare variable b, c and area
#TODO: Check if this is the right way to define and initilaize instance variables
__b,__c,_area=[None, None, None]
# init method or constructor
def __init__(self, b, c):
self.__b = b
self.__c = c
self.calculate()
def get_b(self):
return self.__b
def set_b(self, b):
self.__b = b
self.calculate()
def get_c(self):
return self.__c
def set_c(self, c):
self.__c = c
self.calculate()
def calculate(self):
b=self.__b
c=self.__c
self._area = 0.0 if b == 0 else ((b*b)-((2*b*b*b*c)/(b*b+c*c)))
slider_b=None
slider_c=None
# Initial values for b and c
bnc = square_inside_square(15.0, 5.0)
def main():
fig,ax=plt.subplots()
fig.subplots_adjust(left=0.1, bottom=0.25)
# draw the square x and y axes with the value of b
def draw_axes(b):
ax.set_xlim([0,b])
ax.set_ylim([0,b])
ax.axes.set_aspect('equal')
draw_axes(bnc.get_b())
# draw 4 lines and mark(tick) the x and y axis
def draw_lines(b,c):
ax.axes.get_xaxis().set_ticks([c,b])
ax.axes.get_yaxis().set_ticks([b-c,b])
plt.axis([0, b, 0, b])
ax.plot([0,b], [0,c], color='black')
ax.plot([0,c], [b,0], color='black')
ax.plot([0,b], [b-c,b], color='black')
ax.plot([b-c,b], [b,0], color='black')
draw_lines(bnc.get_b(),bnc.get_c())
# draw slider b and c and set them to values b and c respectively
def draw_sliders(b,c):
global slider_b, slider_c
axis_color='lightgoldenrodyellow'
max_b=100.0
slider_b_ax = fig.add_axes([0.25, 0.15, 0.65, 0.03], facecolor=axis_color)
slider_b = Slider(slider_b_ax, 'b value', 0, max_b, valinit=b, valfmt='%0.1f')
slider_c_ax = fig.add_axes([0.25, 0.1, 0.65, 0.03], facecolor=axis_color)
slider_c = Slider(slider_c_ax, 'c value', 0, b, valinit=c, valfmt='%0.1f')
draw_sliders(bnc.get_b(),bnc.get_c())
# create a text for to show the current arre for a given b,c
axLabel = plt.axes([0.25, 0.02, 0.4, 0.05])
textbox = matplotlib.widgets.TextBox(axLabel, 'Area: ')
textbox.set_val(format("{:.1f}".format(bnc._area)))
# remove all the lines TODO: Is this the right way to remove lines?
def remove_lines():
num_of_lines=len(ax.lines)
while(num_of_lines>0):
ax.lines.pop(num_of_lines-1)
num_of_lines-=1
def sliders_b_changed(val):
global bnc
global slider_b, slider_c
# round b to increments of .1
bnc.set_b(round(val,1))
#bnc.calculate()
remove_lines()
# set max value of slider_c to the b value
slider_c.valmax=bnc.get_b()
slider_c.ax.set_xlim(slider_c.valmin, slider_c.valmax)
# TODO: handle scenario where the new b value is less then the current c value
if (bnc.get_c()>bnc.get_b()):
bnc.set_c(bnc.get_b()/2)
slider_c.val=bnc.get_c()
draw_axes(bnc.get_b())
draw_lines(bnc.get_b(),bnc.get_c())
textbox.set_val(format("{:.1f}".format(bnc._area)))
fig.canvas.draw_idle()
def sliders_c_changed(val):
global bnc
c = round(val,1)
bnc.set_c(c)
#bnc.calculate()
remove_lines()
draw_lines(bnc.get_b(),bnc.get_c())
textbox.set_val(format("{:.1f}".format(bnc._area)))
fig.canvas.draw_idle()
slider_b.on_changed(sliders_b_changed)
slider_c.on_changed(sliders_c_changed)
# Create a Rectangle patch
#rect = patches.Rectangle((0,0),b/2,b/2,linewidth=1,edgecolor='r',facecolor='blue')
# Add the patch to the Axes
#ax.add_patch(rect)
plt.show()
if __name__ == '__main__':
main()