-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshapes_mvc.py
270 lines (219 loc) · 7.02 KB
/
shapes_mvc.py
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
#!/usr/bin/env python
import os
import numpy as np
# import matplotlib as mpl
from matplotlib import rc
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
from abc import ABC
rc("font", **{"family": "serif", "serif": ["Computer Modern Roman"]})
rc("text", usetex=True)
# Illustration of Model-View-Controller (MVC) and (eventually) RTTI attribution.
# =========
class Model(ABC):
# =========
def __init__(self):
# origin
self._X0, self._Y0 = 0, 0
def origin(self):
return self._X0, self._Y0
@property
def points(self):
return [self._X0, self._Y0]
@points.setter
def points(self, value):
self._X0 = value[0]
self._Y0 = value[1]
class AxisModel(Model):
def __init__(self, direction=1):
super().__init__()
# direction=1 is the x-axis
# direction=2 is the y-axis
if direction == 1:
self._X = np.array([0, 1])
self._Y = np.array([0, 0])
else: # direction is y-axis
self._X = np.array([0, 0])
self._Y = np.array([0, 1])
@property
def points(self): # override
return [self._X, self._Y]
@points.setter # override
def points(self, value):
self._X0 = value[0][0] # update the origin x
self._Y0 = value[1][0] # update the origin y
self._X = value[0]
self._Y = value[1]
class BodyModel(Model):
def __init__(self, radius=1):
super().__init__()
self._NPOINTS = 25
theta = np.linspace(0.0, 2 * np.pi, self._NPOINTS) # radians
self._X = np.array(radius * np.cos(theta))
self._Y = np.array(radius * np.sin(theta))
def number_of_points(self):
return self._NPOINTS
def perimeter(self):
return self._X, self._Y
@property
def points(self): # override
where = 0
X = np.insert(self._X, where, self._X0, axis=0)
Y = np.insert(self._Y, where, self._Y0, axis=0)
return [X, Y]
@points.setter # override
def points(self, value):
self._X0 = value[0][0] # update the origin x
self._Y0 = value[1][0] # update the origin y
self._X = value[0][1:]
self._Y = value[1][1:]
# ========
class View(ABC):
# ========
def __init__(self):
self._color = "red"
@property
def color(self):
return self._color
@color.setter
def color(self, value):
self._color = value
class AxisView(View):
def __init__(self, model, axis, color="blue"):
super().__init__()
self._color = color
self._color_origin = "black"
x, y = model.points
axis.plot(
x, y, "-", marker="o", color=self._color, markevery=[-1]
) # plot the x-axis or y-axis, depending on model
x0, y0 = model.origin()
axis.plot(x0, y0, "o", color=self._color_origin)
class BodyView(View):
def __init__(self, model, axis, color="magenta"):
super().__init__()
self._color = color
self._fs = "none" # fillstyle
self._linealpha = 0.5
x0, y0 = model.origin()
x, y = model.perimeter()
axis.plot(x, y, "o-", color=self._color, fillstyle=self._fs) # boundary
axis.plot(
[x0, x[0]],
[y0, y[0]],
"o-",
color="red",
fillstyle=self._fs,
markevery=[-1],
) # tracking line on original x-axis
y_axis_index = int((model.number_of_points() - 1) / 4)
axis.plot(
[x0, x[y_axis_index]],
[y0, y[y_axis_index]],
"o-",
color="green",
fillstyle=self._fs,
markevery=[-1],
) # tracking line on original y-axis
axis.plot(x0, y0, "o", color="black", fillstyle=self._fs) # body origin
# ===========
# Controllers
# ===========
def offset(points, offsets=[0, 0]):
"""Given a list of reference points [X, Y], offset them in
the x-axis and the y-axis.
"""
# x = X + offset_x
# y = Y + offset_y
# return x, y
x = points[0] + offsets[0]
y = points[1] + offsets[1]
return [x, y]
def simple_shear(points, shear_12=0):
"""Given a list of reference points [X, Y], simple shear them in
the x-axis by distance shear_x (Length) to the current points [x, y].
"""
# x = X + shear_12 * Y
# y = Y
# return x, y
x = points[0] + shear_12 * points[1]
y = points[1]
return [x, y]
def rotate(points, angle=0):
"""Given list of reference points [X, Y], rotate them about the
z-axis by angle R (radians) to the current points [x, y].
"""
# x = np.cos(R) * X - np.sin(R) * Y
# y = np.sin(R) * X + np.cos(R) * Y
X = points[0]
Y = points[1]
x = np.cos(angle) * X - np.sin(angle) * Y
y = np.sin(angle) * X + np.cos(angle) * Y
return [x, y]
def stretch(points, stretches=[1, 1]):
"""Given a list of reference points [X, Y], simple stretch j
by factor stretches[0] in the x-direction and
by factor stretches[1] in the y-direction
to the current points [x, y].
"""
x = stretches[0] * points[0]
y = stretches[1] * points[1]
return [x, y]
# ======
# client
# ======
fig = plt.figure(figsize=(6, 6)) # inches, (wide, tall)
ax = fig.add_subplot(1, 1, 1)
RADTODEG = 180.0 / np.pi
DEGTORAD = 1.0 / RADTODEG
xaxis = AxisModel(direction=1) # create
p = xaxis.points # read
o = [-5, -4] # offset
xaxis.points = offset(p, o) # update
gb = AxisView(xaxis, ax, "red") # view
yaxis = AxisModel(direction=2) # create
p = yaxis.points # read
yaxis.points = offset(p, o) # update
gb = AxisView(yaxis, ax, "green") # view
body = BodyModel() # create
gb = BodyView(body, ax, "dimgray") # view
body = BodyModel() # create
r = 30 * DEGTORAD # radians
body.points = rotate(body.points, r) # read then update
o = [4, 3] # offset
body.points = offset(body.points, o) # read then update
gb = BodyView(body, ax, "blue") # view
body = BodyModel() # create
s = 1 # shear
body.points = simple_shear(body.points, s) # read then update
o = [-3, 2] # offset
body.points = offset(body.points, o) # read then update
gb = BodyView(body, ax, "magenta") # view
body = BodyModel() # create
s = [1.5, 2] # stretches
body.points = stretch(body.points, s) # read then update
o = [3, -4] # offset
body.points = offset(body.points, o) # read then update
gb = BodyView(body, ax, "orange") # view
ax.axis("equal")
# major axes
ax.xaxis.set_major_locator(MultipleLocator(1.0))
ax.yaxis.set_major_locator(MultipleLocator(1.0))
ax.grid(b=True, which="major", linestyle=":")
# minor axes
# no operations
ax.set_xlabel(r"reference configuration $X_1, x_1$")
ax.set_ylabel(r"reference configuration $X_2, x_2$")
a = 8
b = a
ax.set_xlim(-a, a)
ax.set_ylim(-b, b)
# ax.legend(loc='lower right', framealpha=1.0)
# fig.tight_layout()
plt.show()
print_to_pdf = 0
if print_to_pdf:
script_name = os.path.basename(__file__)
figure_name = os.path.splitext(script_name)[0]
print(f"Saving figure as {figure_name}.pdf")
fig.savefig(figure_name + ".pdf", bbox_inches="tight")