._pysketcher004.html 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. ('A simple pendulum',
  43. 1,
  44. 'sketcher:ex:pendulum',
  45. 'sketcher:ex:pendulum'),
  46. ('The basic physics sketch',
  47. 2,
  48. 'sketcher:ex:pendulum:basic',
  49. 'sketcher:ex:pendulum:basic'),
  50. ('The body diagram', 2, None, '___sec10'),
  51. ('Animated body diagram',
  52. 2,
  53. 'sketcher:ex:pendulum:anim',
  54. 'sketcher:ex:pendulum:anim'),
  55. ('Function for drawing the body diagram', 3, None, '___sec12'),
  56. ('Equations for the motion and forces', 3, None, '___sec13'),
  57. ('Numerical solution', 3, None, '___sec14'),
  58. ('Animation', 3, None, '___sec15'),
  59. ('Basic shapes', 1, None, '___sec16'),
  60. ('Axis', 2, None, '___sec17'),
  61. ('Distance with text', 2, None, '___sec18'),
  62. ('Rectangle', 2, None, '___sec19'),
  63. ('Triangle', 2, None, '___sec20'),
  64. ('Arc', 2, None, '___sec21'),
  65. ('Spring', 2, None, '___sec22'),
  66. ('Dashpot', 2, None, '___sec23'),
  67. ('Wavy', 2, None, '___sec24'),
  68. ('Stochastic curves', 2, None, '___sec25'),
  69. ('Inner workings of the Pysketcher tool', 1, None, '___sec26'),
  70. ('Example of classes for geometric objects',
  71. 2,
  72. None,
  73. '___sec27'),
  74. ('Simple geometric objects', 3, None, '___sec28'),
  75. ('Class curve', 3, None, '___sec29'),
  76. ('Compound geometric objects', 3, None, '___sec30'),
  77. ('Adding functionality via recursion', 2, None, '___sec31'),
  78. ('Basic principles of recursion', 3, None, '___sec32'),
  79. ('Explaining recursion', 3, None, '___sec33'),
  80. ('Scaling, translating, and rotating a figure',
  81. 2,
  82. 'sketcher:scaling',
  83. 'sketcher:scaling'),
  84. ('Scaling', 3, None, '___sec35'),
  85. ('Translation', 3, None, '___sec36'),
  86. ('Rotation', 3, None, '___sec37')]}
  87. end of tocinfo -->
  88. <body>
  89. <script type="text/x-mathjax-config">
  90. MathJax.Hub.Config({
  91. TeX: {
  92. equationNumbers: { autoNumber: "none" },
  93. extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
  94. }
  95. });
  96. </script>
  97. <script type="text/javascript"
  98. src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
  99. </script>
  100. <!-- newcommands.tex -->
  101. $$
  102. \newcommand{\half}{\frac{1}{2}}
  103. \newcommand{\tp}{\thinspace .}
  104. \newcommand{\rpos}{\boldsymbol{r}}
  105. \newcommand{\ii}{\boldsymbol{i}}
  106. \newcommand{\jj}{\boldsymbol{j}}
  107. \newcommand{\ir}{\boldsymbol{i}_r}
  108. \newcommand{\ith}{\boldsymbol{i}_{\theta}}
  109. $$
  110. <!-- Bootstrap navigation bar -->
  111. <div class="navbar navbar-default navbar-fixed-top">
  112. <div class="navbar-header">
  113. <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
  114. <span class="icon-bar"></span>
  115. <span class="icon-bar"></span>
  116. <span class="icon-bar"></span>
  117. </button>
  118. <a class="navbar-brand" href="pysketcher.html">Using Pysketcher to Create Principal Sketches of Physics Problems</a>
  119. </div>
  120. <div class="navbar-collapse collapse navbar-responsive-collapse">
  121. <ul class="nav navbar-nav navbar-right">
  122. <li class="dropdown">
  123. <a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
  124. <ul class="dropdown-menu">
  125. <!-- navigation toc: --> <li><a href="._pysketcher002.html#___sec0" style="font-size: 80%;"><b>A first glimpse of Pysketcher</b></a></li>
  126. <!-- navigation toc: --> <li><a href="._pysketcher002.html#___sec1" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Basic construction of sketches</a></li>
  127. <!-- navigation toc: --> <li><a href="._pysketcher002.html#___sec2" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Basic drawing</a></li>
  128. <!-- navigation toc: --> <li><a href="._pysketcher002.html#___sec3" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Groups of objects</a></li>
  129. <!-- navigation toc: --> <li><a href="._pysketcher002.html#___sec4" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Changing line styles and colors</a></li>
  130. <!-- navigation toc: --> <li><a href="._pysketcher002.html#___sec5" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;The figure composition as an object hierarchy</a></li>
  131. <!-- navigation toc: --> <li><a href="._pysketcher002.html#___sec6" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Animation: translating the vehicle</a></li>
  132. <!-- navigation toc: --> <li><a href="._pysketcher002.html#sketcher:vehicle1:anim" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Animation: rolling the wheels</a></li>
  133. <!-- navigation toc: --> <li><a href="._pysketcher003.html#sketcher:ex:pendulum" style="font-size: 80%;"><b>A simple pendulum</b></a></li>
  134. <!-- navigation toc: --> <li><a href="._pysketcher003.html#sketcher:ex:pendulum:basic" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The basic physics sketch</a></li>
  135. <!-- navigation toc: --> <li><a href="._pysketcher003.html#___sec10" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;The body diagram</a></li>
  136. <!-- navigation toc: --> <li><a href="._pysketcher003.html#sketcher:ex:pendulum:anim" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Animated body diagram</a></li>
  137. <!-- navigation toc: --> <li><a href="._pysketcher003.html#___sec12" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Function for drawing the body diagram</a></li>
  138. <!-- navigation toc: --> <li><a href="._pysketcher003.html#___sec13" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Equations for the motion and forces</a></li>
  139. <!-- navigation toc: --> <li><a href="._pysketcher003.html#___sec14" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Numerical solution</a></li>
  140. <!-- navigation toc: --> <li><a href="._pysketcher003.html#___sec15" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Animation</a></li>
  141. <!-- navigation toc: --> <li><a href="#___sec16" style="font-size: 80%;"><b>Basic shapes</b></a></li>
  142. <!-- navigation toc: --> <li><a href="#___sec17" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Axis</a></li>
  143. <!-- navigation toc: --> <li><a href="#___sec18" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Distance with text</a></li>
  144. <!-- navigation toc: --> <li><a href="#___sec19" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Rectangle</a></li>
  145. <!-- navigation toc: --> <li><a href="#___sec20" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Triangle</a></li>
  146. <!-- navigation toc: --> <li><a href="#___sec21" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Arc</a></li>
  147. <!-- navigation toc: --> <li><a href="#___sec22" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Spring</a></li>
  148. <!-- navigation toc: --> <li><a href="#___sec23" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Dashpot</a></li>
  149. <!-- navigation toc: --> <li><a href="#___sec24" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Wavy</a></li>
  150. <!-- navigation toc: --> <li><a href="#___sec25" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Stochastic curves</a></li>
  151. <!-- navigation toc: --> <li><a href="._pysketcher005.html#___sec26" style="font-size: 80%;"><b>Inner workings of the Pysketcher tool</b></a></li>
  152. <!-- navigation toc: --> <li><a href="._pysketcher005.html#___sec27" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Example of classes for geometric objects</a></li>
  153. <!-- navigation toc: --> <li><a href="._pysketcher005.html#___sec28" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Simple geometric objects</a></li>
  154. <!-- navigation toc: --> <li><a href="._pysketcher005.html#___sec29" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class curve</a></li>
  155. <!-- navigation toc: --> <li><a href="._pysketcher005.html#___sec30" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Compound geometric objects</a></li>
  156. <!-- navigation toc: --> <li><a href="._pysketcher005.html#___sec31" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Adding functionality via recursion</a></li>
  157. <!-- navigation toc: --> <li><a href="._pysketcher005.html#___sec32" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Basic principles of recursion</a></li>
  158. <!-- navigation toc: --> <li><a href="._pysketcher005.html#___sec33" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Explaining recursion</a></li>
  159. <!-- navigation toc: --> <li><a href="._pysketcher005.html#sketcher:scaling" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;Scaling, translating, and rotating a figure</a></li>
  160. <!-- navigation toc: --> <li><a href="._pysketcher005.html#___sec35" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Scaling</a></li>
  161. <!-- navigation toc: --> <li><a href="._pysketcher005.html#___sec36" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Translation</a></li>
  162. <!-- navigation toc: --> <li><a href="._pysketcher005.html#___sec37" style="font-size: 80%;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Rotation</a></li>
  163. </ul>
  164. </li>
  165. </ul>
  166. </div>
  167. </div>
  168. </div> <!-- end of navigation bar -->
  169. <div class="container">
  170. <p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
  171. <a name="part0004"></a>
  172. <!-- !split -->
  173. <h1 id="___sec16">Basic shapes </h1>
  174. <p>
  175. This section presents many of the basic shapes in Pysketcher:
  176. <code>Axis</code>, <code>Distance_wText</code>, <code>Rectangle</code>, <code>Triangle</code>, <code>Arc</code>,
  177. <code>Spring</code>, <code>Dashpot</code>, and <code>Wavy</code>.
  178. Each shape is demonstrated with a figure and a
  179. unit test that shows how the figure is constructed in Python code.
  180. These demos rely heavily on the method <code>draw_dimensions</code> in
  181. the shape classes, which annotates the basic drawing of the shape
  182. with the various geometric parameters that govern the shape.
  183. <h2 id="___sec17">Axis </h2>
  184. <p>
  185. The <code>Axis</code> object gives the possibility draw a single axis to
  186. notify a coordinate system. Here is an example where we
  187. draw \( x \) and \( y \) axis of three coordinate systems of different
  188. rotation:
  189. <p>
  190. <br />
  191. <br />
  192. <p>
  193. <center><p><img src="fig-tut/Axis.png" align="bottom" width=500></p></center>
  194. <p>
  195. <br />
  196. <br />
  197. <p>
  198. The corresponding code looks like this:
  199. <p>
  200. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  201. <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>():
  202. drawing_tool<span style="color: #666666">.</span>set_coordinate_system(
  203. 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>,
  204. instruction_file<span style="color: #666666">=</span><span style="color: #BA2121">&#39;tmp_Axis.py&#39;</span>)
  205. <span style="color: #408080; font-style: italic"># Draw normal x and y axis with origin at (7.5, 2)</span>
  206. <span style="color: #408080; font-style: italic"># in the coordinate system of the sketch: [0,15]x[-7,8]</span>
  207. 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>)
  208. 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>)
  209. 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})
  210. system<span style="color: #666666">.</span>draw()
  211. drawing_tool<span style="color: #666666">.</span>display()
  212. <span style="color: #408080; font-style: italic"># Rotate this system 40 degrees counter clockwise</span>
  213. <span style="color: #408080; font-style: italic"># and draw it with dashed lines</span>
  214. system<span style="color: #666666">.</span>set_linestyle(<span style="color: #BA2121">&#39;dashed&#39;</span>)
  215. 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>))
  216. system<span style="color: #666666">.</span>draw()
  217. drawing_tool<span style="color: #666666">.</span>display()
  218. <span style="color: #408080; font-style: italic"># Rotate this system another 220 degrees and show</span>
  219. <span style="color: #408080; font-style: italic"># with dotted lines</span>
  220. system<span style="color: #666666">.</span>set_linestyle(<span style="color: #BA2121">&#39;dotted&#39;</span>)
  221. 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>))
  222. system<span style="color: #666666">.</span>draw()
  223. drawing_tool<span style="color: #666666">.</span>display()
  224. drawing_tool<span style="color: #666666">.</span>display(<span style="color: #BA2121">&#39;Axis&#39;</span>)
  225. </pre></div>
  226. <h2 id="___sec18">Distance with text </h2>
  227. <p>
  228. The object <code>Distance_wText</code> is used to display an arrow, to indicate
  229. a distance in a sketch, with an additional text in the middle of the arrow.
  230. <p>
  231. The figure
  232. <p>
  233. <br />
  234. <br />
  235. <p>
  236. <center><p><img src="fig-tut/Distance_wText.png" align="bottom" width=500></p></center>
  237. <p>
  238. <br />
  239. <br />
  240. <p>
  241. was produced by this code:
  242. <p>
  243. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  244. <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>():
  245. drawing_tool<span style="color: #666666">.</span>set_coordinate_system(
  246. 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>,
  247. 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>)
  248. fontsize<span style="color: #666666">=14</span>
  249. 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>
  250. examples <span style="color: #666666">=</span> Composition({
  251. <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),
  252. <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),
  253. <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),
  254. <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),
  255. <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),
  256. <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,
  257. text_spacing<span style="color: #666666">=-1./60</span>),
  258. <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,
  259. text_spacing<span style="color: #666666">=-1./40</span>, alignment<span style="color: #666666">=</span><span style="color: #BA2121">&#39;right&#39;</span>),
  260. <span style="color: #BA2121">&#39;c1&#39;</span>: Text_wArrow(<span style="color: #BA2121">&#39;text_spacing=-1./60&#39;</span>,
  261. (<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>),
  262. fontsize<span style="color: #666666">=10</span>, alignment<span style="color: #666666">=</span><span style="color: #BA2121">&#39;left&#39;</span>),
  263. <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>,
  264. (<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>),
  265. fontsize<span style="color: #666666">=10</span>, alignment<span style="color: #666666">=</span><span style="color: #BA2121">&#39;left&#39;</span>),
  266. })
  267. examples<span style="color: #666666">.</span>draw()
  268. drawing_tool<span style="color: #666666">.</span>display(<span style="color: #BA2121">&#39;Distance_wText and text positioning&#39;</span>)
  269. </pre></div>
  270. <p>
  271. Note the use of <code>Text_wArrow</code> to write an explaining text with an
  272. associated arrow, here used to explain how
  273. the <code>text_spacing</code> and <code>alignment</code> arguments can be used to adjust
  274. the appearance of the text that goes with the distance arrow.
  275. <h2 id="___sec19">Rectangle </h2>
  276. <p>
  277. <center><p><img src="fig-tut/Rectangle.png" align="bottom" width=500></p></center>
  278. <p>
  279. <br />
  280. <br />
  281. <p>
  282. The above figure can be produced by the following code.
  283. <p>
  284. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  285. <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>():
  286. L <span style="color: #666666">=</span> <span style="color: #666666">3.0</span>
  287. W <span style="color: #666666">=</span> <span style="color: #666666">4.0</span>
  288. drawing_tool<span style="color: #666666">.</span>set_coordinate_system(
  289. xmin<span style="color: #666666">=0</span>, xmax<span style="color: #666666">=2*</span>W, ymin<span style="color: #666666">=-</span>L<span style="color: #666666">/2</span>, ymax<span style="color: #666666">=2*</span>L,
  290. 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>)
  291. drawing_tool<span style="color: #666666">.</span>set_linecolor(<span style="color: #BA2121">&#39;blue&#39;</span>)
  292. drawing_tool<span style="color: #666666">.</span>set_grid(<span style="color: #008000">True</span>)
  293. xpos <span style="color: #666666">=</span> W<span style="color: #666666">/2</span>
  294. 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)
  295. r<span style="color: #666666">.</span>draw()
  296. r<span style="color: #666666">.</span>draw_dimensions()
  297. drawing_tool<span style="color: #666666">.</span>display(<span style="color: #BA2121">&#39;Rectangle&#39;</span>)
  298. </pre></div>
  299. <p>
  300. Note that the <code>draw_dimension</code> method adds explanation of dimensions and various
  301. important argument in the construction of a shape. It adapts the annotations
  302. to the geometry of the current shape.
  303. <h2 id="___sec20">Triangle </h2>
  304. <p>
  305. <center><p><img src="fig-tut/Triangle.png" align="bottom" width=500></p></center>
  306. <p>
  307. <br />
  308. <br />
  309. <p>
  310. The code below produces the figure.
  311. <p>
  312. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  313. <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>():
  314. L <span style="color: #666666">=</span> <span style="color: #666666">3.0</span>
  315. W <span style="color: #666666">=</span> <span style="color: #666666">4.0</span>
  316. drawing_tool<span style="color: #666666">.</span>set_coordinate_system(
  317. xmin<span style="color: #666666">=0</span>, xmax<span style="color: #666666">=2*</span>W, ymin<span style="color: #666666">=-</span>L<span style="color: #666666">/2</span>, ymax<span style="color: #666666">=1.2*</span>L,
  318. 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>)
  319. drawing_tool<span style="color: #666666">.</span>set_linecolor(<span style="color: #BA2121">&#39;blue&#39;</span>)
  320. drawing_tool<span style="color: #666666">.</span>set_grid(<span style="color: #008000">True</span>)
  321. xpos <span style="color: #666666">=</span> <span style="color: #666666">1</span>
  322. t <span style="color: #666666">=</span> Triangle(p1<span style="color: #666666">=</span>(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>,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))
  323. t<span style="color: #666666">.</span>draw()
  324. t<span style="color: #666666">.</span>draw_dimensions()
  325. drawing_tool<span style="color: #666666">.</span>display(<span style="color: #BA2121">&#39;Triangle&#39;</span>)
  326. </pre></div>
  327. <p>
  328. Here, the <code>draw_dimension</code> method writes the name of the corners at the
  329. position of the corners, which does not always look nice (the present figure
  330. is an example). For a high-quality sketch one would add some spacing
  331. to the location of the p1, p2, and even p3 texts.
  332. <h2 id="___sec21">Arc </h2>
  333. <p>
  334. <center><p><img src="fig-tut/Arc.png" align="bottom" width=400></p></center>
  335. <p>
  336. <br />
  337. <br />
  338. <p>
  339. An arc like the one above is produced by
  340. <p>
  341. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  342. <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>():
  343. L <span style="color: #666666">=</span> <span style="color: #666666">4.0</span>
  344. W <span style="color: #666666">=</span> <span style="color: #666666">4.0</span>
  345. drawing_tool<span style="color: #666666">.</span>set_coordinate_system(
  346. xmin<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>L<span style="color: #666666">/2</span>, ymax<span style="color: #666666">=1.5*</span>L,
  347. 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>)
  348. drawing_tool<span style="color: #666666">.</span>set_linecolor(<span style="color: #BA2121">&#39;blue&#39;</span>)
  349. drawing_tool<span style="color: #666666">.</span>set_grid(<span style="color: #008000">True</span>)
  350. center <span style="color: #666666">=</span> point(<span style="color: #666666">0</span>,<span style="color: #666666">0</span>)
  351. radius <span style="color: #666666">=</span> L<span style="color: #666666">/2</span>
  352. start_angle <span style="color: #666666">=</span> <span style="color: #666666">60</span>
  353. arc_angle <span style="color: #666666">=</span> <span style="color: #666666">45</span>
  354. a <span style="color: #666666">=</span> Arc(center, radius, start_angle, arc_angle)
  355. a<span style="color: #666666">.</span>draw()
  356. R1 <span style="color: #666666">=</span> <span style="color: #666666">1.25*</span>radius
  357. R2 <span style="color: #666666">=</span> <span style="color: #666666">1.5*</span>radius
  358. R <span style="color: #666666">=</span> <span style="color: #666666">2*</span>radius
  359. a<span style="color: #666666">.</span>dimensions <span style="color: #666666">=</span> {
  360. <span style="color: #BA2121">&#39;start_angle&#39;</span>:
  361. Arc_wText(
  362. <span style="color: #BA2121">&#39;start_angle&#39;</span>, center, R1, start_angle<span style="color: #666666">=0</span>,
  363. arc_angle<span style="color: #666666">=</span>start_angle, text_spacing<span style="color: #666666">=1/10.</span>),
  364. <span style="color: #BA2121">&#39;arc_angle&#39;</span>:
  365. Arc_wText(
  366. <span style="color: #BA2121">&#39;arc_angle&#39;</span>, center, R2, start_angle<span style="color: #666666">=</span>start_angle,
  367. arc_angle<span style="color: #666666">=</span>arc_angle, text_spacing<span style="color: #666666">=1/20.</span>),
  368. <span style="color: #BA2121">&#39;r=0&#39;</span>:
  369. Line(center, center <span style="color: #666666">+</span>
  370. point(R<span style="color: #666666">*</span>cos(radians(start_angle)),
  371. R<span style="color: #666666">*</span>sin(radians(start_angle)))),
  372. <span style="color: #BA2121">&#39;r=start_angle&#39;</span>:
  373. Line(center, center <span style="color: #666666">+</span>
  374. point(R<span style="color: #666666">*</span>cos(radians(start_angle<span style="color: #666666">+</span>arc_angle)),
  375. R<span style="color: #666666">*</span>sin(radians(start_angle<span style="color: #666666">+</span>arc_angle)))),
  376. <span style="color: #BA2121">&#39;r=start+arc_angle&#39;</span>:
  377. Line(center, center <span style="color: #666666">+</span>
  378. point(R, <span style="color: #666666">0</span>))<span style="color: #666666">.</span>set_linestyle(<span style="color: #BA2121">&#39;dashed&#39;</span>),
  379. <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">=1/40.</span>),
  380. <span style="color: #BA2121">&#39;center&#39;</span>: Text(<span style="color: #BA2121">&#39;center&#39;</span>, center<span style="color: #666666">-</span>point(radius<span style="color: #666666">/10.</span>, radius<span style="color: #666666">/10.</span>)),
  381. }
  382. <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:
  383. <span style="color: #008000; font-weight: bold">if</span> dimension<span style="color: #666666">.</span>startswith(<span style="color: #BA2121">&#39;r=&#39;</span>):
  384. dim <span style="color: #666666">=</span> a<span style="color: #666666">.</span>dimensions[dimension]
  385. dim<span style="color: #666666">.</span>set_linestyle(<span style="color: #BA2121">&#39;dashed&#39;</span>)
  386. dim<span style="color: #666666">.</span>set_linewidth(<span style="color: #666666">1</span>)
  387. dim<span style="color: #666666">.</span>set_linecolor(<span style="color: #BA2121">&#39;black&#39;</span>)
  388. a<span style="color: #666666">.</span>draw_dimensions()
  389. drawing_tool<span style="color: #666666">.</span>display(<span style="color: #BA2121">&#39;Arc&#39;</span>)
  390. </pre></div>
  391. <h2 id="___sec22">Spring </h2>
  392. <p>
  393. <center><p><img src="fig-tut/Spring.png" align="bottom" width=800></p></center>
  394. <p>
  395. <br />
  396. <br />
  397. <p>
  398. The code for making these two springs goes like this:
  399. <p>
  400. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  401. <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>():
  402. L <span style="color: #666666">=</span> <span style="color: #666666">5.0</span>
  403. W <span style="color: #666666">=</span> <span style="color: #666666">2.0</span>
  404. drawing_tool<span style="color: #666666">.</span>set_coordinate_system(
  405. xmin<span style="color: #666666">=0</span>, xmax<span style="color: #666666">=7*</span>W, ymin<span style="color: #666666">=-</span>L<span style="color: #666666">/2</span>, ymax<span style="color: #666666">=1.5*</span>L,
  406. 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>)
  407. drawing_tool<span style="color: #666666">.</span>set_linecolor(<span style="color: #BA2121">&#39;blue&#39;</span>)
  408. drawing_tool<span style="color: #666666">.</span>set_grid(<span style="color: #008000">True</span>)
  409. xpos <span style="color: #666666">=</span> W
  410. 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>)
  411. s1_title <span style="color: #666666">=</span> Text(<span style="color: #BA2121">&#39;Default Spring&#39;</span>,
  412. 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>,L<span style="color: #666666">/10</span>))
  413. s1<span style="color: #666666">.</span>draw()
  414. s1_title<span style="color: #666666">.</span>draw()
  415. <span style="color: #408080; font-style: italic">#s1.draw_dimensions()</span>
  416. xpos <span style="color: #666666">+=</span> <span style="color: #666666">3*</span>W
  417. 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>W<span style="color: #666666">/2.</span>,
  418. bar_length<span style="color: #666666">=</span>L<span style="color: #666666">/6.</span>, teeth<span style="color: #666666">=</span><span style="color: #008000">False</span>)
  419. s2<span style="color: #666666">.</span>draw()
  420. s2<span style="color: #666666">.</span>draw_dimensions()
  421. drawing_tool<span style="color: #666666">.</span>display(<span style="color: #BA2121">&#39;Spring&#39;</span>)
  422. </pre></div>
  423. <h2 id="___sec23">Dashpot </h2>
  424. <p>
  425. <center><p><img src="fig-tut/Dashpot.png" align="bottom" width=600></p></center>
  426. <p>
  427. <br />
  428. <br />
  429. <p>
  430. This dashpot is produced by
  431. <p>
  432. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  433. <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>():
  434. L <span style="color: #666666">=</span> <span style="color: #666666">5.0</span>
  435. W <span style="color: #666666">=</span> <span style="color: #666666">2.0</span>
  436. xpos <span style="color: #666666">=</span> <span style="color: #666666">0</span>
  437. drawing_tool<span style="color: #666666">.</span>set_coordinate_system(
  438. 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>L<span style="color: #666666">/2</span>, ymax<span style="color: #666666">=1.5*</span>L,
  439. 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>)
  440. drawing_tool<span style="color: #666666">.</span>set_linecolor(<span style="color: #BA2121">&#39;blue&#39;</span>)
  441. drawing_tool<span style="color: #666666">.</span>set_grid(<span style="color: #008000">True</span>)
  442. <span style="color: #408080; font-style: italic"># Default (simple) dashpot</span>
  443. xpos <span style="color: #666666">=</span> <span style="color: #666666">1.5</span>
  444. 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)
  445. d1_title <span style="color: #666666">=</span> Text(<span style="color: #BA2121">&#39;Dashpot (default)&#39;</span>,
  446. 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>,L<span style="color: #666666">/10</span>))
  447. d1<span style="color: #666666">.</span>draw()
  448. d1_title<span style="color: #666666">.</span>draw()
  449. <span style="color: #408080; font-style: italic"># Dashpot for animation with fixed bar_length, dashpot_length and</span>
  450. <span style="color: #408080; font-style: italic"># prescribed piston_pos</span>
  451. xpos <span style="color: #666666">+=</span> <span style="color: #666666">2.5*</span>W
  452. 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>W<span style="color: #666666">/2</span>,
  453. bar_length<span style="color: #666666">=</span>W, dashpot_length<span style="color: #666666">=</span>L<span style="color: #666666">/2</span>, piston_pos<span style="color: #666666">=2*</span>W)
  454. d2<span style="color: #666666">.</span>draw()
  455. d2<span style="color: #666666">.</span>draw_dimensions()
  456. drawing_tool<span style="color: #666666">.</span>display(<span style="color: #BA2121">&#39;Dashpot&#39;</span>)
  457. </pre></div>
  458. <h2 id="___sec24">Wavy </h2>
  459. <p>
  460. Looks strange. Fix x axis.
  461. <h2 id="___sec25">Stochastic curves </h2>
  462. <p>
  463. The <code>StochasticWavyCurve</code> object offers three precomputed
  464. graphics that have a random variation:
  465. <p>
  466. <br />
  467. <br />
  468. <p>
  469. <center><p><img src="fig-tut/StochasticWavyCurve.png" align="bottom" width=600></p></center>
  470. <p>
  471. <br />
  472. <br />
  473. <p>
  474. The usage is simple. The construction
  475. <p>
  476. <!-- code=python (!bc pycod) typeset with pygments style "default" -->
  477. <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>)
  478. </pre></div>
  479. <p>
  480. picks the second curve (the three are numbered 0, 1, and 2),
  481. and the first 40% of that curve. In case one desires another extent
  482. of the axis, one can just scale the coordinates directly as these
  483. are stored in the arrays <code>curve.x[curve_no]</code> and
  484. <code>curve.y[curve_no]</code>.
  485. <p>
  486. <p>
  487. <!-- navigation buttons at the bottom of the page -->
  488. <ul class="pagination">
  489. <li><a href="._pysketcher003.html">&laquo;</a></li>
  490. <li><a href="._pysketcher000.html">1</a></li>
  491. <li><a href="._pysketcher001.html">2</a></li>
  492. <li><a href="._pysketcher002.html">3</a></li>
  493. <li><a href="._pysketcher003.html">4</a></li>
  494. <li class="active"><a href="._pysketcher004.html">5</a></li>
  495. <li><a href="._pysketcher005.html">6</a></li>
  496. <li><a href="._pysketcher005.html">&raquo;</a></li>
  497. </ul>
  498. <!-- ------------------- end of main content --------------- -->
  499. </div> <!-- end container -->
  500. <!-- include javascript, jQuery *first* -->
  501. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  502. <script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
  503. <!-- Bootstrap footer
  504. <footer>
  505. <a href="http://..."><img width="250" align=right src="http://..."></a>
  506. </footer>
  507. -->
  508. </body>
  509. </html>