._pysketcher002.html 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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="#___sec8" style="font-size: 80%;"><b>Basic shapes</b></a></li>
  107. <!-- navigation toc: --> <li><a href="#___sec9" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Axis</a></li>
  108. <!-- navigation toc: --> <li><a href="#___sec10" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Distance with text</a></li>
  109. <!-- navigation toc: --> <li><a href="#___sec11" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Rectangle</a></li>
  110. <!-- navigation toc: --> <li><a href="#___sec12" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Triangle</a></li>
  111. <!-- navigation toc: --> <li><a href="#___sec13" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Arc</a></li>
  112. <!-- navigation toc: --> <li><a href="#___sec14" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Spring</a></li>
  113. <!-- navigation toc: --> <li><a href="#___sec15" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Dashpot</a></li>
  114. <!-- navigation toc: --> <li><a href="#___sec16" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Wavy</a></li>
  115. <!-- navigation toc: --> <li><a href="#___sec17" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Stochastic curves</a></li>
  116. <!-- navigation toc: --> <li><a href="._pysketcher003.html#___sec18" style="font-size: 80%;"><b>Inner workings of the Pysketcher tool</b></a></li>
  117. <!-- navigation toc: --> <li><a href="._pysketcher003.html#___sec19" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Example of classes for geometric objects</a></li>
  118. <!-- navigation toc: --> <li><a href="._pysketcher003.html#___sec20" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Simple geometric objects</a></li>
  119. <!-- navigation toc: --> <li><a href="._pysketcher003.html#___sec21" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class curve</a></li>
  120. <!-- navigation toc: --> <li><a href="._pysketcher003.html#___sec22" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Compound geometric objects</a></li>
  121. <!-- navigation toc: --> <li><a href="._pysketcher003.html#___sec23" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Adding functionality via recursion</a></li>
  122. <!-- navigation toc: --> <li><a href="._pysketcher003.html#___sec24" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Basic principles of recursion</a></li>
  123. <!-- navigation toc: --> <li><a href="._pysketcher003.html#___sec25" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Explaining recursion</a></li>
  124. <!-- navigation toc: --> <li><a href="._pysketcher003.html#sketcher:scaling" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Scaling, translating, and rotating a figure</a></li>
  125. <!-- navigation toc: --> <li><a href="._pysketcher003.html#___sec27" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Scaling</a></li>
  126. <!-- navigation toc: --> <li><a href="._pysketcher003.html#___sec28" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Translation</a></li>
  127. <!-- navigation toc: --> <li><a href="._pysketcher003.html#___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="part0002"></a>
  137. <!-- !split -->
  138. <h1 id="___sec8">Basic shapes </h1>
  139. <p>
  140. This section presents many of the basic shapes in Pysketcher:
  141. <code>Axis</code>, <code>Distance_wText</code>, <code>Rectangle</code>, <code>Triangle</code>, <code>Arc</code>,
  142. <code>Spring</code>, <code>Dashpot</code>, and <code>Wavy</code>.
  143. Each shape is demonstrated with a figure and a
  144. unit test that shows how the figure is constructed in Python code.
  145. These demos rely heavily on the method <code>draw_dimensions</code> in
  146. the shape classes, which annotates the basic drawing of the shape
  147. with the various geometric parameters that govern the shape.
  148. <h2 id="___sec9">Axis </h2>
  149. <p>
  150. The <code>Axis</code> object gives the possibility draw a single axis to
  151. notify a coordinate system. Here is an example where we
  152. draw \( x \) and \( y \) axis of three coordinate systems of different
  153. rotation:
  154. <p>
  155. <br />
  156. <br />
  157. <p>
  158. <center><p><img src="fig-tut/Axis.png" align="bottom" width=500></p></center>
  159. <p>
  160. <br />
  161. <br />
  162. <p>
  163. The corresponding code looks like this:
  164. <p>
  165. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  166. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">test_Axis</span>():
  167. drawing_tool<span style="color: #666666">.</span>set_coordinate_system(
  168. xmin<span style="color: #666666">=0</span>, xmax<span style="color: #666666">=15</span>, ymin<span style="color: #666666">=-7</span>, ymax<span style="color: #666666">=8</span>, axis<span style="color: #666666">=</span><span style="color: #008000">True</span>,
  169. instruction_file<span style="color: #666666">=</span><span style="color: #BA2121">&#39;tmp_Axis.py&#39;</span>)
  170. <span style="color: #408080; font-style: italic"># Draw normal x and y axis with origin at (7.5, 2)</span>
  171. <span style="color: #408080; font-style: italic"># in the coordinate system of the sketch: [0,15]x[-7,8]</span>
  172. x_axis <span style="color: #666666">=</span> Axis((<span style="color: #666666">7.5</span>,<span style="color: #666666">2</span>), <span style="color: #666666">5</span>, <span style="color: #BA2121">&#39;x&#39;</span>, rotation_angle<span style="color: #666666">=0</span>)
  173. y_axis <span style="color: #666666">=</span> Axis((<span style="color: #666666">7.5</span>,<span style="color: #666666">2</span>), <span style="color: #666666">5</span>, <span style="color: #BA2121">&#39;y&#39;</span>, rotation_angle<span style="color: #666666">=90</span>)
  174. system <span style="color: #666666">=</span> Composition({<span style="color: #BA2121">&#39;x axis&#39;</span>: x_axis, <span style="color: #BA2121">&#39;y axis&#39;</span>: y_axis})
  175. system<span style="color: #666666">.</span>draw()
  176. drawing_tool<span style="color: #666666">.</span>display()
  177. <span style="color: #408080; font-style: italic"># Rotate this system 40 degrees counter clockwise</span>
  178. <span style="color: #408080; font-style: italic"># and draw it with dashed lines</span>
  179. system<span style="color: #666666">.</span>set_linestyle(<span style="color: #BA2121">&#39;dashed&#39;</span>)
  180. system<span style="color: #666666">.</span>rotate(<span style="color: #666666">40</span>, (<span style="color: #666666">7.5</span>,<span style="color: #666666">2</span>))
  181. system<span style="color: #666666">.</span>draw()
  182. drawing_tool<span style="color: #666666">.</span>display()
  183. <span style="color: #408080; font-style: italic"># Rotate this system another 220 degrees and show</span>
  184. <span style="color: #408080; font-style: italic"># with dotted lines</span>
  185. system<span style="color: #666666">.</span>set_linestyle(<span style="color: #BA2121">&#39;dotted&#39;</span>)
  186. system<span style="color: #666666">.</span>rotate(<span style="color: #666666">220</span>, (<span style="color: #666666">7.5</span>,<span style="color: #666666">2</span>))
  187. system<span style="color: #666666">.</span>draw()
  188. drawing_tool<span style="color: #666666">.</span>display()
  189. drawing_tool<span style="color: #666666">.</span>display(<span style="color: #BA2121">&#39;Axis&#39;</span>)
  190. </pre></div>
  191. <h2 id="___sec10">Distance with text </h2>
  192. <p>
  193. The object <code>Distance_wText</code> is used to display an arrow, to indicate
  194. a distance in a sketch, with an additional text in the middle of the arrow.
  195. <p>
  196. The figure
  197. <p>
  198. <br />
  199. <br />
  200. <p>
  201. <center><p><img src="fig-tut/Distance_wText.png" align="bottom" width=500></p></center>
  202. <p>
  203. <br />
  204. <br />
  205. <p>
  206. was produced by this code:
  207. <p>
  208. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  209. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">test_Distance_wText</span>():
  210. drawing_tool<span style="color: #666666">.</span>set_coordinate_system(
  211. xmin<span style="color: #666666">=0</span>, xmax<span style="color: #666666">=10</span>, ymin<span style="color: #666666">=0</span>, ymax<span style="color: #666666">=6</span>,
  212. axis<span style="color: #666666">=</span><span style="color: #008000">True</span>, instruction_file<span style="color: #666666">=</span><span style="color: #BA2121">&#39;tmp_Distance_wText.py&#39;</span>)
  213. fontsize<span style="color: #666666">=14</span>
  214. t <span style="color: #666666">=</span> <span style="color: #BA2121">r&#39;$ 2\pi R^2 $&#39;</span> <span style="color: #408080; font-style: italic"># sample text</span>
  215. examples <span style="color: #666666">=</span> Composition({
  216. <span style="color: #BA2121">&#39;a0&#39;</span>: Distance_wText((<span style="color: #666666">4</span>,<span style="color: #666666">5</span>), (<span style="color: #666666">8</span>, <span style="color: #666666">5</span>), t, fontsize),
  217. <span style="color: #BA2121">&#39;a6&#39;</span>: Distance_wText((<span style="color: #666666">4</span>,<span style="color: #666666">5</span>), (<span style="color: #666666">4</span>, <span style="color: #666666">4</span>), t, fontsize),
  218. <span style="color: #BA2121">&#39;a1&#39;</span>: Distance_wText((<span style="color: #666666">0</span>,<span style="color: #666666">2</span>), (<span style="color: #666666">2</span>, <span style="color: #666666">4.5</span>), t, fontsize),
  219. <span style="color: #BA2121">&#39;a2&#39;</span>: Distance_wText((<span style="color: #666666">0</span>,<span style="color: #666666">2</span>), (<span style="color: #666666">2</span>, <span style="color: #666666">0</span>), t, fontsize),
  220. <span style="color: #BA2121">&#39;a3&#39;</span>: Distance_wText((<span style="color: #666666">2</span>,<span style="color: #666666">4.5</span>), (<span style="color: #666666">0</span>, <span style="color: #666666">5.5</span>), t, fontsize),
  221. <span style="color: #BA2121">&#39;a4&#39;</span>: Distance_wText((<span style="color: #666666">8</span>,<span style="color: #666666">4</span>), (<span style="color: #666666">10</span>, <span style="color: #666666">3</span>), t, fontsize,
  222. text_spacing<span style="color: #666666">=</span>old_div(<span style="color: #666666">-1.</span>,<span style="color: #666666">60</span>)),
  223. <span style="color: #BA2121">&#39;a5&#39;</span>: Distance_wText((<span style="color: #666666">8</span>,<span style="color: #666666">2</span>), (<span style="color: #666666">10</span>, <span style="color: #666666">1</span>), t, fontsize,
  224. text_spacing<span style="color: #666666">=</span>old_div(<span style="color: #666666">-1.</span>,<span style="color: #666666">40</span>), alignment<span style="color: #666666">=</span><span style="color: #BA2121">&#39;right&#39;</span>),
  225. <span style="color: #BA2121">&#39;c1&#39;</span>: Text_wArrow(<span style="color: #BA2121">&#39;text_spacing=-1./60&#39;</span>,
  226. (<span style="color: #666666">4</span>, <span style="color: #666666">3.5</span>), (<span style="color: #666666">9</span>, <span style="color: #666666">3.2</span>),
  227. fontsize<span style="color: #666666">=10</span>, alignment<span style="color: #666666">=</span><span style="color: #BA2121">&#39;left&#39;</span>),
  228. <span style="color: #BA2121">&#39;c2&#39;</span>: Text_wArrow(<span style="color: #BA2121">&#39;text_spacing=-1./40, alignment=&quot;right&quot;&#39;</span>,
  229. (<span style="color: #666666">4</span>, <span style="color: #666666">0.5</span>), (<span style="color: #666666">9</span>, <span style="color: #666666">1.2</span>),
  230. fontsize<span style="color: #666666">=10</span>, alignment<span style="color: #666666">=</span><span style="color: #BA2121">&#39;left&#39;</span>),
  231. })
  232. examples<span style="color: #666666">.</span>draw()
  233. drawing_tool<span style="color: #666666">.</span>display(<span style="color: #BA2121">&#39;Distance_wText and text positioning&#39;</span>)
  234. </pre></div>
  235. <p>
  236. Note the use of <code>Text_wArrow</code> to write an explaining text with an
  237. associated arrow, here used to explain how
  238. the <code>text_spacing</code> and <code>alignment</code> arguments can be used to adjust
  239. the appearance of the text that goes with the distance arrow.
  240. <h2 id="___sec11">Rectangle </h2>
  241. <p>
  242. <center><p><img src="fig-tut/Rectangle.png" align="bottom" width=500></p></center>
  243. <p>
  244. <br />
  245. <br />
  246. <p>
  247. The above figure can be produced by the following code.
  248. <p>
  249. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  250. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">test_Rectangle</span>():
  251. L <span style="color: #666666">=</span> <span style="color: #666666">3.0</span>
  252. W <span style="color: #666666">=</span> <span style="color: #666666">4.0</span>
  253. drawing_tool<span style="color: #666666">.</span>set_coordinate_system(
  254. xmin<span style="color: #666666">=0</span>, xmax<span style="color: #666666">=2*</span>W, ymin<span style="color: #666666">=</span>old_div(<span style="color: #666666">-</span>L,<span style="color: #666666">2</span>), ymax<span style="color: #666666">=2*</span>L,
  255. axis<span style="color: #666666">=</span><span style="color: #008000">True</span>, instruction_file<span style="color: #666666">=</span><span style="color: #BA2121">&#39;tmp_Rectangle.py&#39;</span>)
  256. drawing_tool<span style="color: #666666">.</span>set_linecolor(<span style="color: #BA2121">&#39;blue&#39;</span>)
  257. drawing_tool<span style="color: #666666">.</span>set_grid(<span style="color: #008000">True</span>)
  258. xpos <span style="color: #666666">=</span> old_div(W,<span style="color: #666666">2</span>)
  259. r <span style="color: #666666">=</span> Rectangle(lower_left_corner<span style="color: #666666">=</span>(xpos,<span style="color: #666666">0</span>), width<span style="color: #666666">=</span>W, height<span style="color: #666666">=</span>L)
  260. r<span style="color: #666666">.</span>draw()
  261. r<span style="color: #666666">.</span>draw_dimensions()
  262. drawing_tool<span style="color: #666666">.</span>display(<span style="color: #BA2121">&#39;Rectangle&#39;</span>)
  263. </pre></div>
  264. <p>
  265. Note that the <code>draw_dimension</code> method adds explanation of dimensions and various
  266. important argument in the construction of a shape. It adapts the annotations
  267. to the geometry of the current shape.
  268. <h2 id="___sec12">Triangle </h2>
  269. <p>
  270. <center><p><img src="fig-tut/Triangle.png" align="bottom" width=500></p></center>
  271. <p>
  272. <br />
  273. <br />
  274. <p>
  275. The code below produces the figure.
  276. <p>
  277. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  278. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">test_Triangle</span>():
  279. L <span style="color: #666666">=</span> <span style="color: #666666">3.0</span>
  280. W <span style="color: #666666">=</span> <span style="color: #666666">4.0</span>
  281. drawing_tool<span style="color: #666666">.</span>set_coordinate_system(
  282. xmin<span style="color: #666666">=0</span>, xmax<span style="color: #666666">=2*</span>W, ymin<span style="color: #666666">=</span>old_div(<span style="color: #666666">-</span>L,<span style="color: #666666">2</span>), ymax<span style="color: #666666">=1.2*</span>L,
  283. axis<span style="color: #666666">=</span><span style="color: #008000">True</span>, instruction_file<span style="color: #666666">=</span><span style="color: #BA2121">&#39;tmp_Triangle.py&#39;</span>)
  284. drawing_tool<span style="color: #666666">.</span>set_linecolor(<span style="color: #BA2121">&#39;blue&#39;</span>)
  285. drawing_tool<span style="color: #666666">.</span>set_grid(<span style="color: #008000">True</span>)
  286. xpos <span style="color: #666666">=</span> <span style="color: #666666">1</span>
  287. t <span style="color: #666666">=</span> Triangle(p1<span style="color: #666666">=</span>(old_div(W,<span style="color: #666666">2</span>),<span style="color: #666666">0</span>), p2<span style="color: #666666">=</span>(<span style="color: #666666">3*</span>W<span style="color: #666666">/2</span>,old_div(W,<span style="color: #666666">2</span>)), p3<span style="color: #666666">=</span>(<span style="color: #666666">4*</span>W<span style="color: #666666">/5.</span>,L))
  288. t<span style="color: #666666">.</span>draw()
  289. t<span style="color: #666666">.</span>draw_dimensions()
  290. drawing_tool<span style="color: #666666">.</span>display(<span style="color: #BA2121">&#39;Triangle&#39;</span>)
  291. </pre></div>
  292. <p>
  293. Here, the <code>draw_dimension</code> method writes the name of the corners at the
  294. position of the corners, which does not always look nice (the present figure
  295. is an example). For a high-quality sketch one would add some spacing
  296. to the location of the p1, p2, and even p3 texts.
  297. <h2 id="___sec13">Arc </h2>
  298. <p>
  299. <center><p><img src="fig-tut/Arc.png" align="bottom" width=400></p></center>
  300. <p>
  301. <br />
  302. <br />
  303. <p>
  304. An arc like the one above is produced by
  305. <p>
  306. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  307. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">test_Arc</span>():
  308. L <span style="color: #666666">=</span> <span style="color: #666666">4.0</span>
  309. W <span style="color: #666666">=</span> <span style="color: #666666">4.0</span>
  310. drawing_tool<span style="color: #666666">.</span>set_coordinate_system(
  311. xmin<span style="color: #666666">=</span>old_div(<span style="color: #666666">-</span>W,<span style="color: #666666">2</span>), xmax<span style="color: #666666">=</span>W, ymin<span style="color: #666666">=</span>old_div(<span style="color: #666666">-</span>L,<span style="color: #666666">2</span>), ymax<span style="color: #666666">=1.5*</span>L,
  312. axis<span style="color: #666666">=</span><span style="color: #008000">True</span>, instruction_file<span style="color: #666666">=</span><span style="color: #BA2121">&#39;tmp_Arc.py&#39;</span>)
  313. drawing_tool<span style="color: #666666">.</span>set_linecolor(<span style="color: #BA2121">&#39;blue&#39;</span>)
  314. drawing_tool<span style="color: #666666">.</span>set_grid(<span style="color: #008000">True</span>)
  315. center <span style="color: #666666">=</span> point(<span style="color: #666666">0</span>,<span style="color: #666666">0</span>)
  316. radius <span style="color: #666666">=</span> old_div(L,<span style="color: #666666">2</span>)
  317. start_angle <span style="color: #666666">=</span> <span style="color: #666666">60</span>
  318. arc_angle <span style="color: #666666">=</span> <span style="color: #666666">45</span>
  319. a <span style="color: #666666">=</span> Arc(center, radius, start_angle, arc_angle)
  320. a<span style="color: #666666">.</span>draw()
  321. R1 <span style="color: #666666">=</span> <span style="color: #666666">1.25*</span>radius
  322. R2 <span style="color: #666666">=</span> <span style="color: #666666">1.5*</span>radius
  323. R <span style="color: #666666">=</span> <span style="color: #666666">2*</span>radius
  324. a<span style="color: #666666">.</span>dimensions <span style="color: #666666">=</span> {
  325. <span style="color: #BA2121">&#39;start_angle&#39;</span>:
  326. Arc_wText(
  327. <span style="color: #BA2121">&#39;start_angle&#39;</span>, center, R1, start_angle<span style="color: #666666">=0</span>,
  328. arc_angle<span style="color: #666666">=</span>start_angle, text_spacing<span style="color: #666666">=</span>old_div(<span style="color: #666666">1</span>,<span style="color: #666666">10.</span>)),
  329. <span style="color: #BA2121">&#39;arc_angle&#39;</span>:
  330. Arc_wText(
  331. <span style="color: #BA2121">&#39;arc_angle&#39;</span>, center, R2, start_angle<span style="color: #666666">=</span>start_angle,
  332. arc_angle<span style="color: #666666">=</span>arc_angle, text_spacing<span style="color: #666666">=</span>old_div(<span style="color: #666666">1</span>,<span style="color: #666666">20.</span>)),
  333. <span style="color: #BA2121">&#39;r=0&#39;</span>:
  334. Line(center, center <span style="color: #666666">+</span>
  335. point(R<span style="color: #666666">*</span>cos(radians(start_angle)),
  336. R<span style="color: #666666">*</span>sin(radians(start_angle)))),
  337. <span style="color: #BA2121">&#39;r=start_angle&#39;</span>:
  338. Line(center, center <span style="color: #666666">+</span>
  339. point(R<span style="color: #666666">*</span>cos(radians(start_angle<span style="color: #666666">+</span>arc_angle)),
  340. R<span style="color: #666666">*</span>sin(radians(start_angle<span style="color: #666666">+</span>arc_angle)))),
  341. <span style="color: #BA2121">&#39;r=start+arc_angle&#39;</span>:
  342. Line(center, center <span style="color: #666666">+</span>
  343. point(R, <span style="color: #666666">0</span>))<span style="color: #666666">.</span>set_linestyle(<span style="color: #BA2121">&#39;dashed&#39;</span>),
  344. <span style="color: #BA2121">&#39;radius&#39;</span>: Distance_wText(center, a(<span style="color: #666666">0</span>), <span style="color: #BA2121">&#39;radius&#39;</span>, text_spacing<span style="color: #666666">=</span>old_div(<span style="color: #666666">1</span>,<span style="color: #666666">40.</span>)),
  345. <span style="color: #BA2121">&#39;center&#39;</span>: Text(<span style="color: #BA2121">&#39;center&#39;</span>, center<span style="color: #666666">-</span>point(old_div(radius,<span style="color: #666666">10.</span>), old_div(radius,<span style="color: #666666">10.</span>))),
  346. }
  347. <span style="color: #008000; font-weight: bold">for</span> dimension <span style="color: #AA22FF; font-weight: bold">in</span> a<span style="color: #666666">.</span>dimensions:
  348. <span style="color: #008000; font-weight: bold">if</span> dimension<span style="color: #666666">.</span>startswith(<span style="color: #BA2121">&#39;r=&#39;</span>):
  349. dim <span style="color: #666666">=</span> a<span style="color: #666666">.</span>dimensions[dimension]
  350. dim<span style="color: #666666">.</span>set_linestyle(<span style="color: #BA2121">&#39;dashed&#39;</span>)
  351. dim<span style="color: #666666">.</span>set_linewidth(<span style="color: #666666">1</span>)
  352. dim<span style="color: #666666">.</span>set_linecolor(<span style="color: #BA2121">&#39;black&#39;</span>)
  353. a<span style="color: #666666">.</span>draw_dimensions()
  354. drawing_tool<span style="color: #666666">.</span>display(<span style="color: #BA2121">&#39;Arc&#39;</span>)
  355. </pre></div>
  356. <h2 id="___sec14">Spring </h2>
  357. <p>
  358. <center><p><img src="fig-tut/Spring.png" align="bottom" width=800></p></center>
  359. <p>
  360. <br />
  361. <br />
  362. <p>
  363. The code for making these two springs goes like this:
  364. <p>
  365. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  366. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">test_Spring</span>():
  367. L <span style="color: #666666">=</span> <span style="color: #666666">5.0</span>
  368. W <span style="color: #666666">=</span> <span style="color: #666666">2.0</span>
  369. drawing_tool<span style="color: #666666">.</span>set_coordinate_system(
  370. xmin<span style="color: #666666">=0</span>, xmax<span style="color: #666666">=7*</span>W, ymin<span style="color: #666666">=</span>old_div(<span style="color: #666666">-</span>L,<span style="color: #666666">2</span>), ymax<span style="color: #666666">=1.5*</span>L,
  371. axis<span style="color: #666666">=</span><span style="color: #008000">True</span>, instruction_file<span style="color: #666666">=</span><span style="color: #BA2121">&#39;tmp_Spring.py&#39;</span>)
  372. drawing_tool<span style="color: #666666">.</span>set_linecolor(<span style="color: #BA2121">&#39;blue&#39;</span>)
  373. drawing_tool<span style="color: #666666">.</span>set_grid(<span style="color: #008000">True</span>)
  374. xpos <span style="color: #666666">=</span> W
  375. s1 <span style="color: #666666">=</span> Spring((W,<span style="color: #666666">0</span>), L, teeth<span style="color: #666666">=</span><span style="color: #008000">True</span>)
  376. s1_title <span style="color: #666666">=</span> Text(<span style="color: #BA2121">&#39;Default Spring&#39;</span>,
  377. s1<span style="color: #666666">.</span>geometric_features()[<span style="color: #BA2121">&#39;end&#39;</span>] <span style="color: #666666">+</span> point(<span style="color: #666666">0</span>,old_div(L,<span style="color: #666666">10</span>)))
  378. s1<span style="color: #666666">.</span>draw()
  379. s1_title<span style="color: #666666">.</span>draw()
  380. <span style="color: #408080; font-style: italic">#s1.draw_dimensions()</span>
  381. xpos <span style="color: #666666">+=</span> <span style="color: #666666">3*</span>W
  382. s2 <span style="color: #666666">=</span> Spring(start<span style="color: #666666">=</span>(xpos,<span style="color: #666666">0</span>), length<span style="color: #666666">=</span>L, width<span style="color: #666666">=</span>old_div(W,<span style="color: #666666">2.</span>),
  383. bar_length<span style="color: #666666">=</span>old_div(L,<span style="color: #666666">6.</span>), teeth<span style="color: #666666">=</span><span style="color: #008000">False</span>)
  384. s2<span style="color: #666666">.</span>draw()
  385. s2<span style="color: #666666">.</span>draw_dimensions()
  386. drawing_tool<span style="color: #666666">.</span>display(<span style="color: #BA2121">&#39;Spring&#39;</span>)
  387. </pre></div>
  388. <h2 id="___sec15">Dashpot </h2>
  389. <p>
  390. <center><p><img src="fig-tut/Dashpot.png" align="bottom" width=600></p></center>
  391. <p>
  392. <br />
  393. <br />
  394. <p>
  395. This dashpot is produced by
  396. <p>
  397. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  398. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">test_Dashpot</span>():
  399. L <span style="color: #666666">=</span> <span style="color: #666666">5.0</span>
  400. W <span style="color: #666666">=</span> <span style="color: #666666">2.0</span>
  401. xpos <span style="color: #666666">=</span> <span style="color: #666666">0</span>
  402. drawing_tool<span style="color: #666666">.</span>set_coordinate_system(
  403. xmin<span style="color: #666666">=</span>xpos, xmax<span style="color: #666666">=</span>xpos<span style="color: #666666">+5.5*</span>W, ymin<span style="color: #666666">=</span>old_div(<span style="color: #666666">-</span>L,<span style="color: #666666">2</span>), ymax<span style="color: #666666">=1.5*</span>L,
  404. axis<span style="color: #666666">=</span><span style="color: #008000">True</span>, instruction_file<span style="color: #666666">=</span><span style="color: #BA2121">&#39;tmp_Dashpot.py&#39;</span>)
  405. drawing_tool<span style="color: #666666">.</span>set_linecolor(<span style="color: #BA2121">&#39;blue&#39;</span>)
  406. drawing_tool<span style="color: #666666">.</span>set_grid(<span style="color: #008000">True</span>)
  407. <span style="color: #408080; font-style: italic"># Default (simple) dashpot</span>
  408. xpos <span style="color: #666666">=</span> <span style="color: #666666">1.5</span>
  409. d1 <span style="color: #666666">=</span> Dashpot(start<span style="color: #666666">=</span>(xpos,<span style="color: #666666">0</span>), total_length<span style="color: #666666">=</span>L)
  410. d1_title <span style="color: #666666">=</span> Text(<span style="color: #BA2121">&#39;Dashpot (default)&#39;</span>,
  411. d1<span style="color: #666666">.</span>geometric_features()[<span style="color: #BA2121">&#39;end&#39;</span>] <span style="color: #666666">+</span> point(<span style="color: #666666">0</span>,old_div(L,<span style="color: #666666">10</span>)))
  412. d1<span style="color: #666666">.</span>draw()
  413. d1_title<span style="color: #666666">.</span>draw()
  414. <span style="color: #408080; font-style: italic"># Dashpot for animation with fixed bar_length, dashpot_length and</span>
  415. <span style="color: #408080; font-style: italic"># prescribed piston_pos</span>
  416. xpos <span style="color: #666666">+=</span> <span style="color: #666666">2.5*</span>W
  417. d2 <span style="color: #666666">=</span> Dashpot(start<span style="color: #666666">=</span>(xpos,<span style="color: #666666">0</span>), total_length<span style="color: #666666">=1.2*</span>L, width<span style="color: #666666">=</span>old_div(W,<span style="color: #666666">2</span>),
  418. bar_length<span style="color: #666666">=</span>W, dashpot_length<span style="color: #666666">=</span>old_div(L,<span style="color: #666666">2</span>), piston_pos<span style="color: #666666">=2*</span>W)
  419. d2<span style="color: #666666">.</span>draw()
  420. d2<span style="color: #666666">.</span>draw_dimensions()
  421. drawing_tool<span style="color: #666666">.</span>display(<span style="color: #BA2121">&#39;Dashpot&#39;</span>)
  422. </pre></div>
  423. <h2 id="___sec16">Wavy </h2>
  424. <p>
  425. Looks strange. Fix x axis.
  426. <h2 id="___sec17">Stochastic curves </h2>
  427. <p>
  428. The <code>StochasticWavyCurve</code> object offers three precomputed
  429. graphics that have a random variation:
  430. <p>
  431. <br />
  432. <br />
  433. <p>
  434. <center><p><img src="fig-tut/StochasticWavyCurve.png" align="bottom" width=600></p></center>
  435. <p>
  436. <br />
  437. <br />
  438. <p>
  439. The usage is simple. The construction
  440. <p>
  441. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  442. <div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">curve <span style="color: #666666">=</span> StochasticWavyCurve(curve_no<span style="color: #666666">=1</span>, percentage<span style="color: #666666">=40</span>)
  443. </pre></div>
  444. <p>
  445. picks the second curve (the three are numbered 0, 1, and 2),
  446. and the first 40% of that curve. In case one desires another extent
  447. of the axis, one can just scale the coordinates directly as these
  448. are stored in the arrays <code>curve.x[curve_no]</code> and
  449. <code>curve.y[curve_no]</code>.
  450. <p>
  451. <p>
  452. <!-- navigation buttons at the bottom of the page -->
  453. <ul class="pagination">
  454. <li><a href="._pysketcher001.html">&laquo;</a></li>
  455. <li><a href="._pysketcher000.html">1</a></li>
  456. <li><a href="._pysketcher001.html">2</a></li>
  457. <li class="active"><a href="._pysketcher002.html">3</a></li>
  458. <li><a href="._pysketcher003.html">4</a></li>
  459. <li><a href="._pysketcher003.html">&raquo;</a></li>
  460. </ul>
  461. <!-- ------------------- end of main content --------------- -->
  462. </div> <!-- end container -->
  463. <!-- include javascript, jQuery *first* -->
  464. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  465. <script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
  466. <!-- Bootstrap footer
  467. <footer>
  468. <a href="http://..."><img width="250" align=right src="http://..."></a>
  469. </footer>
  470. -->
  471. </body>
  472. </html>