._pysketcher003.html 60 KB

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