._pysketcher003.html 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. <!--
  2. Automatically generated HTML file from DocOnce source
  3. (https://github.com/hplgit/doconce/)
  4. -->
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  8. <meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
  9. <meta name="description" content="Using Pysketcher to Create Principal Sketches of Physics Problems">
  10. <meta name="keywords" content="tree data structure,recursive function calls">
  11. <title>Using Pysketcher to Create Principal Sketches of Physics Problems</title>
  12. <style type="text/css">
  13. /* blueish style */
  14. /* Color definitions: http://www.december.com/html/spec/color0.html
  15. CSS examples: http://www.w3schools.com/css/css_examples.asp */
  16. body {
  17. margin-top: 1.0em;
  18. background-color: #ffffff;
  19. font-family: Helvetica, Arial, FreeSans, san-serif;
  20. color: #000000;
  21. }
  22. h1 { font-size: 1.8em; color: #1e36ce; }
  23. h2 { font-size: 1.6em; color: #1e36ce; }
  24. h3 { font-size: 1.4em; color: #1e36ce; }
  25. a { color: #1e36ce; text-decoration:none; }
  26. tt { font-family: "Courier New", Courier; }
  27. /* pre style removed because it will interfer with pygments */
  28. p { text-indent: 0px; }
  29. hr { border: 0; width: 80%; border-bottom: 1px solid #aaa}
  30. p.caption { width: 80%; font-style: normal; text-align: left; }
  31. hr.figure { border: 0; width: 80%; border-bottom: 1px solid #aaa}
  32. div { text-align: justify; text-justify: inter-word; }
  33. </style>
  34. </head>
  35. <!-- tocinfo
  36. {'highest level': 1,
  37. 'sections': [(' A First Glimpse of Pysketcher ', 1, None, '___sec0'),
  38. (' Basic Construction of Sketches ', 2, None, '___sec1'),
  39. (' Basic Drawing ', 3, None, '___sec2'),
  40. (' Groups of Objects ', 3, None, '___sec3'),
  41. (' Changing Line Styles and Colors ', 3, None, '___sec4'),
  42. (' The Figure Composition as an Object Hierarchy ',
  43. 3,
  44. None,
  45. '___sec5'),
  46. (' Animation: Translating the Vehicle ', 3, None, '___sec6'),
  47. (' Animation: Rolling the Wheels ',
  48. 3,
  49. 'sketcher:vehicle1:anim',
  50. 'sketcher:vehicle1:anim'),
  51. (' Basic Shapes ', 1, None, '___sec8'),
  52. (' Axis ', 2, None, '___sec9'),
  53. (' Distance with Text ', 2, None, '___sec10'),
  54. (' Rectangle ', 2, None, '___sec11'),
  55. (' Triangle ', 2, None, '___sec12'),
  56. (' Arc ', 2, None, '___sec13'),
  57. (' Spring ', 2, None, '___sec14'),
  58. (' Dashpot ', 2, None, '___sec15'),
  59. (' Wavy ', 2, None, '___sec16'),
  60. (' Stochastic curves ', 2, None, '___sec17'),
  61. (' Inner Workings of the Pysketcher Tool ',
  62. 1,
  63. None,
  64. '___sec18'),
  65. (' Example of Classes for Geometric Objects ',
  66. 2,
  67. None,
  68. '___sec19'),
  69. (' Simple Geometric Objects ', 3, None, '___sec20'),
  70. (' Class Curve ', 3, None, '___sec21'),
  71. (' Compound Geometric Objects ', 3, None, '___sec22'),
  72. (' Adding Functionality via Recursion ', 2, None, '___sec23'),
  73. (' Basic Principles of Recursion ', 3, None, '___sec24'),
  74. (' Explaining Recursion ', 3, None, '___sec25'),
  75. (' Scaling, Translating, and Rotating a Figure ',
  76. 2,
  77. 'sketcher:scaling',
  78. 'sketcher:scaling'),
  79. (' Scaling ', 3, None, '___sec27'),
  80. (' Translation ', 3, None, '___sec28'),
  81. (' Rotation ', 3, None, '___sec29')]}
  82. end of tocinfo -->
  83. <body>
  84. <script type="text/x-mathjax-config">
  85. MathJax.Hub.Config({
  86. TeX: {
  87. equationNumbers: { autoNumber: "none" },
  88. extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
  89. }
  90. });
  91. </script>
  92. <script type="text/javascript"
  93. src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
  94. </script>
  95. <a name="part0003"></a>
  96. <p>
  97. <!-- begin top navigation -->
  98. <table style="width: 100%"><tr><td>
  99. <div style="text-align: left;"><a href="._pysketcher002.html"><img src="http://hplgit.github.io/doconce/bundled/html_images/prev1.png" border=0 alt="&laquo; Previous"></a></div>
  100. </td><td>
  101. </td></tr></table>
  102. <!-- end top navigation -->
  103. </p>
  104. <p>
  105. <!-- !split -->
  106. <h1 id="___sec18">Inner Workings of the Pysketcher Tool </h1>
  107. <p>
  108. We shall now explain how we can, quite easily, realize software with
  109. the capabilities demonstrated in the previous examples. Each object in
  110. the figure is represented as a class in a class hierarchy. Using
  111. inheritance, classes can inherit properties from parent classes and
  112. add new geometric features.
  113. <p>
  114. Class programming is a key technology for realizing Pysketcher.
  115. As soon as some classes are established, more are easily
  116. added. Enhanced functionality for all the classes is also easy to
  117. implement in common, generic code that can immediately be shared by
  118. all present and future classes. The fundamental data structure
  119. involved in the <code>pysketcher</code> package is a hierarchical tree, and much
  120. of the material on implementation issues targets how to traverse tree
  121. structures with recursive function calls in object hierarchies. This
  122. topic is of key relevance in a wide range of other applications as
  123. well. In total, the inner workings of Pysketcher constitute an
  124. excellent example on the power of class programming.
  125. <h2 id="___sec19">Example of Classes for Geometric Objects </h2>
  126. <p>
  127. We introduce class <code>Shape</code> as superclass for all specialized objects
  128. in a figure. This class does not store any data, but provides a
  129. series of functions that add functionality to all the subclasses.
  130. This will be shown later.
  131. <h3 id="___sec20">Simple Geometric Objects </h3>
  132. <p>
  133. One simple subclass is <code>Rectangle</code>, specified by the coordinates of
  134. the lower left corner and its width and height:
  135. <p>
  136. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  137. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Rectangle</span>(Shape):
  138. <span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">__init__</span>(<span style="color: #008000">self</span>, lower_left_corner, width, height):
  139. p <span style="color: #666666">=</span> lower_left_corner <span style="color: #408080; font-style: italic"># short form</span>
  140. x <span style="color: #666666">=</span> [p[<span style="color: #666666">0</span>], p[<span style="color: #666666">0</span>] <span style="color: #666666">+</span> width,
  141. p[<span style="color: #666666">0</span>] <span style="color: #666666">+</span> width, p[<span style="color: #666666">0</span>], p[<span style="color: #666666">0</span>]]
  142. y <span style="color: #666666">=</span> [p[<span style="color: #666666">1</span>], p[<span style="color: #666666">1</span>], p[<span style="color: #666666">1</span>] <span style="color: #666666">+</span> height,
  143. p[<span style="color: #666666">1</span>] <span style="color: #666666">+</span> height, p[<span style="color: #666666">1</span>]]
  144. <span style="color: #008000">self</span><span style="color: #666666">.</span>shapes <span style="color: #666666">=</span> {<span style="color: #BA2121">&#39;rectangle&#39;</span>: Curve(x,y)}
  145. </pre></div>
  146. <p>
  147. Any subclass of <code>Shape</code> will have a constructor that takes geometric
  148. information about the shape of the object and creates a dictionary
  149. <code>self.shapes</code> with the shape built of simpler shapes. The most
  150. fundamental shape is <code>Curve</code>, which is just a collection of \( (x,y) \)
  151. coordinates in two arrays <code>x</code> and <code>y</code>. Drawing the <code>Curve</code> object is
  152. a matter of plotting <code>y</code> versus <code>x</code>. For class <code>Rectangle</code> the <code>x</code>
  153. and <code>y</code> arrays contain the corner points of the rectangle in
  154. counterclockwise direction, starting and ending with in the lower left
  155. corner.
  156. <p>
  157. Class <code>Line</code> is also a simple class:
  158. <p>
  159. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  160. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Line</span>(Shape):
  161. <span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">__init__</span>(<span style="color: #008000">self</span>, start, end):
  162. x <span style="color: #666666">=</span> [start[<span style="color: #666666">0</span>], end[<span style="color: #666666">0</span>]]
  163. y <span style="color: #666666">=</span> [start[<span style="color: #666666">1</span>], end[<span style="color: #666666">1</span>]]
  164. <span style="color: #008000">self</span><span style="color: #666666">.</span>shapes <span style="color: #666666">=</span> {<span style="color: #BA2121">&#39;line&#39;</span>: Curve(x, y)}
  165. </pre></div>
  166. <p>
  167. Here we only need two points, the start and end point on the line.
  168. However, we may want to add some useful functionality, e.g., the ability
  169. to give an \( x \) coordinate and have the class calculate the
  170. corresponding \( y \) coordinate:
  171. <p>
  172. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  173. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"> <span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">__call__</span>(<span style="color: #008000">self</span>, x):
  174. <span style="color: #BA2121; font-style: italic">&quot;&quot;&quot;Given x, return y on the line.&quot;&quot;&quot;</span>
  175. x, y <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>shapes[<span style="color: #BA2121">&#39;line&#39;</span>]<span style="color: #666666">.</span>x, <span style="color: #008000">self</span><span style="color: #666666">.</span>shapes[<span style="color: #BA2121">&#39;line&#39;</span>]<span style="color: #666666">.</span>y
  176. <span style="color: #008000">self</span><span style="color: #666666">.</span>a <span style="color: #666666">=</span> (y[<span style="color: #666666">1</span>] <span style="color: #666666">-</span> y[<span style="color: #666666">0</span>])<span style="color: #666666">/</span>(x[<span style="color: #666666">1</span>] <span style="color: #666666">-</span> x[<span style="color: #666666">0</span>])
  177. <span style="color: #008000">self</span><span style="color: #666666">.</span>b <span style="color: #666666">=</span> y[<span style="color: #666666">0</span>] <span style="color: #666666">-</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>a<span style="color: #666666">*</span>x[<span style="color: #666666">0</span>]
  178. <span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>a<span style="color: #666666">*</span>x <span style="color: #666666">+</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>b
  179. </pre></div>
  180. <p>
  181. Unfortunately, this is too simplistic because vertical lines cannot be
  182. handled (infinite <code>self.a</code>). The true source code of <code>Line</code> therefore
  183. provides a more general solution at the cost of significantly longer
  184. code with more tests.
  185. <p>
  186. A circle implies a somewhat increased complexity. Again we represent
  187. the geometric object by a <code>Curve</code> object, but this time the <code>Curve</code>
  188. object needs to store a large number of points on the curve such that
  189. a plotting program produces a visually smooth curve. The points on
  190. the circle must be calculated manually in the constructor of class
  191. <code>Circle</code>. The formulas for points \( (x,y) \) on a curve with radius \( R \)
  192. and center at \( (x_0, y_0) \) are given by
  193. $$
  194. \begin{align*}
  195. x &= x_0 + R\cos (t),\\
  196. y &= y_0 + R\sin (t),
  197. \end{align*}
  198. $$
  199. where \( t\in [0, 2\pi] \). A discrete set of \( t \) values in this
  200. interval gives the corresponding set of \( (x,y) \) coordinates on
  201. the circle. The user must specify the resolution as the number
  202. of \( t \) values. The circle's radius and center must of course
  203. also be specified.
  204. <p>
  205. We can write the <code>Circle</code> class as
  206. <p>
  207. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  208. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Circle</span>(Shape):
  209. <span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">__init__</span>(<span style="color: #008000">self</span>, center, radius, resolution<span style="color: #666666">=180</span>):
  210. <span style="color: #008000">self</span><span style="color: #666666">.</span>center, <span style="color: #008000">self</span><span style="color: #666666">.</span>radius <span style="color: #666666">=</span> center, radius
  211. <span style="color: #008000">self</span><span style="color: #666666">.</span>resolution <span style="color: #666666">=</span> resolution
  212. t <span style="color: #666666">=</span> linspace(<span style="color: #666666">0</span>, <span style="color: #666666">2*</span>pi, resolution<span style="color: #666666">+1</span>)
  213. x0 <span style="color: #666666">=</span> center[<span style="color: #666666">0</span>]; y0 <span style="color: #666666">=</span> center[<span style="color: #666666">1</span>]
  214. R <span style="color: #666666">=</span> radius
  215. x <span style="color: #666666">=</span> x0 <span style="color: #666666">+</span> R<span style="color: #666666">*</span>cos(t)
  216. y <span style="color: #666666">=</span> y0 <span style="color: #666666">+</span> R<span style="color: #666666">*</span>sin(t)
  217. <span style="color: #008000">self</span><span style="color: #666666">.</span>shapes <span style="color: #666666">=</span> {<span style="color: #BA2121">&#39;circle&#39;</span>: Curve(x, y)}
  218. </pre></div>
  219. <p>
  220. As in class <code>Line</code> we can offer the possibility to give an angle
  221. \( \theta \) (equivalent to \( t \) in the formulas above)
  222. and then get the corresponding \( x \) and \( y \) coordinates:
  223. <p>
  224. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  225. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"> <span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">__call__</span>(<span style="color: #008000">self</span>, theta):
  226. <span style="color: #BA2121; font-style: italic">&quot;&quot;&quot;Return (x, y) point corresponding to angle theta.&quot;&quot;&quot;</span>
  227. <span style="color: #008000; font-weight: bold">return</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>center[<span style="color: #666666">0</span>] <span style="color: #666666">+</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>radius<span style="color: #666666">*</span>cos(theta), \
  228. <span style="color: #008000">self</span><span style="color: #666666">.</span>center[<span style="color: #666666">1</span>] <span style="color: #666666">+</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>radius<span style="color: #666666">*</span>sin(theta)
  229. </pre></div>
  230. <p>
  231. There is one flaw with this method: it yields illegal values after
  232. a translation, scaling, or rotation of the circle.
  233. <p>
  234. A part of a circle, an arc, is a frequent geometric object when
  235. drawing mechanical systems. The arc is constructed much like
  236. a circle, but \( t \) runs in \( [\theta_s, \theta_s + \theta_a] \). Giving
  237. \( \theta_s \) and \( \theta_a \) the slightly more descriptive names
  238. <code>start_angle</code> and <code>arc_angle</code>, the code looks like this:
  239. <p>
  240. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  241. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Arc</span>(Shape):
  242. <span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">__init__</span>(<span style="color: #008000">self</span>, center, radius,
  243. start_angle, arc_angle,
  244. resolution<span style="color: #666666">=180</span>):
  245. <span style="color: #008000">self</span><span style="color: #666666">.</span>start_angle <span style="color: #666666">=</span> radians(start_angle)
  246. <span style="color: #008000">self</span><span style="color: #666666">.</span>arc_angle <span style="color: #666666">=</span> radians(arc_angle)
  247. t <span style="color: #666666">=</span> linspace(<span style="color: #008000">self</span><span style="color: #666666">.</span>start_angle,
  248. <span style="color: #008000">self</span><span style="color: #666666">.</span>start_angle <span style="color: #666666">+</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>arc_angle,
  249. resolution<span style="color: #666666">+1</span>)
  250. x0 <span style="color: #666666">=</span> center[<span style="color: #666666">0</span>]; y0 <span style="color: #666666">=</span> center[<span style="color: #666666">1</span>]
  251. R <span style="color: #666666">=</span> radius
  252. x <span style="color: #666666">=</span> x0 <span style="color: #666666">+</span> R<span style="color: #666666">*</span>cos(t)
  253. y <span style="color: #666666">=</span> y0 <span style="color: #666666">+</span> R<span style="color: #666666">*</span>sin(t)
  254. <span style="color: #008000">self</span><span style="color: #666666">.</span>shapes <span style="color: #666666">=</span> {<span style="color: #BA2121">&#39;arc&#39;</span>: Curve(x, y)}
  255. </pre></div>
  256. <p>
  257. Having the <code>Arc</code> class, a <code>Circle</code> can alternatively be defined as
  258. a subclass specializing the arc to a circle:
  259. <p>
  260. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  261. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Circle</span>(Arc):
  262. <span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">__init__</span>(<span style="color: #008000">self</span>, center, radius, resolution<span style="color: #666666">=180</span>):
  263. Arc<span style="color: #666666">.</span>__init__(<span style="color: #008000">self</span>, center, radius, <span style="color: #666666">0</span>, <span style="color: #666666">360</span>, resolution)
  264. </pre></div>
  265. <h3 id="___sec21">Class Curve </h3>
  266. <p>
  267. Class <code>Curve</code> sits on the coordinates to be drawn, but how is that
  268. done? The constructor of class <code>Curve</code> just stores the coordinates,
  269. while a method <code>draw</code> sends the coordinates to the plotting program to
  270. make a graph. Or more precisely, to avoid a lot of (e.g.)
  271. Matplotlib-specific plotting commands in class <code>Curve</code> we have created
  272. a small layer with a simple programming interface to plotting
  273. programs. This makes it straightforward to change from Matplotlib to
  274. another plotting program. The programming interface is represented by
  275. the <code>drawing_tool</code> object and has a few functions:
  276. <ul>
  277. <li> <code>plot_curve</code> for sending a curve in terms of \( x \) and \( y \) coordinates
  278. to the plotting program,</li>
  279. <li> <code>set_coordinate_system</code> for specifying the graphics area,</li>
  280. <li> <code>erase</code> for deleting all elements of the graph,</li>
  281. <li> <code>set_grid</code> for turning on a grid (convenient while constructing the figure),</li>
  282. <li> <code>set_instruction_file</code> for creating a separate file with all
  283. plotting commands (Matplotlib commands in our case),</li>
  284. <li> a series of <code>set_X</code> functions where <code>X</code> is some property like
  285. <code>linecolor</code>, <code>linestyle</code>, <code>linewidth</code>, <code>filled_curves</code>.</li>
  286. </ul>
  287. This is basically all we need to communicate to a plotting program.
  288. <p>
  289. Any class in the <code>Shape</code> hierarchy inherits <code>set_X</code> functions for
  290. setting properties of curves. This information is propagated to
  291. all other shape objects in the <code>self.shapes</code> dictionary. Class
  292. <code>Curve</code> stores the line properties together with the coordinates
  293. of its curve and propagates this information to the plotting program.
  294. When saying <code>vehicle.set_linewidth(10)</code>, all objects that make
  295. up the <code>vehicle</code> object will get a <code>set_linewidth(10)</code> call,
  296. but only the <code>Curve</code> object at the end of the chain will actually
  297. store the information and send it to the plotting program.
  298. <p>
  299. A rough sketch of class <code>Curve</code> reads
  300. <p>
  301. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  302. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Curve</span>(Shape):
  303. <span style="color: #BA2121; font-style: italic">&quot;&quot;&quot;General curve as a sequence of (x,y) coordintes.&quot;&quot;&quot;</span>
  304. <span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">__init__</span>(<span style="color: #008000">self</span>, x, y):
  305. <span style="color: #008000">self</span><span style="color: #666666">.</span>x <span style="color: #666666">=</span> asarray(x, dtype<span style="color: #666666">=</span><span style="color: #008000">float</span>)
  306. <span style="color: #008000">self</span><span style="color: #666666">.</span>y <span style="color: #666666">=</span> asarray(y, dtype<span style="color: #666666">=</span><span style="color: #008000">float</span>)
  307. <span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">draw</span>(<span style="color: #008000">self</span>):
  308. drawing_tool<span style="color: #666666">.</span>plot_curve(
  309. <span style="color: #008000">self</span><span style="color: #666666">.</span>x, <span style="color: #008000">self</span><span style="color: #666666">.</span>y,
  310. <span style="color: #008000">self</span><span style="color: #666666">.</span>linestyle, <span style="color: #008000">self</span><span style="color: #666666">.</span>linewidth, <span style="color: #008000">self</span><span style="color: #666666">.</span>linecolor, <span style="color: #666666">...</span>)
  311. <span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">set_linewidth</span>(<span style="color: #008000">self</span>, width):
  312. <span style="color: #008000">self</span><span style="color: #666666">.</span>linewidth <span style="color: #666666">=</span> width
  313. det set_linestyle(<span style="color: #008000">self</span>, style):
  314. <span style="color: #008000">self</span><span style="color: #666666">.</span>linestyle <span style="color: #666666">=</span> style
  315. <span style="color: #666666">...</span>
  316. </pre></div>
  317. <h3 id="___sec22">Compound Geometric Objects </h3>
  318. <p>
  319. The simple classes <code>Line</code>, <code>Arc</code>, and <code>Circle</code> could can the geometric
  320. shape through just one <code>Curve</code> object. More complicated shapes are
  321. built from instances of various subclasses of <code>Shape</code>. Classes used
  322. for professional drawings soon get quite complex in composition and
  323. have a lot of geometric details, so here we prefer to make a very
  324. simple composition: the already drawn vehicle from Figure
  325. <a href="._pysketcher001.html#sketcher:fig:vehicle0">2</a>. That is, instead of composing the drawing
  326. in a Python program as shown above, we make a subclass <code>Vehicle0</code> in
  327. the <code>Shape</code> hierarchy for doing the same thing.
  328. <p>
  329. The <code>Shape</code> hierarchy is found in the <code>pysketcher</code> package, so to use these
  330. classes or derive a new one, we need to import <code>pysketcher</code>. The constructor
  331. of class <code>Vehicle0</code> performs approximately the same statements as
  332. in the example program we developed for making the drawing in
  333. Figure <a href="._pysketcher001.html#sketcher:fig:vehicle0">2</a>.
  334. <p>
  335. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  336. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">pysketcher</span> <span style="color: #008000; font-weight: bold">import</span> <span style="color: #666666">*</span>
  337. <span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Vehicle0</span>(Shape):
  338. <span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">__init__</span>(<span style="color: #008000">self</span>, w_1, R, L, H):
  339. wheel1 <span style="color: #666666">=</span> Circle(center<span style="color: #666666">=</span>(w_1, R), radius<span style="color: #666666">=</span>R)
  340. wheel2 <span style="color: #666666">=</span> wheel1<span style="color: #666666">.</span>copy()
  341. wheel2<span style="color: #666666">.</span>translate((L,<span style="color: #666666">0</span>))
  342. under <span style="color: #666666">=</span> Rectangle(lower_left_corner<span style="color: #666666">=</span>(w_1<span style="color: #666666">-2*</span>R, <span style="color: #666666">2*</span>R),
  343. width<span style="color: #666666">=2*</span>R <span style="color: #666666">+</span> L <span style="color: #666666">+</span> <span style="color: #666666">2*</span>R, height<span style="color: #666666">=</span>H)
  344. over <span style="color: #666666">=</span> Rectangle(lower_left_corner<span style="color: #666666">=</span>(w_1, <span style="color: #666666">2*</span>R <span style="color: #666666">+</span> H),
  345. width<span style="color: #666666">=2.5*</span>R, height<span style="color: #666666">=1.25*</span>H)
  346. wheels <span style="color: #666666">=</span> Composition(
  347. {<span style="color: #BA2121">&#39;wheel1&#39;</span>: wheel1, <span style="color: #BA2121">&#39;wheel2&#39;</span>: wheel2})
  348. body <span style="color: #666666">=</span> Composition(
  349. {<span style="color: #BA2121">&#39;under&#39;</span>: under, <span style="color: #BA2121">&#39;over&#39;</span>: over})
  350. vehicle <span style="color: #666666">=</span> Composition({<span style="color: #BA2121">&#39;wheels&#39;</span>: wheels, <span style="color: #BA2121">&#39;body&#39;</span>: body})
  351. xmax <span style="color: #666666">=</span> w_1 <span style="color: #666666">+</span> <span style="color: #666666">2*</span>L <span style="color: #666666">+</span> <span style="color: #666666">3*</span>R
  352. ground <span style="color: #666666">=</span> Wall(x<span style="color: #666666">=</span>[R, xmax], y<span style="color: #666666">=</span>[<span style="color: #666666">0</span>, <span style="color: #666666">0</span>], thickness<span style="color: #666666">=-0.3*</span>R)
  353. <span style="color: #008000">self</span><span style="color: #666666">.</span>shapes <span style="color: #666666">=</span> {<span style="color: #BA2121">&#39;vehicle&#39;</span>: vehicle, <span style="color: #BA2121">&#39;ground&#39;</span>: ground}
  354. </pre></div>
  355. <p>
  356. Any subclass of <code>Shape</code> <em>must</em> define the <code>shapes</code> attribute, otherwise
  357. the inherited <code>draw</code> method (and a lot of other methods too) will
  358. not work.
  359. <p>
  360. The painting of the vehicle, as shown in the right part of
  361. Figure <a href="._pysketcher001.html#sketcher:fig:vehicle0:v2">6</a>, could in class <code>Vehicle0</code>
  362. be offered by a method:
  363. <p>
  364. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  365. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"> <span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">colorful</span>(<span style="color: #008000">self</span>):
  366. wheels <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>shapes[<span style="color: #BA2121">&#39;vehicle&#39;</span>][<span style="color: #BA2121">&#39;wheels&#39;</span>]
  367. wheels<span style="color: #666666">.</span>set_filled_curves(<span style="color: #BA2121">&#39;blue&#39;</span>)
  368. wheels<span style="color: #666666">.</span>set_linewidth(<span style="color: #666666">6</span>)
  369. wheels<span style="color: #666666">.</span>set_linecolor(<span style="color: #BA2121">&#39;black&#39;</span>)
  370. under <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>shapes[<span style="color: #BA2121">&#39;vehicle&#39;</span>][<span style="color: #BA2121">&#39;body&#39;</span>][<span style="color: #BA2121">&#39;under&#39;</span>]
  371. under<span style="color: #666666">.</span>set_filled_curves(<span style="color: #BA2121">&#39;red&#39;</span>)
  372. over <span style="color: #666666">=</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>shapes[<span style="color: #BA2121">&#39;vehicle&#39;</span>][<span style="color: #BA2121">&#39;body&#39;</span>][<span style="color: #BA2121">&#39;over&#39;</span>]
  373. over<span style="color: #666666">.</span>set_filled_curves(pattern<span style="color: #666666">=</span><span style="color: #BA2121">&#39;/&#39;</span>)
  374. over<span style="color: #666666">.</span>set_linewidth(<span style="color: #666666">14</span>)
  375. </pre></div>
  376. <p>
  377. The usage of the class is simple: after having set up an appropriate
  378. coordinate system as previously shown, we can do
  379. <p>
  380. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  381. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">vehicle <span style="color: #666666">=</span> Vehicle0(w_1, R, L, H)
  382. vehicle<span style="color: #666666">.</span>draw()
  383. drawing_tool<span style="color: #666666">.</span>display()
  384. </pre></div>
  385. <p>
  386. and go on the make a painted version by
  387. <p>
  388. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  389. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">drawing_tool<span style="color: #666666">.</span>erase()
  390. vehicle<span style="color: #666666">.</span>colorful()
  391. vehicle<span style="color: #666666">.</span>draw()
  392. drawing_tool<span style="color: #666666">.</span>display()
  393. </pre></div>
  394. <p>
  395. A complete code defining and using class <code>Vehicle0</code> is found in the file
  396. <a href="http://tinyurl.com/ot733jn/vehicle2.py" target="_self"><tt>vehicle2.py</tt></a>.
  397. <p>
  398. The <code>pysketcher</code> package contains a wide range of classes for various
  399. geometrical objects, particularly those that are frequently used in
  400. drawings of mechanical systems.
  401. <h2 id="___sec23">Adding Functionality via Recursion </h2>
  402. <p>
  403. The really powerful feature of our class hierarchy is that we can add
  404. much functionality to the superclass <code>Shape</code> and to the &quot;bottom&quot; class
  405. <code>Curve</code>, and then all other classes for various types of geometrical shapes
  406. immediately get the new functionality. To explain the idea we may
  407. look at the <code>draw</code> method, which all classes in the <code>Shape</code>
  408. hierarchy must have. The inner workings of the <code>draw</code> method explain
  409. the secrets of how a series of other useful operations on figures
  410. can be implemented.
  411. <h3 id="___sec24">Basic Principles of Recursion </h3>
  412. <p>
  413. Note that we work with two types of hierarchies in the
  414. present documentation: one Python <em>class hierarchy</em>,
  415. with <code>Shape</code> as superclass, and one <em>object hierarchy</em> of figure elements
  416. in a specific figure. A subclass of <code>Shape</code> stores its figure in the
  417. <code>self.shapes</code> dictionary. This dictionary represents the object hierarchy
  418. of figure elements for that class. We want to make one <code>draw</code> call
  419. for an instance, say our class <code>Vehicle0</code>, and then we want this call
  420. to be propagated to <em>all</em> objects that are contained in
  421. <code>self.shapes</code> and all is nested subdictionaries. How is this done?
  422. <p>
  423. The natural starting point is to call <code>draw</code> for each <code>Shape</code> object
  424. in the <code>self.shapes</code> dictionary:
  425. <p>
  426. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  427. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">draw</span>(<span style="color: #008000">self</span>):
  428. <span style="color: #008000; font-weight: bold">for</span> shape <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>shapes:
  429. <span style="color: #008000">self</span><span style="color: #666666">.</span>shapes[shape]<span style="color: #666666">.</span>draw()
  430. </pre></div>
  431. <p>
  432. This general method can be provided by class <code>Shape</code> and inherited in
  433. subclasses like <code>Vehicle0</code>. Let <code>v</code> be a <code>Vehicle0</code> instance.
  434. Seemingly, a call <code>v.draw()</code> just calls
  435. <p>
  436. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  437. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">v<span style="color: #666666">.</span>shapes[<span style="color: #BA2121">&#39;vehicle&#39;</span>]<span style="color: #666666">.</span>draw()
  438. v<span style="color: #666666">.</span>shapes[<span style="color: #BA2121">&#39;ground&#39;</span>]<span style="color: #666666">.</span>draw()
  439. </pre></div>
  440. <p>
  441. However, in the former call we call the <code>draw</code> method of a <code>Composition</code> object
  442. whose <code>self.shapes</code> attributed has two elements: <code>wheels</code> and <code>body</code>.
  443. Since class <code>Composition</code> inherits the same <code>draw</code> method, this method will
  444. run through <code>self.shapes</code> and call <code>wheels.draw()</code> and <code>body.draw()</code>.
  445. Now, the <code>wheels</code> object is also a <code>Composition</code> with the same <code>draw</code>
  446. method, which will run through <code>self.shapes</code>, now containing
  447. the <code>wheel1</code> and <code>wheel2</code> objects. The <code>wheel1</code> object is a <code>Circle</code>,
  448. so calling <code>wheel1.draw()</code> calls the <code>draw</code> method in class <code>Circle</code>,
  449. but this is the same <code>draw</code> method as shown above. This method will
  450. therefore traverse the circle's <code>shapes</code> dictionary, which we have seen
  451. consists of one <code>Curve</code> element.
  452. <p>
  453. The <code>Curve</code> object holds the coordinates to be plotted so here <code>draw</code>
  454. really needs to do something &quot;physical&quot;, namely send the coordinates to
  455. the plotting program. The <code>draw</code> method is outlined in the short listing
  456. of class <code>Curve</code> shown previously.
  457. <p>
  458. We can go to any of the other shape objects that appear in the figure
  459. hierarchy and follow their <code>draw</code> calls in the similar way. Every time,
  460. a <code>draw</code> call will invoke a new <code>draw</code> call, until we eventually hit
  461. a <code>Curve</code> object at the &quot;bottom&quot; of the figure hierarchy, and then that part
  462. of the figure is really plotted (or more precisely, the coordinates
  463. are sent to a plotting program).
  464. <p>
  465. When a method calls itself, such as <code>draw</code> does, the calls are known as
  466. <em>recursive</em> and the programming principle is referred to as
  467. <em>recursion</em>. This technique is very often used to traverse hierarchical
  468. structures like the figure structures we work with here. Even though the
  469. hierarchy of objects building up a figure are of different types, they
  470. all inherit the same <code>draw</code> method and therefore exhibit the same
  471. behavior with respect to drawing. Only the <code>Curve</code> object has a different
  472. <code>draw</code> method, which does not lead to more recursion.
  473. <h3 id="___sec25">Explaining Recursion </h3>
  474. <p>
  475. Understanding recursion is usually a challenge. To get a better idea of
  476. how recursion works, we have equipped class <code>Shape</code> with a method <code>recurse</code>
  477. that just visits all the objects in the <code>shapes</code> dictionary and prints
  478. out a message for each object.
  479. This feature allows us to trace the execution and see exactly where
  480. we are in the hierarchy and which objects that are visited.
  481. <p>
  482. The <code>recurse</code> method is very similar to <code>draw</code>:
  483. <p>
  484. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  485. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"> <span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">recurse</span>(<span style="color: #008000">self</span>, name, indent<span style="color: #666666">=0</span>):
  486. <span style="color: #408080; font-style: italic"># print message where we are (name is where we come from)</span>
  487. <span style="color: #008000; font-weight: bold">for</span> shape <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>shapes:
  488. <span style="color: #408080; font-style: italic"># print message about which object to visit</span>
  489. <span style="color: #008000">self</span><span style="color: #666666">.</span>shapes[shape]<span style="color: #666666">.</span>recurse(indent<span style="color: #666666">+2</span>, shape)
  490. </pre></div>
  491. <p>
  492. The <code>indent</code> parameter governs how much the message from this
  493. <code>recurse</code> method is intended. We increase <code>indent</code> by 2 for every
  494. level in the hierarchy, i.e., every row of objects in Figure
  495. <a href="#sketcher:fig:Vehicle0:hier2">8</a>. This indentation makes it easy to
  496. see on the printout how far down in the hierarchy we are.
  497. <p>
  498. A typical message written by <code>recurse</code> when <code>name</code> is <code>'body'</code> and
  499. the <code>shapes</code> dictionary has the keys <code>'over'</code> and <code>'under'</code>,
  500. will be
  501. <p>
  502. <!-- code=text (!bc dat) typeset with pygments style "default" -->
  503. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"> Composition: body.shapes has entries &#39;over&#39;, &#39;under&#39;
  504. call body.shapes[&quot;over&quot;].recurse(&quot;over&quot;, 6)
  505. </pre></div>
  506. <p>
  507. The number of leading blanks on each line corresponds to the value of
  508. <code>indent</code>. The code printing out such messages looks like
  509. <p>
  510. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  511. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"> <span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">recurse</span>(<span style="color: #008000">self</span>, name, indent<span style="color: #666666">=0</span>):
  512. space <span style="color: #666666">=</span> <span style="color: #BA2121">&#39; &#39;</span><span style="color: #666666">*</span>indent
  513. <span style="color: #008000; font-weight: bold">print</span> space, <span style="color: #BA2121">&#39;</span><span style="color: #BB6688; font-weight: bold">%s</span><span style="color: #BA2121">: </span><span style="color: #BB6688; font-weight: bold">%s</span><span style="color: #BA2121">.shapes has entries&#39;</span> <span style="color: #666666">%</span> \
  514. (<span style="color: #008000">self</span><span style="color: #666666">.</span>__class__<span style="color: #666666">.</span>__name__, name), \
  515. <span style="color: #008000">str</span>(<span style="color: #008000">list</span>(<span style="color: #008000">self</span><span style="color: #666666">.</span>shapes<span style="color: #666666">.</span>keys()))[<span style="color: #666666">1</span>:<span style="color: #666666">-1</span>]
  516. <span style="color: #008000; font-weight: bold">for</span> shape <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>shapes:
  517. <span style="color: #008000; font-weight: bold">print</span> space,
  518. <span style="color: #008000; font-weight: bold">print</span> <span style="color: #BA2121">&#39;call </span><span style="color: #BB6688; font-weight: bold">%s</span><span style="color: #BA2121">.shapes[&quot;</span><span style="color: #BB6688; font-weight: bold">%s</span><span style="color: #BA2121">&quot;].recurse(&quot;</span><span style="color: #BB6688; font-weight: bold">%s</span><span style="color: #BA2121">&quot;, </span><span style="color: #BB6688; font-weight: bold">%d</span><span style="color: #BA2121">)&#39;</span> <span style="color: #666666">%</span> \
  519. (name, shape, shape, indent<span style="color: #666666">+2</span>)
  520. <span style="color: #008000">self</span><span style="color: #666666">.</span>shapes[shape]<span style="color: #666666">.</span>recurse(shape, indent<span style="color: #666666">+2</span>)
  521. </pre></div>
  522. <p>
  523. Let us follow a <code>v.recurse('vehicle')</code> call in detail, <code>v</code> being
  524. a <code>Vehicle0</code> instance. Before looking into the output from <code>recurse</code>,
  525. let us get an overview of the figure hierarchy in the <code>v</code> object
  526. (as produced by <code>print v</code>)
  527. <p>
  528. <!-- code=text (!bc dat) typeset with pygments style "default" -->
  529. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">ground
  530. wall
  531. vehicle
  532. body
  533. over
  534. rectangle
  535. under
  536. rectangle
  537. wheels
  538. wheel1
  539. arc
  540. wheel2
  541. arc
  542. </pre></div>
  543. <p>
  544. The <code>recurse</code> method performs the same kind of traversal of the
  545. hierarchy, but writes out and explains a lot more.
  546. <p>
  547. The data structure represented by <code>v.shapes</code> is known as a <em>tree</em>.
  548. As in physical trees, there is a <em>root</em>, here the <code>v.shapes</code>
  549. dictionary. A graphical illustration of the tree (upside down) is
  550. shown in Figure <a href="#sketcher:fig:Vehicle0:hier2">8</a>.
  551. From the root there are one or more branches, here two:
  552. <code>ground</code> and <code>vehicle</code>. Following the <code>vehicle</code> branch, it has two new
  553. branches, <code>body</code> and <code>wheels</code>. Relationships as in family trees
  554. are often used to describe the relations in object trees too: we say
  555. that <code>vehicle</code> is the parent of <code>body</code> and that <code>body</code> is a child of
  556. <code>vehicle</code>. The term <em>node</em> is also often used to describe an element
  557. in a tree. A node may have several other nodes as <em>descendants</em>.
  558. <p>
  559. <center> <!-- figure -->
  560. <hr class="figure">
  561. <center><p class="caption">Figure 8: Hierarchy of figure elements in an instance of class <code>Vehicle0</code>. <div id="sketcher:fig:Vehicle0:hier2"></div> </p></center>
  562. <p><img src="fig-tut/Vehicle0_hier2.png" align="bottom" width=600></p>
  563. </center>
  564. <p>
  565. Recursion is the principal programming technique to traverse tree structures.
  566. Any object in the tree can be viewed as a root of a subtree. For
  567. example, <code>wheels</code> is the root of a subtree that branches into
  568. <code>wheel1</code> and <code>wheel2</code>. So when processing an object in the tree,
  569. we imagine we process the root and then recurse into a subtree, but the
  570. first object we recurse into can be viewed as the root of the subtree, so the
  571. processing procedure of the parent object can be repeated.
  572. <p>
  573. A recommended next step is to simulate the <code>recurse</code> method by hand and
  574. carefully check that what happens in the visits to <code>recurse</code> is
  575. consistent with the output listed below. Although tedious, this is
  576. a major exercise that guaranteed will help to demystify recursion.
  577. <p>
  578. A part of the printout of <code>v.recurse('vehicle')</code> looks like
  579. <p>
  580. <!-- code=text (!bc dat) typeset with pygments style "default" -->
  581. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"> Vehicle0: vehicle.shapes has entries &#39;ground&#39;, &#39;vehicle&#39;
  582. call vehicle.shapes[&quot;ground&quot;].recurse(&quot;ground&quot;, 2)
  583. Wall: ground.shapes has entries &#39;wall&#39;
  584. call ground.shapes[&quot;wall&quot;].recurse(&quot;wall&quot;, 4)
  585. reached &quot;bottom&quot; object Curve
  586. call vehicle.shapes[&quot;vehicle&quot;].recurse(&quot;vehicle&quot;, 2)
  587. Composition: vehicle.shapes has entries &#39;body&#39;, &#39;wheels&#39;
  588. call vehicle.shapes[&quot;body&quot;].recurse(&quot;body&quot;, 4)
  589. Composition: body.shapes has entries &#39;over&#39;, &#39;under&#39;
  590. call body.shapes[&quot;over&quot;].recurse(&quot;over&quot;, 6)
  591. Rectangle: over.shapes has entries &#39;rectangle&#39;
  592. call over.shapes[&quot;rectangle&quot;].recurse(&quot;rectangle&quot;, 8)
  593. reached &quot;bottom&quot; object Curve
  594. call body.shapes[&quot;under&quot;].recurse(&quot;under&quot;, 6)
  595. Rectangle: under.shapes has entries &#39;rectangle&#39;
  596. call under.shapes[&quot;rectangle&quot;].recurse(&quot;rectangle&quot;, 8)
  597. reached &quot;bottom&quot; object Curve
  598. ...
  599. </pre></div>
  600. <p>
  601. This example should clearly demonstrate the principle that we
  602. can start at any object in the tree and do a recursive set
  603. of calls with that object as root.
  604. <h2 id="sketcher:scaling">Scaling, Translating, and Rotating a Figure</h2>
  605. <p>
  606. With recursion, as explained in the previous section, we can within
  607. minutes equip <em>all</em> classes in the <code>Shape</code> hierarchy, both present and
  608. future ones, with the ability to scale the figure, translate it,
  609. or rotate it. This added functionality requires only a few lines
  610. of code.
  611. <h3 id="___sec27">Scaling </h3>
  612. <p>
  613. We start with the simplest of the three geometric transformations,
  614. namely scaling. For a <code>Curve</code> instance containing a set of \( n \)
  615. coordinates \( (x_i,y_i) \) that make up a curve, scaling by a factor \( a \)
  616. means that we multiply all the \( x \) and \( y \) coordinates by \( a \):
  617. $$
  618. x_i \leftarrow ax_i,\quad y_i\leftarrow ay_i,
  619. \quad i=0,\ldots,n-1\thinspace .
  620. $$
  621. Here we apply the arrow as an assignment operator.
  622. The corresponding Python implementation in
  623. class <code>Curve</code> reads
  624. <p>
  625. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  626. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Curve</span>:
  627. <span style="color: #666666">...</span>
  628. <span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">scale</span>(<span style="color: #008000">self</span>, factor):
  629. <span style="color: #008000">self</span><span style="color: #666666">.</span>x <span style="color: #666666">=</span> factor<span style="color: #666666">*</span><span style="color: #008000">self</span><span style="color: #666666">.</span>x
  630. <span style="color: #008000">self</span><span style="color: #666666">.</span>y <span style="color: #666666">=</span> factor<span style="color: #666666">*</span><span style="color: #008000">self</span><span style="color: #666666">.</span>y
  631. </pre></div>
  632. <p>
  633. Note here that <code>self.x</code> and <code>self.y</code> are Numerical Python arrays,
  634. so that multiplication by a scalar number <code>factor</code> is
  635. a vectorized operation.
  636. <p>
  637. An even more efficient implementation is to make use of in-place
  638. multiplication in the arrays,
  639. <p>
  640. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  641. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Curve</span>:
  642. <span style="color: #666666">...</span>
  643. <span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">scale</span>(<span style="color: #008000">self</span>, factor):
  644. <span style="color: #008000">self</span><span style="color: #666666">.</span>x <span style="color: #666666">*=</span> factor
  645. <span style="color: #008000">self</span><span style="color: #666666">.</span>y <span style="color: #666666">*=</span> factor
  646. </pre></div>
  647. <p>
  648. as this saves the creation of temporary arrays like <code>factor*self.x</code>.
  649. <p>
  650. In an instance of a subclass of <code>Shape</code>, the meaning of a method
  651. <code>scale</code> is to run through all objects in the dictionary <code>shapes</code> and
  652. ask each object to scale itself. This is the same delegation of
  653. actions to subclass instances as we do in the <code>draw</code> (or <code>recurse</code>)
  654. method. All objects, except <code>Curve</code> instances, can share the same
  655. implementation of the <code>scale</code> method. Therefore, we place the <code>scale</code>
  656. method in the superclass <code>Shape</code> such that all subclasses inherit the
  657. method. Since <code>scale</code> and <code>draw</code> are so similar, we can easily
  658. implement the <code>scale</code> method in class <code>Shape</code> by copying and editing
  659. the <code>draw</code> method:
  660. <p>
  661. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  662. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Shape</span>:
  663. <span style="color: #666666">...</span>
  664. <span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">scale</span>(<span style="color: #008000">self</span>, factor):
  665. <span style="color: #008000; font-weight: bold">for</span> shape <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>shapes:
  666. <span style="color: #008000">self</span><span style="color: #666666">.</span>shapes[shape]<span style="color: #666666">.</span>scale(factor)
  667. </pre></div>
  668. <p>
  669. This is all we have to do in order to equip all subclasses of
  670. <code>Shape</code> with scaling functionality!
  671. Any piece of the figure will scale itself, in the same manner
  672. as it can draw itself.
  673. <h3 id="___sec28">Translation </h3>
  674. <p>
  675. A set of coordinates \( (x_i, y_i) \) can be translated \( v_0 \) units in
  676. the \( x \) direction and \( v_1 \) units in the \( y \) direction using the formulas
  677. $$
  678. \begin{equation*}
  679. x_i\leftarrow x_i+v_0,\quad y_i\leftarrow y_i+v_1,
  680. \quad i=0,\ldots,n-1\thinspace .
  681. \end{equation*}
  682. $$
  683. The natural specification of the translation is in terms of the
  684. vector \( v=(v_0,v_1) \).
  685. The corresponding Python implementation in class <code>Curve</code> becomes
  686. <p>
  687. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  688. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Curve</span>:
  689. <span style="color: #666666">...</span>
  690. <span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">translate</span>(<span style="color: #008000">self</span>, v):
  691. <span style="color: #008000">self</span><span style="color: #666666">.</span>x <span style="color: #666666">+=</span> v[<span style="color: #666666">0</span>]
  692. <span style="color: #008000">self</span><span style="color: #666666">.</span>y <span style="color: #666666">+=</span> v[<span style="color: #666666">1</span>]
  693. </pre></div>
  694. <p>
  695. The translation operation for a shape object is very similar to the
  696. scaling and drawing operations. This means that we can implement a
  697. common method <code>translate</code> in the superclass <code>Shape</code>. The code
  698. is parallel to the <code>scale</code> method:
  699. <p>
  700. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  701. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">class</span> <span style="color: #0000FF; font-weight: bold">Shape</span>:
  702. <span style="color: #666666">....</span>
  703. <span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">translate</span>(<span style="color: #008000">self</span>, v):
  704. <span style="color: #008000; font-weight: bold">for</span> shape <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">self</span><span style="color: #666666">.</span>shapes:
  705. <span style="color: #008000">self</span><span style="color: #666666">.</span>shapes[shape]<span style="color: #666666">.</span>translate(v)
  706. </pre></div>
  707. <h3 id="___sec29">Rotation </h3>
  708. <p>
  709. Rotating a figure is more complicated than scaling and translating.
  710. A counter clockwise rotation of \( \theta \) degrees for a set of
  711. coordinates \( (x_i,y_i) \) is given by
  712. $$
  713. \begin{align*}
  714. \bar x_i &\leftarrow x_i\cos\theta - y_i\sin\theta,\\
  715. \bar y_i &\leftarrow x_i\sin\theta + y_i\cos\theta\thinspace .
  716. \end{align*}
  717. $$
  718. This rotation is performed around the origin. If we want the figure
  719. to be rotated with respect to a general point \( (x,y) \), we need to
  720. extend the formulas above:
  721. $$
  722. \begin{align*}
  723. \bar x_i &\leftarrow x + (x_i -x)\cos\theta - (y_i -y)\sin\theta,\\
  724. \bar y_i &\leftarrow y + (x_i -x)\sin\theta + (y_i -y)\cos\theta\thinspace .
  725. \end{align*}
  726. $$
  727. The Python implementation in class <code>Curve</code>, assuming that \( \theta \)
  728. is given in degrees and not in radians, becomes
  729. <p>
  730. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  731. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"> <span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">rotate</span>(<span style="color: #008000">self</span>, angle, center):
  732. angle <span style="color: #666666">=</span> radians(angle)
  733. x, y <span style="color: #666666">=</span> center
  734. c <span style="color: #666666">=</span> cos(angle); s <span style="color: #666666">=</span> sin(angle)
  735. xnew <span style="color: #666666">=</span> x <span style="color: #666666">+</span> (<span style="color: #008000">self</span><span style="color: #666666">.</span>x <span style="color: #666666">-</span> x)<span style="color: #666666">*</span>c <span style="color: #666666">-</span> (<span style="color: #008000">self</span><span style="color: #666666">.</span>y <span style="color: #666666">-</span> y)<span style="color: #666666">*</span>s
  736. ynew <span style="color: #666666">=</span> y <span style="color: #666666">+</span> (<span style="color: #008000">self</span><span style="color: #666666">.</span>x <span style="color: #666666">-</span> x)<span style="color: #666666">*</span>s <span style="color: #666666">+</span> (<span style="color: #008000">self</span><span style="color: #666666">.</span>y <span style="color: #666666">-</span> y)<span style="color: #666666">*</span>c
  737. <span style="color: #008000">self</span><span style="color: #666666">.</span>x <span style="color: #666666">=</span> xnew
  738. <span style="color: #008000">self</span><span style="color: #666666">.</span>y <span style="color: #666666">=</span> ynew
  739. </pre></div>
  740. <p>
  741. The <code>rotate</code> method in class <code>Shape</code> follows the principle of the
  742. <code>draw</code>, <code>scale</code>, and <code>translate</code> methods.
  743. <p>
  744. We have already seen the <code>rotate</code> method in action when animating the
  745. rolling wheel at the end of the section <a href="._pysketcher001.html#sketcher:vehicle1:anim">Animation: Rolling the Wheels</a>.
  746. <p>
  747. <!-- begin bottom navigation -->
  748. <table style="width: 100%"><tr><td>
  749. <div style="text-align: left;"><a href="._pysketcher002.html"><img src="http://hplgit.github.io/doconce/bundled/html_images/prev1.png" border=0 alt="&laquo; Previous"></a></div>
  750. </td><td>
  751. </td></tr></table>
  752. <!-- end bottom navigation -->
  753. </p>
  754. <!-- ------------------- end of main content --------------- -->
  755. </body>
  756. </html>