|
|
@@ -46,34 +46,43 @@ Automatically generated HTML file from DocOnce source
|
|
|
3,
|
|
|
'sketcher:vehicle1:anim',
|
|
|
'sketcher:vehicle1:anim'),
|
|
|
- ('Basic shapes', 1, None, '___sec8'),
|
|
|
- ('Axis', 2, None, '___sec9'),
|
|
|
- ('Distance with text', 2, None, '___sec10'),
|
|
|
- ('Rectangle', 2, None, '___sec11'),
|
|
|
- ('Triangle', 2, None, '___sec12'),
|
|
|
- ('Arc', 2, None, '___sec13'),
|
|
|
- ('Spring', 2, None, '___sec14'),
|
|
|
- ('Dashpot', 2, None, '___sec15'),
|
|
|
- ('Wavy', 2, None, '___sec16'),
|
|
|
- ('Stochastic curves', 2, None, '___sec17'),
|
|
|
- ('Inner workings of the Pysketcher tool', 1, None, '___sec18'),
|
|
|
+ ('A simple pendulum',
|
|
|
+ 1,
|
|
|
+ 'sketcher:ex:pendulum',
|
|
|
+ 'sketcher:ex:pendulum'),
|
|
|
+ ('The basic physics sketch',
|
|
|
+ 2,
|
|
|
+ 'sketcher:ex:pendulum:basic',
|
|
|
+ 'sketcher:ex:pendulum:basic'),
|
|
|
+ ('The body diagram', 2, None, '___sec10'),
|
|
|
+ ('Basic shapes', 1, None, '___sec11'),
|
|
|
+ ('Axis', 2, None, '___sec12'),
|
|
|
+ ('Distance with text', 2, None, '___sec13'),
|
|
|
+ ('Rectangle', 2, None, '___sec14'),
|
|
|
+ ('Triangle', 2, None, '___sec15'),
|
|
|
+ ('Arc', 2, None, '___sec16'),
|
|
|
+ ('Spring', 2, None, '___sec17'),
|
|
|
+ ('Dashpot', 2, None, '___sec18'),
|
|
|
+ ('Wavy', 2, None, '___sec19'),
|
|
|
+ ('Stochastic curves', 2, None, '___sec20'),
|
|
|
+ ('Inner workings of the Pysketcher tool', 1, None, '___sec21'),
|
|
|
('Example of classes for geometric objects',
|
|
|
2,
|
|
|
None,
|
|
|
- '___sec19'),
|
|
|
- ('Simple geometric objects', 3, None, '___sec20'),
|
|
|
- ('Class curve', 3, None, '___sec21'),
|
|
|
- ('Compound geometric objects', 3, None, '___sec22'),
|
|
|
- ('Adding functionality via recursion', 2, None, '___sec23'),
|
|
|
- ('Basic principles of recursion', 3, None, '___sec24'),
|
|
|
- ('Explaining recursion', 3, None, '___sec25'),
|
|
|
+ '___sec22'),
|
|
|
+ ('Simple geometric objects', 3, None, '___sec23'),
|
|
|
+ ('Class curve', 3, None, '___sec24'),
|
|
|
+ ('Compound geometric objects', 3, None, '___sec25'),
|
|
|
+ ('Adding functionality via recursion', 2, None, '___sec26'),
|
|
|
+ ('Basic principles of recursion', 3, None, '___sec27'),
|
|
|
+ ('Explaining recursion', 3, None, '___sec28'),
|
|
|
('Scaling, translating, and rotating a figure',
|
|
|
2,
|
|
|
'sketcher:scaling',
|
|
|
'sketcher:scaling'),
|
|
|
- ('Scaling', 3, None, '___sec27'),
|
|
|
- ('Translation', 3, None, '___sec28'),
|
|
|
- ('Rotation', 3, None, '___sec29')]}
|
|
|
+ ('Scaling', 3, None, '___sec30'),
|
|
|
+ ('Translation', 3, None, '___sec31'),
|
|
|
+ ('Rotation', 3, None, '___sec32')]}
|
|
|
end of tocinfo -->
|
|
|
|
|
|
<body>
|
|
|
@@ -119,28 +128,31 @@ MathJax.Hub.Config({
|
|
|
<!-- navigation toc: --> <li><a href="._pysketcher002.html#___sec5" style="font-size: 80%;"> The figure composition as an object hierarchy</a></li>
|
|
|
<!-- navigation toc: --> <li><a href="._pysketcher002.html#___sec6" style="font-size: 80%;"> Animation: translating the vehicle</a></li>
|
|
|
<!-- navigation toc: --> <li><a href="._pysketcher002.html#sketcher:vehicle1:anim" style="font-size: 80%;"> Animation: rolling the wheels</a></li>
|
|
|
- <!-- navigation toc: --> <li><a href="#___sec8" style="font-size: 80%;"><b>Basic shapes</b></a></li>
|
|
|
- <!-- navigation toc: --> <li><a href="#___sec9" style="font-size: 80%;"> Axis</a></li>
|
|
|
- <!-- navigation toc: --> <li><a href="#___sec10" style="font-size: 80%;"> Distance with text</a></li>
|
|
|
- <!-- navigation toc: --> <li><a href="#___sec11" style="font-size: 80%;"> Rectangle</a></li>
|
|
|
- <!-- navigation toc: --> <li><a href="#___sec12" style="font-size: 80%;"> Triangle</a></li>
|
|
|
- <!-- navigation toc: --> <li><a href="#___sec13" style="font-size: 80%;"> Arc</a></li>
|
|
|
- <!-- navigation toc: --> <li><a href="#___sec14" style="font-size: 80%;"> Spring</a></li>
|
|
|
- <!-- navigation toc: --> <li><a href="#___sec15" style="font-size: 80%;"> Dashpot</a></li>
|
|
|
- <!-- navigation toc: --> <li><a href="#___sec16" style="font-size: 80%;"> Wavy</a></li>
|
|
|
- <!-- navigation toc: --> <li><a href="#___sec17" style="font-size: 80%;"> Stochastic curves</a></li>
|
|
|
- <!-- navigation toc: --> <li><a href="._pysketcher004.html#___sec18" style="font-size: 80%;"><b>Inner workings of the Pysketcher tool</b></a></li>
|
|
|
- <!-- navigation toc: --> <li><a href="._pysketcher004.html#___sec19" style="font-size: 80%;"> Example of classes for geometric objects</a></li>
|
|
|
- <!-- navigation toc: --> <li><a href="._pysketcher004.html#___sec20" style="font-size: 80%;"> Simple geometric objects</a></li>
|
|
|
- <!-- navigation toc: --> <li><a href="._pysketcher004.html#___sec21" style="font-size: 80%;"> Class curve</a></li>
|
|
|
- <!-- navigation toc: --> <li><a href="._pysketcher004.html#___sec22" style="font-size: 80%;"> Compound geometric objects</a></li>
|
|
|
- <!-- navigation toc: --> <li><a href="._pysketcher004.html#___sec23" style="font-size: 80%;"> Adding functionality via recursion</a></li>
|
|
|
- <!-- navigation toc: --> <li><a href="._pysketcher004.html#___sec24" style="font-size: 80%;"> Basic principles of recursion</a></li>
|
|
|
- <!-- navigation toc: --> <li><a href="._pysketcher004.html#___sec25" style="font-size: 80%;"> Explaining recursion</a></li>
|
|
|
- <!-- navigation toc: --> <li><a href="._pysketcher004.html#sketcher:scaling" style="font-size: 80%;"> Scaling, translating, and rotating a figure</a></li>
|
|
|
- <!-- navigation toc: --> <li><a href="._pysketcher004.html#___sec27" style="font-size: 80%;"> Scaling</a></li>
|
|
|
- <!-- navigation toc: --> <li><a href="._pysketcher004.html#___sec28" style="font-size: 80%;"> Translation</a></li>
|
|
|
- <!-- navigation toc: --> <li><a href="._pysketcher004.html#___sec29" style="font-size: 80%;"> Rotation</a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="#sketcher:ex:pendulum" style="font-size: 80%;"><b>A simple pendulum</b></a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="#sketcher:ex:pendulum:basic" style="font-size: 80%;"> The basic physics sketch</a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="#___sec10" style="font-size: 80%;"> The body diagram</a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="._pysketcher004.html#___sec11" style="font-size: 80%;"><b>Basic shapes</b></a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="._pysketcher004.html#___sec12" style="font-size: 80%;"> Axis</a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="._pysketcher004.html#___sec13" style="font-size: 80%;"> Distance with text</a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="._pysketcher004.html#___sec14" style="font-size: 80%;"> Rectangle</a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="._pysketcher004.html#___sec15" style="font-size: 80%;"> Triangle</a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="._pysketcher004.html#___sec16" style="font-size: 80%;"> Arc</a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="._pysketcher004.html#___sec17" style="font-size: 80%;"> Spring</a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="._pysketcher004.html#___sec18" style="font-size: 80%;"> Dashpot</a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="._pysketcher004.html#___sec19" style="font-size: 80%;"> Wavy</a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="._pysketcher004.html#___sec20" style="font-size: 80%;"> Stochastic curves</a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="._pysketcher005.html#___sec21" style="font-size: 80%;"><b>Inner workings of the Pysketcher tool</b></a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="._pysketcher005.html#___sec22" style="font-size: 80%;"> Example of classes for geometric objects</a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="._pysketcher005.html#___sec23" style="font-size: 80%;"> Simple geometric objects</a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="._pysketcher005.html#___sec24" style="font-size: 80%;"> Class curve</a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="._pysketcher005.html#___sec25" style="font-size: 80%;"> Compound geometric objects</a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="._pysketcher005.html#___sec26" style="font-size: 80%;"> Adding functionality via recursion</a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="._pysketcher005.html#___sec27" style="font-size: 80%;"> Basic principles of recursion</a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="._pysketcher005.html#___sec28" style="font-size: 80%;"> Explaining recursion</a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="._pysketcher005.html#sketcher:scaling" style="font-size: 80%;"> Scaling, translating, and rotating a figure</a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="._pysketcher005.html#___sec30" style="font-size: 80%;"> Scaling</a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="._pysketcher005.html#___sec31" style="font-size: 80%;"> Translation</a></li>
|
|
|
+ <!-- navigation toc: --> <li><a href="._pysketcher005.html#___sec32" style="font-size: 80%;"> Rotation</a></li>
|
|
|
|
|
|
</ul>
|
|
|
</li>
|
|
|
@@ -156,394 +168,350 @@ MathJax.Hub.Config({
|
|
|
<a name="part0003"></a>
|
|
|
<!-- !split -->
|
|
|
|
|
|
-<h1 id="___sec8">Basic shapes </h1>
|
|
|
+<h1 id="sketcher:ex:pendulum">A simple pendulum</h1>
|
|
|
|
|
|
-<p>
|
|
|
-This section presents many of the basic shapes in Pysketcher:
|
|
|
-<code>Axis</code>, <code>Distance_wText</code>, <code>Rectangle</code>, <code>Triangle</code>, <code>Arc</code>,
|
|
|
-<code>Spring</code>, <code>Dashpot</code>, and <code>Wavy</code>.
|
|
|
-Each shape is demonstrated with a figure and a
|
|
|
-unit test that shows how the figure is constructed in Python code.
|
|
|
-These demos rely heavily on the method <code>draw_dimensions</code> in
|
|
|
-the shape classes, which annotates the basic drawing of the shape
|
|
|
-with the various geometric parameters that govern the shape.
|
|
|
+<h2 id="sketcher:ex:pendulum:basic">The basic physics sketch</h2>
|
|
|
|
|
|
-<h2 id="___sec9">Axis </h2>
|
|
|
+<p>
|
|
|
+We now want to make a sketch of simple pendulum from physics, as shown
|
|
|
+in Figure <a href="#sketcher:ex:pendulum:fig1">8</a>. A suggested work flow is to
|
|
|
+first sketch the figure on a piece of paper and introduce a coordinate
|
|
|
+system. A simple coordinate system is indicated in Figure
|
|
|
+<a href="#sketcher:ex:pendulum:fig1wgrid">9</a>. In a code we introduce variables
|
|
|
+<code>W</code> and <code>H</code> for the width and height of the figure (i.e., extent of
|
|
|
+the coordinate system) and open the program like this:
|
|
|
|
|
|
<p>
|
|
|
-The <code>Axis</code> object gives the possibility draw a single axis to
|
|
|
-notify a coordinate system. Here is an example where we
|
|
|
-draw \( x \) and \( y \) axis of three coordinate systems of different
|
|
|
-rotation:
|
|
|
|
|
|
+<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
|
+<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>
|
|
|
+
|
|
|
+H <span style="color: #666666">=</span> <span style="color: #666666">7.</span>
|
|
|
+W <span style="color: #666666">=</span> <span style="color: #666666">6.</span>
|
|
|
+
|
|
|
+drawing_tool<span style="color: #666666">.</span>set_coordinate_system(xmin<span style="color: #666666">=0</span>, xmax<span style="color: #666666">=</span>W,
|
|
|
+ ymin<span style="color: #666666">=0</span>, ymax<span style="color: #666666">=</span>H,
|
|
|
+ axis<span style="color: #666666">=</span><span style="color: #008000">True</span>)
|
|
|
+drawing_tool<span style="color: #666666">.</span>set_grid(<span style="color: #008000">True</span>)
|
|
|
+drawing_tool<span style="color: #666666">.</span>set_linecolor(<span style="color: #BA2121">'blue'</span>)
|
|
|
+</pre></div>
|
|
|
<p>
|
|
|
-<br />
|
|
|
-<br />
|
|
|
+Note that when the sketch is ready for "production", we will (normally)
|
|
|
+set <code>axis=False</code> to remove the coordinate system and also remove the
|
|
|
+grid, i.e., delete or
|
|
|
+comment out the line <code>drawing_tool.set_grid(True)</code>.
|
|
|
+Also note that we in this example let all lines be blue by default.
|
|
|
|
|
|
<p>
|
|
|
-<center><p><img src="fig-tut/Axis.png" align="bottom" width=500></p></center>
|
|
|
+<center> <!-- figure -->
|
|
|
+<hr class="figure">
|
|
|
+<center><p class="caption">Figure 8: Sketch of a simple pendulum. <div id="sketcher:ex:pendulum:fig1"></div> </p></center>
|
|
|
+<p><img src="fig-tut/pendulum1.png" align="bottom" width=400></p>
|
|
|
+</center>
|
|
|
|
|
|
<p>
|
|
|
-<br />
|
|
|
-<br />
|
|
|
+<center> <!-- figure -->
|
|
|
+<hr class="figure">
|
|
|
+<center><p class="caption">Figure 9: Sketch of a simple pendulum. <div id="sketcher:ex:pendulum:fig1wgrid"></div> </p></center>
|
|
|
+<p><img src="fig-tut/pendulum1_wgrid.png" align="bottom" width=400></p>
|
|
|
+</center>
|
|
|
|
|
|
<p>
|
|
|
-The corresponding code looks like this:
|
|
|
+The next step is to introduce variables for key quantities in the sketch.
|
|
|
+Let <code>L</code> be the length of the pendulum, <code>P</code> the rotation point, and let
|
|
|
+<code>a</code> be the angle the pendulum makes with the vertical (measured in degrees).
|
|
|
+We may set
|
|
|
|
|
|
<p>
|
|
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
|
-<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>():
|
|
|
- drawing_tool<span style="color: #666666">.</span>set_coordinate_system(
|
|
|
- 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>,
|
|
|
- instruction_file<span style="color: #666666">=</span><span style="color: #BA2121">'tmp_Axis.py'</span>)
|
|
|
- <span style="color: #408080; font-style: italic"># Draw normal x and y axis with origin at (7.5, 2)</span>
|
|
|
- <span style="color: #408080; font-style: italic"># in the coordinate system of the sketch: [0,15]x[-7,8]</span>
|
|
|
- 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">'x'</span>, rotation_angle<span style="color: #666666">=0</span>)
|
|
|
- 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">'y'</span>, rotation_angle<span style="color: #666666">=90</span>)
|
|
|
- system <span style="color: #666666">=</span> Composition({<span style="color: #BA2121">'x axis'</span>: x_axis, <span style="color: #BA2121">'y axis'</span>: y_axis})
|
|
|
- system<span style="color: #666666">.</span>draw()
|
|
|
- drawing_tool<span style="color: #666666">.</span>display()
|
|
|
-
|
|
|
- <span style="color: #408080; font-style: italic"># Rotate this system 40 degrees counter clockwise</span>
|
|
|
- <span style="color: #408080; font-style: italic"># and draw it with dashed lines</span>
|
|
|
- system<span style="color: #666666">.</span>set_linestyle(<span style="color: #BA2121">'dashed'</span>)
|
|
|
- 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>))
|
|
|
- system<span style="color: #666666">.</span>draw()
|
|
|
- drawing_tool<span style="color: #666666">.</span>display()
|
|
|
-
|
|
|
- <span style="color: #408080; font-style: italic"># Rotate this system another 220 degrees and show</span>
|
|
|
- <span style="color: #408080; font-style: italic"># with dotted lines</span>
|
|
|
- system<span style="color: #666666">.</span>set_linestyle(<span style="color: #BA2121">'dotted'</span>)
|
|
|
- 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>))
|
|
|
- system<span style="color: #666666">.</span>draw()
|
|
|
- drawing_tool<span style="color: #666666">.</span>display()
|
|
|
-
|
|
|
- drawing_tool<span style="color: #666666">.</span>display(<span style="color: #BA2121">'Axis'</span>)
|
|
|
+<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">L <span style="color: #666666">=</span> <span style="color: #666666">5*</span>H<span style="color: #666666">/7</span> <span style="color: #408080; font-style: italic"># length</span>
|
|
|
+P <span style="color: #666666">=</span> (W<span style="color: #666666">/6</span>, <span style="color: #666666">0.85*</span>H) <span style="color: #408080; font-style: italic"># rotation point</span>
|
|
|
+a <span style="color: #666666">=</span> <span style="color: #666666">40</span> <span style="color: #408080; font-style: italic"># angle</span>
|
|
|
</pre></div>
|
|
|
-
|
|
|
-<h2 id="___sec10">Distance with text </h2>
|
|
|
-
|
|
|
<p>
|
|
|
-The object <code>Distance_wText</code> is used to display an arrow, to indicate
|
|
|
-a distance in a sketch, with an additional text in the middle of the arrow.
|
|
|
+Be careful with integer division if you use Python 2! Fortunately, we
|
|
|
+started out with <code>float</code> objects for <code>W</code> and <code>H</code> so the expressions above
|
|
|
+are safe.
|
|
|
|
|
|
<p>
|
|
|
-The figure
|
|
|
+What kind of objects do we need in this sketch? Looking at
|
|
|
+Figure <a href="#sketcher:ex:pendulum:fig1">8</a> we see that we need
|
|
|
|
|
|
-<p>
|
|
|
-<br />
|
|
|
-<br />
|
|
|
+<ol>
|
|
|
+<li> a vertical, dashed line</li>
|
|
|
+<li> an arc with no text but dashed line to indicate the <em>path</em> of the
|
|
|
+ mass</li>
|
|
|
+<li> an arc with name \( \theta \) to indicate the <em>angle</em></li>
|
|
|
+<li> a line, here called <em>rod</em>, from the rotation point to the mass</li>
|
|
|
+<li> a blue, filled circle representing the <em>mass</em></li>
|
|
|
+<li> a text \( m \) associated with the mass</li>
|
|
|
+<li> an indicator of the pendulum's <em>length</em> \( L \), visualized as
|
|
|
+ a line with two arrows tips and the text \( L \)</li>
|
|
|
+<li> a gravity vector with the text \( g \)</li>
|
|
|
+</ol>
|
|
|
|
|
|
-<p>
|
|
|
-<center><p><img src="fig-tut/Distance_wText.png" align="bottom" width=500></p></center>
|
|
|
+Pysketcher has objects for each of these elements in our sketch.
|
|
|
+We start with the simplest element: the vertical line, going from
|
|
|
+<code>P</code> to <code>P</code> minus the length in \( y \) direction:
|
|
|
|
|
|
<p>
|
|
|
-<br />
|
|
|
-<br />
|
|
|
|
|
|
+<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
|
+<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">vertical <span style="color: #666666">=</span> Line(P, P<span style="color: #666666">-</span>point(<span style="color: #666666">0</span>,L))
|
|
|
+</pre></div>
|
|
|
<p>
|
|
|
-was produced by this code:
|
|
|
+The path of the mass is an arc that can be made by
|
|
|
+Pysketcher's <code>Arc</code> object:
|
|
|
|
|
|
<p>
|
|
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
|
-<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>():
|
|
|
- drawing_tool<span style="color: #666666">.</span>set_coordinate_system(
|
|
|
- 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>,
|
|
|
- axis<span style="color: #666666">=</span><span style="color: #008000">True</span>, instruction_file<span style="color: #666666">=</span><span style="color: #BA2121">'tmp_Distance_wText.py'</span>)
|
|
|
-
|
|
|
- fontsize<span style="color: #666666">=14</span>
|
|
|
- t <span style="color: #666666">=</span> <span style="color: #BA2121">r'$ 2\pi R^2 $'</span> <span style="color: #408080; font-style: italic"># sample text</span>
|
|
|
- examples <span style="color: #666666">=</span> Composition({
|
|
|
- <span style="color: #BA2121">'a0'</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),
|
|
|
- <span style="color: #BA2121">'a6'</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),
|
|
|
- <span style="color: #BA2121">'a1'</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),
|
|
|
- <span style="color: #BA2121">'a2'</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),
|
|
|
- <span style="color: #BA2121">'a3'</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),
|
|
|
- <span style="color: #BA2121">'a4'</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,
|
|
|
- text_spacing<span style="color: #666666">=-1./60</span>),
|
|
|
- <span style="color: #BA2121">'a5'</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,
|
|
|
- text_spacing<span style="color: #666666">=-1./40</span>, alignment<span style="color: #666666">=</span><span style="color: #BA2121">'right'</span>),
|
|
|
- <span style="color: #BA2121">'c1'</span>: Text_wArrow(<span style="color: #BA2121">'text_spacing=-1./60'</span>,
|
|
|
- (<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>),
|
|
|
- fontsize<span style="color: #666666">=10</span>, alignment<span style="color: #666666">=</span><span style="color: #BA2121">'left'</span>),
|
|
|
- <span style="color: #BA2121">'c2'</span>: Text_wArrow(<span style="color: #BA2121">'text_spacing=-1./40, alignment="right"'</span>,
|
|
|
- (<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>),
|
|
|
- fontsize<span style="color: #666666">=10</span>, alignment<span style="color: #666666">=</span><span style="color: #BA2121">'left'</span>),
|
|
|
- })
|
|
|
- examples<span style="color: #666666">.</span>draw()
|
|
|
- drawing_tool<span style="color: #666666">.</span>display(<span style="color: #BA2121">'Distance_wText and text positioning'</span>)
|
|
|
+<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">path <span style="color: #666666">=</span> Arc(P, L, <span style="color: #666666">-90</span>, a)
|
|
|
</pre></div>
|
|
|
<p>
|
|
|
-Note the use of <code>Text_wArrow</code> to write an explaining text with an
|
|
|
-associated arrow, here used to explain how
|
|
|
-the <code>text_spacing</code> and <code>alignment</code> arguments can be used to adjust
|
|
|
-the appearance of the text that goes with the distance arrow.
|
|
|
-
|
|
|
-<h2 id="___sec11">Rectangle </h2>
|
|
|
+The first argument <code>P</code> is the center point, the second is the
|
|
|
+radius (<code>L</code> here), the next arguments is the start angle, here
|
|
|
+it starts at -90 degrees, while the next argument is the angle of
|
|
|
+the arc, here <code>a</code>.
|
|
|
+For the path of the mass, we also need an arc object, but this
|
|
|
+time with an associated text. Pysketcher has a specialized object
|
|
|
+for this purpose, <code>Arc_wText</code>, since placing the text manually can
|
|
|
+be somewhat cumbersome.
|
|
|
|
|
|
<p>
|
|
|
-<center><p><img src="fig-tut/Rectangle.png" align="bottom" width=500></p></center>
|
|
|
|
|
|
+<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
|
+<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">angle <span style="color: #666666">=</span> Arc_wText(<span style="color: #BA2121">r'$\theta$'</span>, P, L<span style="color: #666666">/4</span>, <span style="color: #666666">-90</span>, a, text_spacing<span style="color: #666666">=1/30.</span>)
|
|
|
+</pre></div>
|
|
|
<p>
|
|
|
-<br />
|
|
|
-<br />
|
|
|
+The arguments are as for <code>Arc</code> above, but the first one is the desired
|
|
|
+text. Remember to use a raw string since we want a LaTeX greek letter
|
|
|
+that contains a backslash.
|
|
|
+The <code>text_spacing</code> argument must often be tweaked. It is recommended
|
|
|
+to create only a few objects before rendering the sketch and then
|
|
|
+adjust spacings as one goes along.
|
|
|
|
|
|
<p>
|
|
|
-The above figure can be produced by the following code.
|
|
|
+The rod is simply a line from <code>P</code> to the mass. We can easily
|
|
|
+compute the position of the mass from basic geometry considerations,
|
|
|
+but it is easier and safer to look up this point in other objects
|
|
|
+if it is already computed. The <code>path</code> object stores its start and
|
|
|
+end points, so <code>path.geometric_features()['end']</code> is the end point
|
|
|
+of the path, which is the position of the mass. We can therefore
|
|
|
+create the rod simply as a line from <code>P</code> to this end point:
|
|
|
|
|
|
<p>
|
|
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
|
-<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>():
|
|
|
- L <span style="color: #666666">=</span> <span style="color: #666666">3.0</span>
|
|
|
- W <span style="color: #666666">=</span> <span style="color: #666666">4.0</span>
|
|
|
-
|
|
|
- drawing_tool<span style="color: #666666">.</span>set_coordinate_system(
|
|
|
- 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,
|
|
|
- axis<span style="color: #666666">=</span><span style="color: #008000">True</span>, instruction_file<span style="color: #666666">=</span><span style="color: #BA2121">'tmp_Rectangle.py'</span>)
|
|
|
- drawing_tool<span style="color: #666666">.</span>set_linecolor(<span style="color: #BA2121">'blue'</span>)
|
|
|
- drawing_tool<span style="color: #666666">.</span>set_grid(<span style="color: #008000">True</span>)
|
|
|
-
|
|
|
- xpos <span style="color: #666666">=</span> W<span style="color: #666666">/2</span>
|
|
|
- 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)
|
|
|
- r<span style="color: #666666">.</span>draw()
|
|
|
- r<span style="color: #666666">.</span>draw_dimensions()
|
|
|
- drawing_tool<span style="color: #666666">.</span>display(<span style="color: #BA2121">'Rectangle'</span>)
|
|
|
+<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">mass_pt <span style="color: #666666">=</span> path<span style="color: #666666">.</span>geometric_features()[<span style="color: #BA2121">'end'</span>]
|
|
|
+rod <span style="color: #666666">=</span> Line(P, mass_pt)
|
|
|
</pre></div>
|
|
|
<p>
|
|
|
-Note that the <code>draw_dimension</code> method adds explanation of dimensions and various
|
|
|
-important argument in the construction of a shape. It adapts the annotations
|
|
|
-to the geometry of the current shape.
|
|
|
-
|
|
|
-<h2 id="___sec12">Triangle </h2>
|
|
|
+The mass is a circle filled with color:
|
|
|
|
|
|
<p>
|
|
|
-<center><p><img src="fig-tut/Triangle.png" align="bottom" width=500></p></center>
|
|
|
-
|
|
|
-<p>
|
|
|
-<br />
|
|
|
-<br />
|
|
|
|
|
|
+<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
|
+<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">mass <span style="color: #666666">=</span> Circle(center<span style="color: #666666">=</span>mass_pt, radius<span style="color: #666666">=</span>L<span style="color: #666666">/20.</span>)
|
|
|
+mass<span style="color: #666666">.</span>set_filled_curves(color<span style="color: #666666">=</span><span style="color: #BA2121">'blue'</span>)
|
|
|
+</pre></div>
|
|
|
<p>
|
|
|
-The code below produces the figure.
|
|
|
+To place the \( m \) correctly, we go a small distance in the direction of
|
|
|
+the rod, from the center of the circle. To this end, we need to
|
|
|
+compute the direction. This is easiest done by computing a vector
|
|
|
+from <code>P</code> to the center of the circle and calling <code>unit_vec</code> to make
|
|
|
+a unit vector in this direction:
|
|
|
|
|
|
<p>
|
|
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
|
-<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>():
|
|
|
- L <span style="color: #666666">=</span> <span style="color: #666666">3.0</span>
|
|
|
- W <span style="color: #666666">=</span> <span style="color: #666666">4.0</span>
|
|
|
-
|
|
|
- drawing_tool<span style="color: #666666">.</span>set_coordinate_system(
|
|
|
- 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,
|
|
|
- axis<span style="color: #666666">=</span><span style="color: #008000">True</span>, instruction_file<span style="color: #666666">=</span><span style="color: #BA2121">'tmp_Triangle.py'</span>)
|
|
|
- drawing_tool<span style="color: #666666">.</span>set_linecolor(<span style="color: #BA2121">'blue'</span>)
|
|
|
- drawing_tool<span style="color: #666666">.</span>set_grid(<span style="color: #008000">True</span>)
|
|
|
-
|
|
|
- xpos <span style="color: #666666">=</span> <span style="color: #666666">1</span>
|
|
|
- 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))
|
|
|
- t<span style="color: #666666">.</span>draw()
|
|
|
- t<span style="color: #666666">.</span>draw_dimensions()
|
|
|
- drawing_tool<span style="color: #666666">.</span>display(<span style="color: #BA2121">'Triangle'</span>)
|
|
|
+<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">rod_vec <span style="color: #666666">=</span> rod<span style="color: #666666">.</span>geometric_features()[<span style="color: #BA2121">'end'</span>] <span style="color: #666666">-</span> \
|
|
|
+ rod<span style="color: #666666">.</span>geometric_features()[<span style="color: #BA2121">'start'</span>]
|
|
|
+unit_rod_vec <span style="color: #666666">=</span> unit_vec(rod_vec)
|
|
|
+mass_symbol <span style="color: #666666">=</span> Text(<span style="color: #BA2121">'$m$'</span>, mass_pt <span style="color: #666666">+</span> L<span style="color: #666666">/10*</span>unit_rod_vec)
|
|
|
</pre></div>
|
|
|
<p>
|
|
|
-Here, the <code>draw_dimension</code> method writes the name of the corners at the
|
|
|
-position of the corners, which does not always look nice (the present figure
|
|
|
-is an example). For a high-quality sketch one would add some spacing
|
|
|
-to the location of the p1, p2, and even p3 texts.
|
|
|
+Again, the distance <code>L/10</code> is something one has to experiment with.
|
|
|
|
|
|
-<h2 id="___sec13">Arc </h2>
|
|
|
+<p>
|
|
|
+The next object is the length measure with the text \( L \). Such length
|
|
|
+measures are represented by Pysketcher's <code>Distance_wText</code> object.
|
|
|
+An easy construction is to first place this length measure along the
|
|
|
+rod and then translate it a little distance (<code>L/15</code>) in the
|
|
|
+normal direction of the rod:
|
|
|
|
|
|
<p>
|
|
|
-<center><p><img src="fig-tut/Arc.png" align="bottom" width=400></p></center>
|
|
|
|
|
|
+<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
|
+<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">length <span style="color: #666666">=</span> Distance_wText(P, mass_pt, <span style="color: #BA2121">'$L$'</span>)
|
|
|
+length<span style="color: #666666">.</span>translate(L<span style="color: #666666">/15*</span>point(cos(radians(a)), sin(radians(a))))
|
|
|
+</pre></div>
|
|
|
<p>
|
|
|
-<br />
|
|
|
-<br />
|
|
|
+For this translation we need a unit vector in the normal direction
|
|
|
+of the rod, which is from geometric considerations given by
|
|
|
+\( (\cos a, \sin a) \), when \( a \) is the angle of the pendulum.
|
|
|
|
|
|
<p>
|
|
|
-An arc like the one above is produced by
|
|
|
+The final object is the gravity force vector, which is so common
|
|
|
+in physics sketches that Pysketcher has a ready-made object: <code>Gravity</code>,
|
|
|
|
|
|
<p>
|
|
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
|
-<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>():
|
|
|
- L <span style="color: #666666">=</span> <span style="color: #666666">4.0</span>
|
|
|
- W <span style="color: #666666">=</span> <span style="color: #666666">4.0</span>
|
|
|
-
|
|
|
- drawing_tool<span style="color: #666666">.</span>set_coordinate_system(
|
|
|
- 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,
|
|
|
- axis<span style="color: #666666">=</span><span style="color: #008000">True</span>, instruction_file<span style="color: #666666">=</span><span style="color: #BA2121">'tmp_Arc.py'</span>)
|
|
|
- drawing_tool<span style="color: #666666">.</span>set_linecolor(<span style="color: #BA2121">'blue'</span>)
|
|
|
- drawing_tool<span style="color: #666666">.</span>set_grid(<span style="color: #008000">True</span>)
|
|
|
-
|
|
|
- center <span style="color: #666666">=</span> point(<span style="color: #666666">0</span>,<span style="color: #666666">0</span>)
|
|
|
- radius <span style="color: #666666">=</span> L<span style="color: #666666">/2</span>
|
|
|
- start_angle <span style="color: #666666">=</span> <span style="color: #666666">60</span>
|
|
|
- arc_angle <span style="color: #666666">=</span> <span style="color: #666666">45</span>
|
|
|
- a <span style="color: #666666">=</span> Arc(center, radius, start_angle, arc_angle)
|
|
|
- a<span style="color: #666666">.</span>draw()
|
|
|
-
|
|
|
- R1 <span style="color: #666666">=</span> <span style="color: #666666">1.25*</span>radius
|
|
|
- R2 <span style="color: #666666">=</span> <span style="color: #666666">1.5*</span>radius
|
|
|
- R <span style="color: #666666">=</span> <span style="color: #666666">2*</span>radius
|
|
|
- a<span style="color: #666666">.</span>dimensions <span style="color: #666666">=</span> {
|
|
|
- <span style="color: #BA2121">'start_angle'</span>:
|
|
|
- Arc_wText(
|
|
|
- <span style="color: #BA2121">'start_angle'</span>, center, R1, start_angle<span style="color: #666666">=0</span>,
|
|
|
- arc_angle<span style="color: #666666">=</span>start_angle, text_spacing<span style="color: #666666">=1/10.</span>),
|
|
|
- <span style="color: #BA2121">'arc_angle'</span>:
|
|
|
- Arc_wText(
|
|
|
- <span style="color: #BA2121">'arc_angle'</span>, center, R2, start_angle<span style="color: #666666">=</span>start_angle,
|
|
|
- arc_angle<span style="color: #666666">=</span>arc_angle, text_spacing<span style="color: #666666">=1/20.</span>),
|
|
|
- <span style="color: #BA2121">'r=0'</span>:
|
|
|
- Line(center, center <span style="color: #666666">+</span>
|
|
|
- point(R<span style="color: #666666">*</span>cos(radians(start_angle)),
|
|
|
- R<span style="color: #666666">*</span>sin(radians(start_angle)))),
|
|
|
- <span style="color: #BA2121">'r=start_angle'</span>:
|
|
|
- Line(center, center <span style="color: #666666">+</span>
|
|
|
- point(R<span style="color: #666666">*</span>cos(radians(start_angle<span style="color: #666666">+</span>arc_angle)),
|
|
|
- R<span style="color: #666666">*</span>sin(radians(start_angle<span style="color: #666666">+</span>arc_angle)))),
|
|
|
- <span style="color: #BA2121">'r=start+arc_angle'</span>:
|
|
|
- Line(center, center <span style="color: #666666">+</span>
|
|
|
- point(R, <span style="color: #666666">0</span>))<span style="color: #666666">.</span>set_linestyle(<span style="color: #BA2121">'dashed'</span>),
|
|
|
- <span style="color: #BA2121">'radius'</span>: Distance_wText(center, a(<span style="color: #666666">0</span>), <span style="color: #BA2121">'radius'</span>, text_spacing<span style="color: #666666">=1/40.</span>),
|
|
|
- <span style="color: #BA2121">'center'</span>: Text(<span style="color: #BA2121">'center'</span>, center<span style="color: #666666">-</span>point(radius<span style="color: #666666">/10.</span>, radius<span style="color: #666666">/10.</span>)),
|
|
|
- }
|
|
|
- <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:
|
|
|
- <span style="color: #008000; font-weight: bold">if</span> dimension<span style="color: #666666">.</span>startswith(<span style="color: #BA2121">'r='</span>):
|
|
|
- dim <span style="color: #666666">=</span> a<span style="color: #666666">.</span>dimensions[dimension]
|
|
|
- dim<span style="color: #666666">.</span>set_linestyle(<span style="color: #BA2121">'dashed'</span>)
|
|
|
- dim<span style="color: #666666">.</span>set_linewidth(<span style="color: #666666">1</span>)
|
|
|
- dim<span style="color: #666666">.</span>set_linecolor(<span style="color: #BA2121">'black'</span>)
|
|
|
-
|
|
|
- a<span style="color: #666666">.</span>draw_dimensions()
|
|
|
- drawing_tool<span style="color: #666666">.</span>display(<span style="color: #BA2121">'Arc'</span>)
|
|
|
+<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">gravity <span style="color: #666666">=</span> Gravity(start<span style="color: #666666">=</span>P<span style="color: #666666">+</span>point(<span style="color: #666666">0.8*</span>L,<span style="color: #666666">0</span>), length<span style="color: #666666">=</span>L<span style="color: #666666">/3</span>)
|
|
|
</pre></div>
|
|
|
-
|
|
|
-<h2 id="___sec14">Spring </h2>
|
|
|
-
|
|
|
<p>
|
|
|
-<center><p><img src="fig-tut/Spring.png" align="bottom" width=800></p></center>
|
|
|
+Since blue is the default color for
|
|
|
+lines, we want the dashed lines (<code>vertical</code> and <code>path</code>) to be black
|
|
|
+and dashed with linewidth 1. These properties can be set one by one,
|
|
|
+but we can also make a little helper function:
|
|
|
|
|
|
<p>
|
|
|
-<br />
|
|
|
-<br />
|
|
|
|
|
|
+<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
|
+<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">set_dashed_thin_blackline</span>(<span style="color: #666666">*</span>objects):
|
|
|
+ <span style="color: #BA2121; font-style: italic">"""Set linestyle of an object to dashed, black, width=1."""</span>
|
|
|
+ <span style="color: #008000; font-weight: bold">for</span> obj <span style="color: #AA22FF; font-weight: bold">in</span> objects:
|
|
|
+ obj<span style="color: #666666">.</span>set_linestyle(<span style="color: #BA2121">'dashed'</span>)
|
|
|
+ obj<span style="color: #666666">.</span>set_linecolor(<span style="color: #BA2121">'black'</span>)
|
|
|
+ obj<span style="color: #666666">.</span>set_linewidth(<span style="color: #666666">1</span>)
|
|
|
+
|
|
|
+set_dashed_thin_blackline(vertical, path)
|
|
|
+</pre></div>
|
|
|
<p>
|
|
|
-The code for making these two springs goes like this:
|
|
|
+Now, all objects are in place, so it remains to compose the final
|
|
|
+figure and draw the composition:
|
|
|
|
|
|
<p>
|
|
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
|
-<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>():
|
|
|
- L <span style="color: #666666">=</span> <span style="color: #666666">5.0</span>
|
|
|
- W <span style="color: #666666">=</span> <span style="color: #666666">2.0</span>
|
|
|
-
|
|
|
- drawing_tool<span style="color: #666666">.</span>set_coordinate_system(
|
|
|
- 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,
|
|
|
- axis<span style="color: #666666">=</span><span style="color: #008000">True</span>, instruction_file<span style="color: #666666">=</span><span style="color: #BA2121">'tmp_Spring.py'</span>)
|
|
|
- drawing_tool<span style="color: #666666">.</span>set_linecolor(<span style="color: #BA2121">'blue'</span>)
|
|
|
- drawing_tool<span style="color: #666666">.</span>set_grid(<span style="color: #008000">True</span>)
|
|
|
-
|
|
|
- xpos <span style="color: #666666">=</span> W
|
|
|
- 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>)
|
|
|
- s1_title <span style="color: #666666">=</span> Text(<span style="color: #BA2121">'Default Spring'</span>,
|
|
|
- s1<span style="color: #666666">.</span>geometric_features()[<span style="color: #BA2121">'end'</span>] <span style="color: #666666">+</span> point(<span style="color: #666666">0</span>,L<span style="color: #666666">/10</span>))
|
|
|
- s1<span style="color: #666666">.</span>draw()
|
|
|
- s1_title<span style="color: #666666">.</span>draw()
|
|
|
- <span style="color: #408080; font-style: italic">#s1.draw_dimensions()</span>
|
|
|
- xpos <span style="color: #666666">+=</span> <span style="color: #666666">3*</span>W
|
|
|
- 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>,
|
|
|
- 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>)
|
|
|
- s2<span style="color: #666666">.</span>draw()
|
|
|
- s2<span style="color: #666666">.</span>draw_dimensions()
|
|
|
- drawing_tool<span style="color: #666666">.</span>display(<span style="color: #BA2121">'Spring'</span>)
|
|
|
+<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">fig <span style="color: #666666">=</span> Composition(
|
|
|
+ {<span style="color: #BA2121">'body'</span>: mass, <span style="color: #BA2121">'rod'</span>: rod,
|
|
|
+ <span style="color: #BA2121">'vertical'</span>: vertical, <span style="color: #BA2121">'theta'</span>: angle, <span style="color: #BA2121">'path'</span>: path,
|
|
|
+ <span style="color: #BA2121">'g'</span>: gravity, <span style="color: #BA2121">'L'</span>: length, <span style="color: #BA2121">'m'</span>: mass_symbol})
|
|
|
+
|
|
|
+fig<span style="color: #666666">.</span>draw()
|
|
|
+drawing_tool<span style="color: #666666">.</span>display()
|
|
|
+drawing_tool<span style="color: #666666">.</span>savefig(<span style="color: #BA2121">'pendulum1'</span>)
|
|
|
</pre></div>
|
|
|
|
|
|
-<h2 id="___sec15">Dashpot </h2>
|
|
|
+<h2 id="___sec10">The body diagram </h2>
|
|
|
|
|
|
<p>
|
|
|
-<center><p><img src="fig-tut/Dashpot.png" align="bottom" width=600></p></center>
|
|
|
+Now we want to isolate the mass and draw all the forces that act on it.
|
|
|
+Figure <a href="#sketcher:ex:pendulum:fig2wgrid">10</a> shows the desired result, but
|
|
|
+embedded in the coordinate system.
|
|
|
+We consider three types of forces: the gravity force, the force from the
|
|
|
+rod, and air resistance. The body diagram is key for deriving the
|
|
|
+equation of motion, so it is illustrative to add useful mathematical
|
|
|
+quantities needed in the derivation, such as the unit vectors in polar
|
|
|
+coordinates.
|
|
|
|
|
|
<p>
|
|
|
-<br />
|
|
|
-<br />
|
|
|
+<center> <!-- figure -->
|
|
|
+<hr class="figure">
|
|
|
+<center><p class="caption">Figure 10: Body diagram of a simple pendulum. <div id="sketcher:ex:pendulum:fig2wgrid"></div> </p></center>
|
|
|
+<p><img src="fig-tut/pendulum5_wgrid.png" align="bottom" width=400></p>
|
|
|
+</center>
|
|
|
|
|
|
<p>
|
|
|
-This dashpot is produced by
|
|
|
+We start by listing the objects in the sketch:
|
|
|
+
|
|
|
+<ol>
|
|
|
+<li> a text \( (x_0,y_0) \) representing the rotation point <code>P</code></li>
|
|
|
+<li> unit vector \( \boldsymbol{i}_r \) with text</li>
|
|
|
+<li> unit vector \( \boldsymbol{i}_\theta \) with text</li>
|
|
|
+<li> a dashed vertical line</li>
|
|
|
+<li> a dashed line along the rod</li>
|
|
|
+<li> an arc with text \( \theta \)</li>
|
|
|
+<li> the gravity force with text \( mg \)</li>
|
|
|
+<li> the force in the rod with text \( S \)</li>
|
|
|
+<li> the air resistance force with text \( \sim |v|v \)</li>
|
|
|
+</ol>
|
|
|
+
|
|
|
+The first object, \( (x_0,y_0) \), is simply a plain text where we have
|
|
|
+to experiment with the position. The unit vectors in polar coordinates
|
|
|
+may be drawn using the Pysketcher's <code>Force</code> object since it has an
|
|
|
+arrow with a text. The first three object can then be made as follows:
|
|
|
|
|
|
<p>
|
|
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
|
-<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>():
|
|
|
- L <span style="color: #666666">=</span> <span style="color: #666666">5.0</span>
|
|
|
- W <span style="color: #666666">=</span> <span style="color: #666666">2.0</span>
|
|
|
- xpos <span style="color: #666666">=</span> <span style="color: #666666">0</span>
|
|
|
-
|
|
|
- drawing_tool<span style="color: #666666">.</span>set_coordinate_system(
|
|
|
- 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,
|
|
|
- axis<span style="color: #666666">=</span><span style="color: #008000">True</span>, instruction_file<span style="color: #666666">=</span><span style="color: #BA2121">'tmp_Dashpot.py'</span>)
|
|
|
- drawing_tool<span style="color: #666666">.</span>set_linecolor(<span style="color: #BA2121">'blue'</span>)
|
|
|
- drawing_tool<span style="color: #666666">.</span>set_grid(<span style="color: #008000">True</span>)
|
|
|
-
|
|
|
- <span style="color: #408080; font-style: italic"># Default (simple) dashpot</span>
|
|
|
- xpos <span style="color: #666666">=</span> <span style="color: #666666">1.5</span>
|
|
|
- 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)
|
|
|
- d1_title <span style="color: #666666">=</span> Text(<span style="color: #BA2121">'Dashpot (default)'</span>,
|
|
|
- d1<span style="color: #666666">.</span>geometric_features()[<span style="color: #BA2121">'end'</span>] <span style="color: #666666">+</span> point(<span style="color: #666666">0</span>,L<span style="color: #666666">/10</span>))
|
|
|
- d1<span style="color: #666666">.</span>draw()
|
|
|
- d1_title<span style="color: #666666">.</span>draw()
|
|
|
-
|
|
|
- <span style="color: #408080; font-style: italic"># Dashpot for animation with fixed bar_length, dashpot_length and</span>
|
|
|
- <span style="color: #408080; font-style: italic"># prescribed piston_pos</span>
|
|
|
- xpos <span style="color: #666666">+=</span> <span style="color: #666666">2.5*</span>W
|
|
|
- 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>,
|
|
|
- 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)
|
|
|
- d2<span style="color: #666666">.</span>draw()
|
|
|
- d2<span style="color: #666666">.</span>draw_dimensions()
|
|
|
-
|
|
|
- drawing_tool<span style="color: #666666">.</span>display(<span style="color: #BA2121">'Dashpot'</span>)
|
|
|
+<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">x0y0 <span style="color: #666666">=</span> Text(<span style="color: #BA2121">'$(x_0,y_0)$'</span>, P <span style="color: #666666">+</span> point(<span style="color: #666666">-0.4</span>,<span style="color: #666666">-0.1</span>))
|
|
|
+ir <span style="color: #666666">=</span> Force(P, P <span style="color: #666666">+</span> L<span style="color: #666666">/10*</span>unit_vec(rod_vec),
|
|
|
+ <span style="color: #BA2121">r'$\boldsymbol{i}_r$'</span>, text_pos<span style="color: #666666">=</span><span style="color: #BA2121">'end'</span>,
|
|
|
+ text_spacing<span style="color: #666666">=</span>(<span style="color: #666666">0.015</span>,<span style="color: #666666">0</span>))
|
|
|
+ith <span style="color: #666666">=</span> Force(P, P <span style="color: #666666">+</span> L<span style="color: #666666">/10*</span>unit_vec((<span style="color: #666666">-</span>rod_vec[<span style="color: #666666">1</span>], rod_vec[<span style="color: #666666">0</span>])),
|
|
|
+ <span style="color: #BA2121">r'$\boldsymbol{i}_{\theta}$'</span>, text_pos<span style="color: #666666">=</span><span style="color: #BA2121">'end'</span>,
|
|
|
+ text_spacing<span style="color: #666666">=</span>(<span style="color: #666666">0.02</span>,<span style="color: #666666">0.005</span>))
|
|
|
</pre></div>
|
|
|
-
|
|
|
-<h2 id="___sec16">Wavy </h2>
|
|
|
-
|
|
|
<p>
|
|
|
-Looks strange. Fix x axis.
|
|
|
+Note that tweaking of the position of <code>x0y0</code> use absolute coordinates, so
|
|
|
+if <code>W</code> or <code>H</code> is changed in the beginning of the figure, the tweaked position
|
|
|
+will most likely not look good. A better solution would be to express
|
|
|
+the tweaked displacement <code>point(-0.4,-0.1)</code> in terms of <code>W</code> and <code>H</code>.
|
|
|
+The <code>text_spacing</code> values in the <code>Force</code> objects also use absolute
|
|
|
+coordinates. Very often, this is much more convenient when adjusting
|
|
|
+the objects, and global size parameters like <code>W</code> and <code>H</code> are in practice
|
|
|
+seldom changed.
|
|
|
|
|
|
-<h2 id="___sec17">Stochastic curves </h2>
|
|
|
+<p>
|
|
|
+The vertical, dashed line, the dashed rod, and the arc for \( \theta \)
|
|
|
+are made by
|
|
|
|
|
|
<p>
|
|
|
-The <code>StochasticWavyCurve</code> object offers three precomputed
|
|
|
-graphics that have a random variation:
|
|
|
|
|
|
+<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
|
+<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">rod_start <span style="color: #666666">=</span> rod<span style="color: #666666">.</span>geometric_features()[<span style="color: #BA2121">'start'</span>] <span style="color: #408080; font-style: italic"># Point P</span>
|
|
|
+vertical2 <span style="color: #666666">=</span> Line(rod_start, rod_start <span style="color: #666666">+</span> point(<span style="color: #666666">0</span>,<span style="color: #666666">-</span>L<span style="color: #666666">/3</span>))
|
|
|
+set_dashed_thin_blackline(vertical2)
|
|
|
+set_dashed_thin_blackline(rod)
|
|
|
+angle2 <span style="color: #666666">=</span> Arc_wText(<span style="color: #BA2121">r'$\theta$'</span>, rod_start, L<span style="color: #666666">/6</span>, <span style="color: #666666">-90</span>, a,
|
|
|
+ text_spacing<span style="color: #666666">=1/30.</span>)
|
|
|
+</pre></div>
|
|
|
<p>
|
|
|
-<br />
|
|
|
-<br />
|
|
|
+Note how we reuse the earlier defined object <code>rod</code>.
|
|
|
|
|
|
<p>
|
|
|
-<center><p><img src="fig-tut/StochasticWavyCurve.png" align="bottom" width=600></p></center>
|
|
|
+The forces are constructed as shown below.
|
|
|
|
|
|
<p>
|
|
|
-<br />
|
|
|
-<br />
|
|
|
|
|
|
+<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
|
+<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">mg_force <span style="color: #666666">=</span> Force(mass_pt, mass_pt <span style="color: #666666">+</span> L<span style="color: #666666">/5*</span>point(<span style="color: #666666">0</span>,<span style="color: #666666">-1</span>),
|
|
|
+ <span style="color: #BA2121">'$mg$'</span>, text_pos<span style="color: #666666">=</span><span style="color: #BA2121">'end'</span>)
|
|
|
+rod_force <span style="color: #666666">=</span> Force(mass_pt, mass_pt <span style="color: #666666">-</span> L<span style="color: #666666">/3*</span>unit_vec(rod_vec),
|
|
|
+ <span style="color: #BA2121">'$S$'</span>, text_pos<span style="color: #666666">=</span><span style="color: #BA2121">'end'</span>,
|
|
|
+ text_spacing<span style="color: #666666">=</span>(<span style="color: #666666">0.03</span>, <span style="color: #666666">0.01</span>))
|
|
|
+air_force <span style="color: #666666">=</span> Force(mass_pt, mass_pt <span style="color: #666666">-</span>
|
|
|
+ L<span style="color: #666666">/6*</span>unit_vec((rod_vec[<span style="color: #666666">1</span>], <span style="color: #666666">-</span>rod_vec[<span style="color: #666666">0</span>])),
|
|
|
+ <span style="color: #BA2121">'$\sim|v|v$'</span>, text_pos<span style="color: #666666">=</span><span style="color: #BA2121">'end'</span>,
|
|
|
+ text_spacing<span style="color: #666666">=</span>(<span style="color: #666666">0.04</span>,<span style="color: #666666">0.005</span>))
|
|
|
+</pre></div>
|
|
|
<p>
|
|
|
-The usage is simple. The construction
|
|
|
+All objects are in place, and we can compose a figure to be drawn:
|
|
|
|
|
|
<p>
|
|
|
|
|
|
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
|
|
|
-<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>)
|
|
|
+<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%">body_diagram <span style="color: #666666">=</span> Composition(
|
|
|
+ {<span style="color: #BA2121">'mg'</span>: mg_force, <span style="color: #BA2121">'S'</span>: rod_force, <span style="color: #BA2121">'rod'</span>: rod,
|
|
|
+ <span style="color: #BA2121">'vertical'</span>: vertical2, <span style="color: #BA2121">'theta'</span>: angle2,
|
|
|
+ <span style="color: #BA2121">'body'</span>: mass, <span style="color: #BA2121">'m'</span>: mass_symbol})
|
|
|
+
|
|
|
+body_diagram[<span style="color: #BA2121">'air'</span>] <span style="color: #666666">=</span> air_force
|
|
|
+body_diagram[<span style="color: #BA2121">'ir'</span>] <span style="color: #666666">=</span> ir
|
|
|
+body_diagram[<span style="color: #BA2121">'ith'</span>] <span style="color: #666666">=</span> ith
|
|
|
+body_diagram[<span style="color: #BA2121">'origin'</span>] <span style="color: #666666">=</span> x0y0
|
|
|
</pre></div>
|
|
|
<p>
|
|
|
-picks the second curve (the three are numbered 0, 1, and 2),
|
|
|
-and the first 40% of that curve. In case one desires another extent
|
|
|
-of the axis, one can just scale the coordinates directly as these
|
|
|
-are stored in the arrays <code>curve.x[curve_no]</code> and
|
|
|
-<code>curve.y[curve_no]</code>.
|
|
|
+Here, we exemplify that we can start out with a composition as a
|
|
|
+dictionary, but (as in ordinary Python dictionaries) add new
|
|
|
+elements later when desired.
|
|
|
+
|
|
|
+<p>
|
|
|
+<!-- FIGURE: [fig-tut/pendulum1.png, width=400 frac=0.5] Sketch of a simple pendulum. <div id="sketcher:ex:pendulum:fig2"></div> -->
|
|
|
|
|
|
<p>
|
|
|
<p>
|
|
|
@@ -555,6 +523,7 @@ are stored in the arrays <code>curve.x[curve_no]</code> and
|
|
|
<li><a href="._pysketcher002.html">3</a></li>
|
|
|
<li class="active"><a href="._pysketcher003.html">4</a></li>
|
|
|
<li><a href="._pysketcher004.html">5</a></li>
|
|
|
+ <li><a href="._pysketcher005.html">6</a></li>
|
|
|
<li><a href="._pysketcher004.html">»</a></li>
|
|
|
</ul>
|
|
|
<!-- ------------------- end of main content --------------- -->
|