main_sketcher.txt 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440
  1. .. Automatically generated Sphinx-extended reStructuredText file from DocOnce source
  2. (https://github.com/hplgit/doconce/)
  3. .. Sphinx can only have title with less than 63 chars...
  4. .. Document title:
  5. Pysketcher: Create Principal Sketches of Physics Problems
  6. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  7. :Authors: Hans Petter Langtangen
  8. :Date: Jan 22, 2016
  9. .. The below box could be typeset as .. admonition: Attention
  10. but we have decided not to do so since the admon needs a title
  11. (the box formatting is therefore just ignored)
  12. This document is derived from Chapter 9 in the book
  13. `A Primer on Scientific Programming with Python <http://www.amazon.com/Scientific-Programming-Computational-Science-Engineering/dp/3642549586/ref=sr_1_2?s=books&ie=UTF8&qid=1407225588&sr=1-2&keywords=langtangen>`__, by H. P. Langtangen,
  14. 4th edition, Springer, 2014.
  15. *Abstract.* Pysketcher is a Python package which allows principal sketches of
  16. physics and mechanics problems to be realized through short programs
  17. instead of interactive (and potentially tedious and inaccurate)
  18. drawing. Elements of the sketch, such as lines, circles, angles,
  19. forces, coordinate systems, etc., are realized as objects and
  20. collected in hierarchical structures. Parts of the hierarchical
  21. structures can easily change line styles and colors, or be copied,
  22. scaled, translated, and rotated. These features make it
  23. straightforward to move parts of the sketch to create animation,
  24. usually in accordance with the physics of the underlying problem.
  25. Exact dimensioning of the elements in the sketch is trivial to obtain
  26. since distances are specified in computer code.
  27. Pysketcher is easy to learn from a number of examples. Beyond
  28. essential Python programming and a knowledge about mechanics problems,
  29. no further background is required.
  30. .. Task (can be questions): make sketches of physical problems, see fig
  31. .. through user-friendly composition of basic shapes
  32. .. Desired knowledge: plotting curves, basic OO (ch. X.Y, ...)
  33. .. Required knowledge?
  34. .. Learning Goals: these targets the inner workings of pysketcher,
  35. .. which is just a part of this document...
  36. .. !split
  37. A first glimpse of Pysketcher
  38. =============================
  39. Formulation of physical problems makes heavy use of *principal sketches*
  40. such as the one in Figure :ref:`sketcher:fig:inclinedplane`.
  41. This particular sketch illustrates the classical mechanics problem
  42. of a rolling wheel on an inclined plane.
  43. The figure
  44. is made up many individual elements: a rectangle
  45. filled with a pattern (the inclined plane), a hollow circle with color
  46. (the wheel), arrows with labels (the :math:`N` and :math:`Mg` forces, and the :math:`x`
  47. axis), an angle with symbol :math:`\theta`, and a dashed line indicating the
  48. starting location of the wheel.
  49. Drawing software and plotting programs can produce such figures quite
  50. easily in principle, but the amount of details the user needs to
  51. control with the mouse can be substantial. Software more tailored to
  52. producing sketches of this type would work with more convenient
  53. abstractions, such as circle, wall, angle, force arrow, axis, and so
  54. forth. And as soon we start *programming* to construct the figure we
  55. get a range of other powerful tools at disposal. For example, we can
  56. easily translate and rotate parts of the figure and make an animation
  57. that illustrates the physics of the problem.
  58. Programming as a superior alternative to interactive drawing is
  59. the mantra of this section.
  60. .. _sketcher:fig:inclinedplane:
  61. .. figure:: wheel_on_inclined_plane.png
  62. :width: 400
  63. *Sketch of a physics problem*
  64. Basic construction of sketches
  65. ------------------------------
  66. Before attacking real-life sketches as in Figure :ref:`sketcher:fig:inclinedplane`
  67. we focus on the significantly simpler drawing shown
  68. in Figure :ref:`sketcher:fig:vehicle0`. This toy sketch consists of
  69. several elements: two circles, two rectangles, and a "ground" element.
  70. .. _sketcher:fig:vehicle0:
  71. .. figure:: vehicle0_dim.png
  72. :width: 600
  73. *Sketch of a simple figure*
  74. When the sketch is defined in terms of computer code, it is natural to
  75. parameterize geometric features, such as the radius of the wheel (:math:`R`),
  76. the center point of the left wheel (:math:`w_1`), as well as the height (:math:`H`) and
  77. length (:math:`L`) of the main part. The simple vehicle in
  78. Figure :ref:`sketcher:fig:vehicle0` is quickly drawn in almost any interactive
  79. tool. However, if we want to change the radius of the wheels, you need a
  80. sophisticated drawing tool to avoid redrawing the whole figure, while
  81. in computer code this is a matter of changing the :math:`R` parameter and
  82. rerunning the program.
  83. For example, Figure :ref:`sketcher:fig:vehicle0b` shows
  84. a variation of the drawing in
  85. Figure :ref:`sketcher:fig:vehicle0` obtained by just setting
  86. :math:`R=0.5`, :math:`L=5`, :math:`H=2`, and :math:`R=2`. Being able
  87. to quickly change geometric sizes is key to many problem settings in
  88. physics and engineering, but then a program must define the geometry.
  89. .. _sketcher:fig:vehicle0b:
  90. .. figure:: vehicle_v2.png
  91. :width: 500
  92. *Redrawing a figure with other geometric parameters*
  93. Basic drawing
  94. ~~~~~~~~~~~~~
  95. A typical program creating these five elements is shown next.
  96. After importing the ``pysketcher`` package, the first task is always to
  97. define a coordinate system:
  98. .. code-block:: python
  99. from pysketcher import *
  100. drawing_tool.set_coordinate_system(
  101. xmin=0, xmax=10, ymin=-1, ymax=8)
  102. Instead of working with lengths expressed by specific numbers it is
  103. highly recommended to use variables to parameterize lengths as
  104. this makes it easier to change dimensions later.
  105. Here we introduce some key lengths for the radius of the wheels,
  106. distance between the wheels, etc.:
  107. .. code-block:: python
  108. R = 1 # radius of wheel
  109. L = 4 # distance between wheels
  110. H = 2 # height of vehicle body
  111. w_1 = 5 # position of front wheel
  112. drawing_tool.set_coordinate_system(xmin=0, xmax=w_1 + 2*L + 3*R,
  113. ymin=-1, ymax=2*R + 3*H)
  114. With the drawing area in place we can make the first ``Circle`` object
  115. in an intuitive fashion:
  116. .. code-block:: python
  117. wheel1 = Circle(center=(w_1, R), radius=R)
  118. to change dimensions later.
  119. To translate the geometric information about the ``wheel1`` object to
  120. instructions for the plotting engine (in this case Matplotlib), one calls the
  121. ``wheel1.draw()``. To display all drawn objects, one issues
  122. ``drawing_tool.display()``. The typical steps are hence:
  123. .. code-block:: python
  124. wheel1 = Circle(center=(w_1, R), radius=R)
  125. wheel1.draw()
  126. # Define other objects and call their draw() methods
  127. drawing_tool.display()
  128. drawing_tool.savefig('tmp.png') # store picture
  129. The next wheel can be made by taking a copy of ``wheel1`` and
  130. translating the object to the right according to a
  131. displacement vector :math:`(L,0)`:
  132. .. code-block:: python
  133. wheel2 = wheel1.copy()
  134. wheel2.translate((L,0))
  135. The two rectangles are also made in an intuitive way:
  136. .. code-block:: python
  137. under = Rectangle(lower_left_corner=(w_1-2*R, 2*R),
  138. width=2*R + L + 2*R, height=H)
  139. over = Rectangle(lower_left_corner=(w_1, 2*R + H),
  140. width=2.5*R, height=1.25*H)
  141. Groups of objects
  142. ~~~~~~~~~~~~~~~~~
  143. Instead of calling the ``draw`` method of every object, we can
  144. group objects and call ``draw``, or perform other operations, for
  145. the whole group. For example, we may collect the two wheels
  146. in a ``wheels`` group and the ``over`` and ``under`` rectangles
  147. in a ``body`` group. The whole vehicle is a composition
  148. of its ``wheels`` and ``body`` groups. The code goes like
  149. .. code-block:: python
  150. wheels = Composition({'wheel1': wheel1, 'wheel2': wheel2})
  151. body = Composition({'under': under, 'over': over})
  152. vehicle = Composition({'wheels': wheels, 'body': body})
  153. The ground is illustrated by an object of type ``Wall``,
  154. mostly used to indicate walls in sketches of mechanical systems.
  155. A ``Wall`` takes the ``x`` and ``y`` coordinates of some curve,
  156. and a ``thickness`` parameter, and creates a thick curve filled
  157. with a simple pattern. In this case the curve is just a flat
  158. line so the construction is made of two points on the
  159. ground line (:math:`(w_1-L,0)` and :math:`(w_1+3L,0)`):
  160. .. code-block:: python
  161. ground = Wall(x=[w_1 - L, w_1 + 3*L], y=[0, 0], thickness=-0.3*R)
  162. The negative thickness makes the pattern-filled rectangle appear below
  163. the defined line, otherwise it appears above.
  164. We may now collect all the objects in a "top" object that contains
  165. the whole figure:
  166. .. code-block:: python
  167. fig = Composition({'vehicle': vehicle, 'ground': ground})
  168. fig.draw() # send all figures to plotting backend
  169. drawing_tool.display()
  170. drawing_tool.savefig('tmp.png')
  171. The ``fig.draw()`` call will visit
  172. all subgroups, their subgroups,
  173. and so forth in the hierarchical tree structure of
  174. figure elements,
  175. and call ``draw`` for every object.
  176. Changing line styles and colors
  177. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  178. Controlling the line style, line color, and line width is
  179. fundamental when designing figures. The ``pysketcher``
  180. package allows the user to control such properties in
  181. single objects, but also set global properties that are
  182. used if the object has no particular specification of
  183. the properties. Setting the global properties are done like
  184. .. code-block:: python
  185. drawing_tool.set_linestyle('dashed')
  186. drawing_tool.set_linecolor('black')
  187. drawing_tool.set_linewidth(4)
  188. At the object level the properties are specified in a similar
  189. way:
  190. .. code-block:: python
  191. wheels.set_linestyle('solid')
  192. wheels.set_linecolor('red')
  193. and so on.
  194. Geometric figures can be specified as *filled*, either with a color or with a
  195. special visual pattern:
  196. .. code-block:: python
  197. # Set filling of all curves
  198. drawing_tool.set_filled_curves(color='blue', pattern='/')
  199. # Turn off filling of all curves
  200. drawing_tool.set_filled_curves(False)
  201. # Fill the wheel with red color
  202. wheel1.set_filled_curves('red')
  203. .. `<http://packages.python.org/ete2/>`_ for visualizing tree structures!
  204. The figure composition as an object hierarchy
  205. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  206. The composition of objects making up the figure
  207. is hierarchical, similar to a family, where
  208. each object has a parent and a number of children. Do a
  209. ``print fig`` to display the relations:
  210. .. code-block:: text
  211. ground
  212. wall
  213. vehicle
  214. body
  215. over
  216. rectangle
  217. under
  218. rectangle
  219. wheels
  220. wheel1
  221. arc
  222. wheel2
  223. arc
  224. The indentation reflects how deep down in the hierarchy (family)
  225. we are.
  226. This output is to be interpreted as follows:
  227. * ``fig`` contains two objects, ``ground`` and ``vehicle``
  228. * ``ground`` contains an object ``wall``
  229. * ``vehicle`` contains two objects, ``body`` and ``wheels``
  230. * ``body`` contains two objects, ``over`` and ``under``
  231. * ``wheels`` contains two objects, ``wheel1`` and ``wheel2``
  232. In this listing there are also objects not defined by the
  233. programmer: ``rectangle`` and ``arc``. These are of type ``Curve``
  234. and automatically generated by the classes ``Rectangle`` and ``Circle``.
  235. More detailed information can be printed by
  236. .. code-block:: python
  237. print fig.show_hierarchy('std')
  238. yielding the output
  239. .. code-block:: text
  240. ground (Wall):
  241. wall (Curve): 4 coords fillcolor='white' fillpattern='/'
  242. vehicle (Composition):
  243. body (Composition):
  244. over (Rectangle):
  245. rectangle (Curve): 5 coords
  246. under (Rectangle):
  247. rectangle (Curve): 5 coords
  248. wheels (Composition):
  249. wheel1 (Circle):
  250. arc (Curve): 181 coords
  251. wheel2 (Circle):
  252. arc (Curve): 181 coords
  253. Here we can see the class type for each figure object, how many
  254. coordinates that are involved in basic figures (``Curve`` objects), and
  255. special settings of the basic figure (fillcolor, line types, etc.).
  256. For example, ``wheel2`` is a ``Circle`` object consisting of an ``arc``,
  257. which is a ``Curve`` object consisting of 181 coordinates (the
  258. points needed to draw a smooth circle). The ``Curve`` objects are the
  259. only objects that really holds specific coordinates to be drawn.
  260. The other object types are just compositions used to group
  261. parts of the complete figure.
  262. One can also get a graphical overview of the hierarchy of figure objects
  263. that build up a particular figure ``fig``.
  264. Just call ``fig.graphviz_dot('fig')`` to produce a file ``fig.dot`` in
  265. the *dot format*. This file contains relations between parent and
  266. child objects in the figure and can be turned into an image,
  267. as in Figure :ref:`sketcher:fig:vehicle0:hier1`, by
  268. running the ``dot`` program:
  269. .. code-block:: text
  270. Terminal> dot -Tpng -o fig.png fig.dot
  271. .. _sketcher:fig:vehicle0:hier1:
  272. .. figure:: vehicle0_hier1.png
  273. :width: 500
  274. *Hierarchical relation between figure objects*
  275. The call ``fig.graphviz_dot('fig', classname=True)`` makes a ``fig.dot`` file
  276. where the class type of each object is also visible, see
  277. Figure :ref:`sketcher:fig:vehicle0:hier2`. The ability to write out the
  278. object hierarchy or view it graphically can be of great help when
  279. working with complex figures that involve layers of subfigures.
  280. .. _sketcher:fig:vehicle0:hier2:
  281. .. figure:: Vehicle0_hier2.png
  282. :width: 500
  283. *Hierarchical relation between figure objects, including their class names*
  284. Any of the objects can in the program be reached through their names, e.g.,
  285. .. code-block:: python
  286. fig['vehicle']
  287. fig['vehicle']['wheels']
  288. fig['vehicle']['wheels']['wheel2']
  289. fig['vehicle']['wheels']['wheel2']['arc']
  290. fig['vehicle']['wheels']['wheel2']['arc'].x # x coords
  291. fig['vehicle']['wheels']['wheel2']['arc'].y # y coords
  292. fig['vehicle']['wheels']['wheel2']['arc'].linestyle
  293. fig['vehicle']['wheels']['wheel2']['arc'].linetype
  294. Grabbing a part of the figure this way is handy for
  295. changing properties of that part, for example, colors, line styles
  296. (see Figure :ref:`sketcher:fig:vehicle0:v2`):
  297. .. code-block:: python
  298. fig['vehicle']['wheels'].set_filled_curves('blue')
  299. fig['vehicle']['wheels'].set_linewidth(6)
  300. fig['vehicle']['wheels'].set_linecolor('black')
  301. fig['vehicle']['body']['under'].set_filled_curves('red')
  302. fig['vehicle']['body']['over'].set_filled_curves(pattern='/')
  303. fig['vehicle']['body']['over'].set_linewidth(14)
  304. fig['vehicle']['body']['over']['rectangle'].linewidth = 4
  305. The last line accesses the ``Curve`` object directly, while the line above,
  306. accesses the ``Rectangle`` object, which will then set the linewidth of
  307. its ``Curve`` object, and other objects if it had any.
  308. The result of the actions above is shown in Figure :ref:`sketcher:fig:vehicle0:v2`.
  309. .. _sketcher:fig:vehicle0:v2:
  310. .. figure:: vehicle0.png
  311. :width: 700
  312. *Left: Basic line-based drawing. Right: Thicker lines and filled parts*
  313. We can also change position of parts of the figure and thereby make
  314. animations, as shown next.
  315. Animation: translating the vehicle
  316. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  317. Can we make our little vehicle roll? A first attempt will be to
  318. fake rolling by just displacing all parts of the vehicle.
  319. The relevant parts constitute the ``fig['vehicle']`` object.
  320. This part of the figure can be translated, rotated, and scaled.
  321. A translation along the ground means a translation in :math:`x` direction,
  322. say a length :math:`L` to the right:
  323. .. code-block:: python
  324. fig['vehicle'].translate((L,0))
  325. You need to erase, draw, and display to see the movement:
  326. .. code-block:: python
  327. drawing_tool.erase()
  328. fig.draw()
  329. drawing_tool.display()
  330. Without erasing, the old drawing of the vehicle will remain in
  331. the figure so you get two vehicles. Without ``fig.draw()`` the
  332. new coordinates of the vehicle will not be communicated to
  333. the drawing tool, and without calling display the updated
  334. drawing will not be visible.
  335. A figure that moves in time is conveniently realized by the
  336. function ``animate``:
  337. .. code-block:: python
  338. animate(fig, tp, action)
  339. Here, ``fig`` is the entire figure, ``tp`` is an array of
  340. time points, and ``action`` is a user-specified function that changes
  341. ``fig`` at a specific time point. Typically, ``action`` will move
  342. parts of ``fig``.
  343. In the present case we can define the movement through a velocity
  344. function ``v(t)`` and displace the figure ``v(t)*dt`` for small time
  345. intervals ``dt``. A possible velocity function is
  346. .. code-block:: python
  347. def v(t):
  348. return -8*R*t*(1 - t/(2*R))
  349. Our action function for horizontal displacements ``v(t)*dt`` becomes
  350. .. code-block:: python
  351. def move(t, fig):
  352. x_displacement = dt*v(t)
  353. fig['vehicle'].translate((x_displacement, 0))
  354. Since our velocity is negative for :math:`t\in [0,2R]` the displacement is
  355. to the left.
  356. The ``animate`` function will for each time point ``t`` in ``tp`` erase
  357. the drawing, call ``action(t, fig)``, and show the new figure by
  358. ``fig.draw()`` and ``drawing_tool.display()``.
  359. Here we choose a resolution of the animation corresponding to
  360. 25 time points in the time interval :math:`[0,2R]`:
  361. .. code-block:: python
  362. import numpy
  363. tp = numpy.linspace(0, 2*R, 25)
  364. dt = tp[1] - tp[0] # time step
  365. animate(fig, tp, move, pause_per_frame=0.2)
  366. The ``pause_per_frame`` adds a pause, here 0.2 seconds, between
  367. each frame in the animation.
  368. We can also ask ``animate`` to store each frame in a file:
  369. .. code-block:: python
  370. files = animate(fig, tp, move_vehicle, moviefiles=True,
  371. pause_per_frame=0.2)
  372. The ``files`` variable, here ``'tmp_frame_%04d.png'``,
  373. is the printf-specification used to generate the individual
  374. plot files. We can use this specification to make a video
  375. file via ``ffmpeg`` (or ``avconv`` on Debian-based Linux systems such
  376. as Ubuntu). Videos in the Flash and WebM formats can be created
  377. by
  378. .. code-block:: text
  379. Terminal> ffmpeg -r 12 -i tmp_frame_%04d.png -vcodec flv mov.flv
  380. Terminal> ffmpeg -r 12 -i tmp_frame_%04d.png -vcodec libvpx mov.webm
  381. An animated GIF movie can also be made using the ``convert`` program
  382. from the ImageMagick software suite:
  383. .. code-block:: text
  384. Terminal> convert -delay 20 tmp_frame*.png mov.gif
  385. Terminal> animate mov.gif # play movie
  386. The delay between frames, in units of 1/100 s,
  387. governs the speed of the movie.
  388. To play the animated GIF file in a web page, simply insert
  389. ``<img src="mov.gif">`` in the HTML code.
  390. The individual PNG frames can be directly played in a web
  391. browser by running
  392. .. code-block:: text
  393. Terminal> scitools movie output_file=mov.html fps=5 tmp_frame*
  394. or calling
  395. .. code-block:: python
  396. from scitools.std import movie
  397. movie(files, encoder='html', output_file='mov.html')
  398. in Python. Load the resulting file ``mov.html`` into a web browser
  399. to play the movie.
  400. Try to run `vehicle0.py <http://tinyurl.com/ot733jn/vehicle0.py>`__ and
  401. then load ``mov.html`` into a browser, or play one of the ``mov.*``
  402. video files. Alternatively, you can view a ready-made `movie <http://tinyurl.com/oou9lp7/mov-tut/vehicle0.html>`__.
  403. .. _sketcher:vehicle1:anim:
  404. Animation: rolling the wheels
  405. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  406. It is time to show rolling wheels. To this end, we add spokes to the
  407. wheels, formed by two crossing lines, see Figure :ref:`sketcher:fig:vehicle1`.
  408. The construction of the wheels will now involve a circle and two lines:
  409. .. code-block:: python
  410. wheel1 = Composition({
  411. 'wheel': Circle(center=(w_1, R), radius=R),
  412. 'cross': Composition({'cross1': Line((w_1,0), (w_1,2*R)),
  413. 'cross2': Line((w_1-R,R), (w_1+R,R))})})
  414. wheel2 = wheel1.copy()
  415. wheel2.translate((L,0))
  416. Observe that ``wheel1.copy()`` copies all the objects that make
  417. up the first wheel, and ``wheel2.translate`` translates all
  418. the copied objects.
  419. .. _sketcher:fig:vehicle1:
  420. .. figure:: vehicle1.png
  421. :width: 400
  422. *Wheels with spokes to illustrate rolling*
  423. The ``move`` function now needs to displace all the objects in the
  424. entire vehicle and also rotate the ``cross1`` and ``cross2``
  425. objects in both wheels.
  426. The rotation angle follows from the fact that the arc length
  427. of a rolling wheel equals the displacement of the center of
  428. the wheel, leading to a rotation angle
  429. .. code-block:: python
  430. angle = - x_displacement/R
  431. With ``w_1`` tracking the :math:`x` coordinate of the center
  432. of the front wheel, we can rotate that wheel by
  433. .. code-block:: python
  434. w1 = fig['vehicle']['wheels']['wheel1']
  435. from math import degrees
  436. w1.rotate(degrees(angle), center=(w_1, R))
  437. The ``rotate`` function takes two parameters: the rotation angle
  438. (in degrees) and the center point of the rotation, which is the
  439. center of the wheel in this case. The other wheel is rotated by
  440. .. code-block:: python
  441. w2 = fig['vehicle']['wheels']['wheel2']
  442. w2.rotate(degrees(angle), center=(w_1 + L, R))
  443. That is, the angle is the same, but the rotation point is different.
  444. The update of the center point is done by ``w_1 += x_displacement``.
  445. The complete ``move`` function with translation of the entire
  446. vehicle and rotation of the wheels then becomes
  447. .. code-block:: python
  448. w_1 = w_1 + L # start position
  449. def move(t, fig):
  450. x_displacement = dt*v(t)
  451. fig['vehicle'].translate((x_displacement, 0))
  452. # Rotate wheels
  453. global w_1
  454. w_1 += x_displacement
  455. # R*angle = -x_displacement
  456. angle = - x_displacement/R
  457. w1 = fig['vehicle']['wheels']['wheel1']
  458. w1.rotate(degrees(angle), center=(w_1, R))
  459. w2 = fig['vehicle']['wheels']['wheel2']
  460. w2.rotate(degrees(angle), center=(w_1 + L, R))
  461. The complete example is found in the file
  462. `vehicle1.py <http://tinyurl.com/ot733jn/vehicle1.py>`__. You may run this file or watch a `ready-made movie <http://tinyurl.com/oou9lp7/mov-tut/vehicle1.html>`__.
  463. The advantages with making figures this way, through programming
  464. rather than using interactive drawing programs, are numerous. For
  465. example, the objects are parameterized by variables so that various
  466. dimensions can easily be changed. Subparts of the figure, possible
  467. involving a lot of figure objects, can change color, linetype, filling
  468. or other properties through a *single* function call. Subparts of the
  469. figure can be rotated, translated, or scaled. Subparts of the figure
  470. can also be copied and moved to other parts of the drawing
  471. area. However, the single most important feature is probably the
  472. ability to make animations governed by mathematical formulas or data
  473. coming from physics simulations of the problem, as shown in the example above.
  474. .. !split
  475. .. _sketcher:ex:pendulum:
  476. A simple pendulum
  477. =================
  478. .. _sketcher:ex:pendulum:basic:
  479. The basic physics sketch
  480. ------------------------
  481. We now want to make a sketch of simple pendulum from physics, as shown
  482. in Figure :ref:`sketcher:ex:pendulum:fig1`. A body with mass :math:`m` is attached
  483. to a massless, stiff rod, which can rotate about a point, causing the
  484. pendulum to oscillate.
  485. A suggested work flow is to
  486. first sketch the figure on a piece of paper and introduce a coordinate
  487. system. A simple coordinate system is indicated in Figure
  488. :ref:`sketcher:ex:pendulum:fig1wgrid`. In a code we introduce variables
  489. ``W`` and ``H`` for the width and height of the figure (i.e., extent of
  490. the coordinate system) and open the program like this:
  491. .. code-block:: python
  492. from pysketcher import *
  493. H = 7.
  494. W = 6.
  495. drawing_tool.set_coordinate_system(xmin=0, xmax=W,
  496. ymin=0, ymax=H,
  497. axis=True)
  498. drawing_tool.set_grid(True)
  499. drawing_tool.set_linecolor('blue')
  500. Note that when the sketch is ready for "production", we will (normally)
  501. set ``axis=False`` to remove the coordinate system and also remove the
  502. grid, i.e., delete or
  503. comment out the line ``drawing_tool.set_grid(True)``.
  504. Also note that we in this example let all lines be blue by default.
  505. .. _sketcher:ex:pendulum:fig1:
  506. .. figure:: pendulum1.png
  507. :width: 300
  508. *Sketch of a simple pendulum*
  509. .. _sketcher:ex:pendulum:fig1wgrid:
  510. .. figure:: pendulum1_wgrid.png
  511. :width: 400
  512. *Sketch with assisting coordinate system*
  513. The next step is to introduce variables for key quantities in the sketch.
  514. Let ``L`` be the length of the pendulum, ``P`` the rotation point, and let
  515. ``a`` be the angle the pendulum makes with the vertical (measured in degrees).
  516. We may set
  517. .. code-block:: python
  518. L = 5*H/7 # length
  519. P = (W/6, 0.85*H) # rotation point
  520. a = 40 # angle
  521. Be careful with integer division if you use Python 2! Fortunately, we
  522. started out with ``float`` objects for ``W`` and ``H`` so the expressions above
  523. are safe.
  524. What kind of objects do we need in this sketch? Looking at
  525. Figure :ref:`sketcher:ex:pendulum:fig1` we see that we need
  526. 1. a vertical, dashed line
  527. 2. an arc with no text but dashed line to indicate the *path* of the
  528. mass
  529. 3. an arc with name :math:`\theta` to indicate the *angle*
  530. 4. a line, here called *rod*, from the rotation point to the mass
  531. 5. a blue, filled circle representing the *mass*
  532. 6. a text :math:`m` associated with the mass
  533. 7. an indicator of the pendulum's *length* :math:`L`, visualized as
  534. a line with two arrows tips and the text :math:`L`
  535. 8. a gravity vector with the text :math:`g`
  536. Pysketcher has objects for each of these elements in our sketch.
  537. We start with the simplest element: the vertical line, going from
  538. ``P`` to ``P`` minus the length :math:`L` in :math:`y` direction:
  539. .. code-block:: python
  540. vertical = Line(P, P-point(0,L))
  541. The class ``point`` is very convenient: it turns its two coordinates into
  542. a vector with which we can compute, and is therefore one of the most
  543. widely used Pysketcher objects.
  544. The path of the mass is an arc that can be made by
  545. Pysketcher's ``Arc`` object:
  546. .. code-block:: python
  547. path = Arc(P, L, -90, a)
  548. The first argument ``P`` is the center point, the second is the
  549. radius (``L`` here), the next argument is the start angle, here
  550. it starts at -90 degrees, while the next argument is the angle of
  551. the arc, here ``a``.
  552. For the path of the mass, we also need an arc object, but this
  553. time with an associated text. Pysketcher has a specialized object
  554. for this purpose, ``Arc_wText``, since placing the text manually can
  555. be somewhat cumbersome.
  556. .. code-block:: python
  557. angle = Arc_wText(r'$\theta$', P, L/4, -90, a, text_spacing=1/30.)
  558. The arguments are as for ``Arc`` above, but the first one is the desired
  559. text. Remember to use a raw string since we want a LaTeX greek letter
  560. that contains a backslash.
  561. The ``text_spacing`` argument must often be tweaked. It is recommended
  562. to create only a few objects before rendering the sketch and then
  563. adjust spacings as one goes along.
  564. The rod is simply a line from ``P`` to the mass. We can easily
  565. compute the position of the mass from basic geometry considerations,
  566. but it is easier and safer to look up this point in other objects
  567. if it is already computed. In the present case,
  568. the ``path`` object stored its start and
  569. end points, so ``path.geometric_features()['end']`` is the end point
  570. of the path, which is the position of the mass. We can therefore
  571. create the rod simply as a line from ``P`` to this already computed end point:
  572. .. code-block:: python
  573. mass_pt = path.geometric_features()['end']
  574. rod = Line(P, mass_pt)
  575. The mass is a circle filled with color:
  576. .. code-block:: python
  577. mass = Circle(center=mass_pt, radius=L/20.)
  578. mass.set_filled_curves(color='blue')
  579. To place the :math:`m` correctly, we go a small distance in the direction of
  580. the rod, from the center of the circle. To this end, we need to
  581. compute the direction. This is easiest done by computing a vector
  582. from ``P`` to the center of the circle and calling ``unit_vec`` to make
  583. a unit vector in this direction:
  584. .. code-block:: python
  585. rod_vec = rod.geometric_features()['end'] - \
  586. rod.geometric_features()['start']
  587. unit_rod_vec = unit_vec(rod_vec)
  588. mass_symbol = Text('$m$', mass_pt + L/10*unit_rod_vec)
  589. Again, the distance ``L/10`` is something one has to experiment with.
  590. The next object is the length measure with the text :math:`L`. Such length
  591. measures are represented by Pysketcher's ``Distance_wText`` object.
  592. An easy construction is to first place this length measure along the
  593. rod and then translate it a little distance (``L/15``) in the
  594. normal direction of the rod:
  595. .. code-block:: python
  596. length = Distance_wText(P, mass_pt, '$L$')
  597. length.translate(L/15*point(cos(radians(a)), sin(radians(a))))
  598. For this translation we need a unit vector in the normal direction
  599. of the rod, which is from geometric considerations given by
  600. :math:`(\cos a, \sin a)`, when :math:`a` is the angle of the pendulum.
  601. Alternatively, we could have found the normal vector as a vector that
  602. is normal to ``unit_rod_vec``: ``point(-unit_rod_vec[1],unit_rod_vec[0])``.
  603. The final object is the gravity force vector, which is so common
  604. in physics sketches that Pysketcher has a ready-made object: ``Gravity``,
  605. .. code-block:: python
  606. gravity = Gravity(start=P+point(0.8*L,0), length=L/3)
  607. Since blue is the default color for
  608. lines, we want the dashed lines (for ``vertical`` and ``path``) to be black
  609. and with linewidth 1. These properties can be set one by one for each
  610. object, but we can also make a little helper function:
  611. .. code-block:: python
  612. def set_dashed_thin_blackline(*objects):
  613. """Set linestyle of objects to dashed, black, width=1."""
  614. for obj in objects:
  615. obj.set_linestyle('dashed')
  616. obj.set_linecolor('black')
  617. obj.set_linewidth(1)
  618. set_dashed_thin_blackline(vertical, path)
  619. Now, all objects are in place, so it remains to compose the final
  620. figure and draw the composition:
  621. .. code-block:: python
  622. fig = Composition(
  623. {'body': mass, 'rod': rod,
  624. 'vertical': vertical, 'theta': angle, 'path': path,
  625. 'g': gravity, 'L': length, 'm': mass_symbol})
  626. fig.draw()
  627. drawing_tool.display()
  628. drawing_tool.savefig('pendulum1')
  629. The body diagram
  630. ----------------
  631. Now we want to isolate the mass and draw all the forces that act on it.
  632. Figure :ref:`sketcher:ex:pendulum:fig2wgrid` shows the desired result, but
  633. embedded in the coordinate system.
  634. We consider three types of forces: the gravity force, the force from the
  635. rod, and air resistance. The body diagram is key for deriving the
  636. equation of motion, so it is illustrative to add useful mathematical
  637. quantities needed in the derivation, such as the unit vectors in polar
  638. coordinates.
  639. .. _sketcher:ex:pendulum:fig2wgrid:
  640. .. figure:: pendulum5_wgrid.png
  641. :width: 300
  642. *Body diagram of a simple pendulum*
  643. We start by listing the objects in the sketch:
  644. 1. a text :math:`(x_0,y_0)` representing the rotation point ``P``
  645. 2. unit vector :math:`\boldsymbol{i}_r` with text
  646. 3. unit vector :math:`\boldsymbol{i}_\theta` with text
  647. 4. a dashed vertical line
  648. 5. a dashed line along the rod
  649. 6. an arc with text :math:`\theta`
  650. 7. the gravity force with text :math:`mg`
  651. 8. the force in the rod with text :math:`S`
  652. 9. the air resistance force with text :math:`\sim |v|v`
  653. The first object, :math:`(x_0,y_0)`, is simply a plain text where we have
  654. to experiment with its position. The unit vectors in polar coordinates
  655. may be drawn using the Pysketcher's ``Force`` object since it has an
  656. arrow with a text. The first three objects can then be made as follows:
  657. .. code-block:: python
  658. x0y0 = Text('$(x_0,y_0)$', P + point(-0.4,-0.1))
  659. ir = Force(P, P + L/10*unit_vec(rod_vec),
  660. r'$\boldsymbol{i}_r$', text_pos='end',
  661. text_spacing=(0.015,0))
  662. ith = Force(P, P + L/10*unit_vec((-rod_vec[1], rod_vec[0])),
  663. r'$\boldsymbol{i}_{\theta}$', text_pos='end',
  664. text_spacing=(0.02,0.005))
  665. Note that tweaking of the position of ``x0y0`` use absolute coordinates, so
  666. if ``W`` or ``H`` is changed in the beginning of the figure, the tweaked position
  667. will most likely not look good. A better solution would be to express
  668. the tweaked displacement ``point(-0.4,-0.1)`` in terms of ``W`` and ``H``.
  669. The ``text_spacing`` values in the ``Force`` objects also use absolute
  670. coordinates. Very often, this is much more convenient when adjusting
  671. the objects, and global size parameters like ``W`` and ``H`` are in practice
  672. seldom changed, so the solution above is quite typical.
  673. The vertical, dashed line, the dashed rod, and the arc for :math:`\theta`
  674. are made by
  675. .. code-block:: python
  676. rod_start = rod.geometric_features()['start'] # Point P
  677. vertical2 = Line(rod_start, rod_start + point(0,-L/3))
  678. set_dashed_thin_blackline(vertical2)
  679. set_dashed_thin_blackline(rod)
  680. angle2 = Arc_wText(r'$\theta$', rod_start, L/6, -90, a,
  681. text_spacing=1/30.)
  682. Note how we reuse the earlier defined object ``rod``.
  683. The forces are constructed as shown below.
  684. .. code-block:: python
  685. mg_force = Force(mass_pt, mass_pt + L/5*point(0,-1),
  686. '$mg$', text_pos='end')
  687. rod_force = Force(mass_pt, mass_pt - L/3*unit_vec(rod_vec),
  688. '$S$', text_pos='end',
  689. text_spacing=(0.03, 0.01))
  690. air_force = Force(mass_pt, mass_pt -
  691. L/6*unit_vec((rod_vec[1], -rod_vec[0])),
  692. '$\sim|v|v$', text_pos='end',
  693. text_spacing=(0.04,0.005))
  694. Note that the drag force from the air is directed perpendicular to
  695. the rod, so we construct a unit vector in this direction directly from
  696. the ``rod_vec`` vector.
  697. All objects are in place, and we can compose a figure to be drawn:
  698. .. code-block:: python
  699. body_diagram = Composition(
  700. {'mg': mg_force, 'S': rod_force, 'rod': rod,
  701. 'vertical': vertical2, 'theta': angle2,
  702. 'body': mass, 'm': mass_symbol})
  703. body_diagram['air'] = air_force
  704. body_diagram['ir'] = ir
  705. body_diagram['ith'] = ith
  706. body_diagram['origin'] = x0y0
  707. Here, we exemplify that we can start out with a composition as a
  708. dictionary, but (as in ordinary Python dictionaries) add new
  709. elements later when desired.
  710. .. FIGURE: [fig-tut/pendulum1.png, width=300 frac=0.5] Sketch of a simple pendulum.
  711. .. _sketcher:ex:pendulum:anim:
  712. Animated body diagram
  713. ---------------------
  714. We want to make an animated body diagram so that we can see how forces
  715. develop in time according to the motion. This means that we must
  716. couple the sketch at each time level to a numerical solution for
  717. the motion of the pendulum.
  718. Function for drawing the body diagram
  719. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  720. The previous flat program for making sketches of the pendulum is not
  721. suitable when we want to make a sketch at a lot of different points
  722. in time, i.e., for a lot of different angles that the pendulum makes
  723. with the vertical. We therefore need to draw the body diagram in
  724. a function where the angle is a parameter. We also supply arrays
  725. containing the (numerically computed) values of the angle :math:`\theta` and
  726. the forces at various time levels, plus the desired time point and level
  727. for this particular sketch:
  728. .. code-block:: python
  729. from pysketcher import *
  730. H = 15.
  731. W = 17.
  732. drawing_tool.set_coordinate_system(xmin=0, xmax=W,
  733. ymin=0, ymax=H,
  734. axis=False)
  735. def pendulum(theta, S, mg, drag, t, time_level):
  736. drawing_tool.set_linecolor('blue')
  737. import math
  738. a = math.degrees(theta[time_level])
  739. L = 0.4*H # length
  740. P = (W/2, 0.8*H) # rotation point
  741. vertical = Line(P, P-point(0,L))
  742. path = Arc(P, L, -90, a)
  743. angle = Arc_wText(r'$\theta$', P, L/4, -90, a, text_spacing=1/30.)
  744. mass_pt = path.geometric_features()['end']
  745. rod = Line(P, mass_pt)
  746. mass = Circle(center=mass_pt, radius=L/20.)
  747. mass.set_filled_curves(color='blue')
  748. rod_vec = rod.geometric_features()['end'] - \
  749. rod.geometric_features()['start']
  750. unit_rod_vec = unit_vec(rod_vec)
  751. mass_symbol = Text('$m$', mass_pt + L/10*unit_rod_vec)
  752. length = Distance_wText(P, mass_pt, '$L$')
  753. # Displace length indication
  754. length.translate(L/15*point(cos(radians(a)), sin(radians(a))))
  755. gravity = Gravity(start=P+point(0.8*L,0), length=L/3)
  756. def set_dashed_thin_blackline(*objects):
  757. """Set linestyle of objects to dashed, black, width=1."""
  758. for obj in objects:
  759. obj.set_linestyle('dashed')
  760. obj.set_linecolor('black')
  761. obj.set_linewidth(1)
  762. set_dashed_thin_blackline(vertical, path)
  763. fig = Composition(
  764. {'body': mass, 'rod': rod,
  765. 'vertical': vertical, 'theta': angle, 'path': path,
  766. 'g': gravity, 'L': length})
  767. #fig.draw()
  768. #drawing_tool.display()
  769. #drawing_tool.savefig('tmp_pendulum1')
  770. drawing_tool.set_linecolor('black')
  771. rod_start = rod.geometric_features()['start'] # Point P
  772. vertical2 = Line(rod_start, rod_start + point(0,-L/3))
  773. set_dashed_thin_blackline(vertical2)
  774. set_dashed_thin_blackline(rod)
  775. angle2 = Arc_wText(r'$\theta$', rod_start, L/6, -90, a,
  776. text_spacing=1/30.)
  777. magnitude = 1.2*L/2 # length of a unit force in figure
  778. force = mg[time_level] # constant (scaled eq: about 1)
  779. force *= magnitude
  780. mg_force = Force(mass_pt, mass_pt + force*point(0,-1),
  781. '', text_pos='end')
  782. force = S[time_level]
  783. force *= magnitude
  784. rod_force = Force(mass_pt, mass_pt - force*unit_vec(rod_vec),
  785. '', text_pos='end',
  786. text_spacing=(0.03, 0.01))
  787. force = drag[time_level]
  788. force *= magnitude
  789. #print('drag(%g)=%g' % (t, drag[time_level]))
  790. air_force = Force(mass_pt, mass_pt -
  791. force*unit_vec((rod_vec[1], -rod_vec[0])),
  792. '', text_pos='end',
  793. text_spacing=(0.04,0.005))
  794. body_diagram = Composition(
  795. {'mg': mg_force, 'S': rod_force, 'air': air_force,
  796. 'rod': rod,
  797. 'vertical': vertical2, 'theta': angle2,
  798. 'body': mass})
  799. x0y0 = Text('$(x_0,y_0)$', P + point(-0.4,-0.1))
  800. ir = Force(P, P + L/10*unit_vec(rod_vec),
  801. r'$\boldsymbol{i}_r$', text_pos='end',
  802. text_spacing=(0.015,0))
  803. ith = Force(P, P + L/10*unit_vec((-rod_vec[1], rod_vec[0])),
  804. r'$\boldsymbol{i}_{\theta}$', text_pos='end',
  805. text_spacing=(0.02,0.005))
  806. #body_diagram['ir'] = ir
  807. #body_diagram['ith'] = ith
  808. #body_diagram['origin'] = x0y0
  809. drawing_tool.erase()
  810. body_diagram.draw(verbose=0)
  811. #drawing_tool.display('Body diagram')
  812. drawing_tool.savefig('tmp_%04d.png' % time_level, crop=False)
  813. # No cropping: otherwise movies will be very strange
  814. Equations for the motion and forces
  815. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  816. The modeling of the motion of a pendulum is most conveniently done in
  817. polar coordinates since then the unknown force in the rod is separated
  818. from the equation determining the motion :math:`\theta(t)`.
  819. The position vector for the mass is
  820. .. math::
  821. \boldsymbol{r} = x_0\boldsymbol{i} + y_0\boldsymbol{j} + L{\boldsymbol{i}_r}{\thinspace .}
  822. The corresponding acceleration becomes
  823. .. math::
  824. \ddot{\boldsymbol{r}} = L\ddot{\theta}{\boldsymbol{i}_{\theta}} - L\dot{\theta^2}{{\boldsymbol{i}_r}}{\thinspace .}
  825. .. Note: the extra braces help to render the equation correctly in sphinx!
  826. There are three forces on the mass: the gravity force
  827. :math:`mg\boldsymbol{j} = mg(-\cos\theta\,{\boldsymbol{i}_r} + \sin\theta\,\boldsymbol{i}_{\theta})`, the force in the rod
  828. :math:`-S{\boldsymbol{i}_r}`, and the drag force because of air resistance:
  829. .. math::
  830. -\frac{1}{2} C_D \varrho \pi R^2 |v|v\,\boldsymbol{i}_{\theta},
  831. where :math:`C_D\approx 0.4` is the drag coefficient for a sphere, :math:`\varrho`
  832. is the density of air, :math:`R` is the radius of the mass, and :math:`v` is the
  833. velocity (:math:`v=L\dot\theta`). The drag force acts in :math:`-\boldsymbol{i}_{\theta}` direction
  834. when :math:`v>0`.
  835. Newton's second law of motion for the pendulum now becomes
  836. .. math::
  837. mL\ddot\theta\boldsymbol{i}_{\theta} - mL\dot\theta^2{\boldsymbol{i}_r} = -mg(-\cos\theta\,{\boldsymbol{i}_r} +
  838. \sin\theta\,\boldsymbol{i}_{\theta})
  839. -S{\boldsymbol{i}_r} - \frac{1}{2} C_D \varrho \pi R^2 L^2|\dot\theta|\dot\theta\boldsymbol{i}_{\theta},
  840. which gives two component equations
  841. .. math::
  842. :label: sketcher:ex:pendulum:anim:eq:ith
  843. mL\ddot\theta + \frac{1}{2} C_D \varrho \pi R^2 L^2|\dot\theta|\dot\theta +
  844. mg\sin\theta = 0,
  845. .. math::
  846. :label: sketcher:ex:pendulum:anim:eq:ir
  847. S = mL\dot\theta^2 + mg\cos\theta
  848. {\thinspace .}
  849. It is almost always convenient to scale such equations. Introducing
  850. the dimensionless time
  851. .. math::
  852. \bar t = \frac{t}{t_c},\quad t_c = \sqrt{\frac{L}{g}},
  853. leads to
  854. .. math::
  855. :label: sketcher:ex:pendulum:anim:eq:ith:s
  856. \frac{d^2\theta}{d\bar t^2} +
  857. \alpha\left\vert\frac{d\theta}{d\bar t}\right\vert\frac{d\theta}{d\bar t} +
  858. \sin\theta = 0,
  859. .. math::
  860. :label: sketcher:ex:pendulum:anim:eq:ir:s
  861. \bar S = \left(\frac{d\theta}{d\bar t}\right)^2
  862. + \cos\theta,
  863. where :math:`\alpha` is a dimensionless drag coefficient
  864. .. math::
  865. \alpha = \frac{C_D\varrho\pi R^2L}{2m},
  866. and :math:`\bar S` is the scaled force
  867. .. math::
  868. \bar S = \frac{S}{mg}{\thinspace .}
  869. We see that :math:`\bar S = 1` for the equilibrium position :math:`\theta=0`, so this
  870. scaling of :math:`S` seems appropriate.
  871. The parameter :math:`\alpha` is about
  872. the ratio of the drag force and the gravity force:
  873. .. math::
  874. \frac{|\frac{1}{2} C_D\varrho \pi R^2 |v|v|}{|mg|}\sim
  875. \frac{C_D\varrho \pi R^2 L^2 t_c^{-2}}{mg}
  876. \left|\frac{d\bar\theta}{d\bar t}\right|\frac{d\bar\theta}{d\bar t}
  877. \sim \frac{C_D\varrho \pi R^2 L}{2m}\theta_0^2 = \alpha \theta_0^2{\thinspace .}
  878. (We have that :math:`\theta(t)/d\theta_0` is in :math:`[-1,1]`, so we expect
  879. since :math:`\theta_0^{-1}d\bar\theta/d\bar t` to be around unity.)
  880. The next step is to write a numerical solver for
  881. :eq:`sketcher:ex:pendulum:anim:eq:ith:s`-:eq:`sketcher:ex:pendulum:anim:eq:ir:s`. To
  882. this end, we use the `Odespy <https://github.com/hplgit/odespy>`__
  883. package. The system of second-order ODEs must be expressed as a system
  884. of first-order ODEs. We realize that the unknown :math:`\bar S` is decoupled
  885. from :math:`\theta` in the sense that we can first use
  886. :eq:`sketcher:ex:pendulum:anim:eq:ith:s` to solve for :math:`\theta` and
  887. then compute :math:`\bar S` from :eq:`sketcher:ex:pendulum:anim:eq:ir:s`.
  888. The first-order ODEs become
  889. .. math::
  890. :label: _auto1
  891. \frac{d\omega}{d\bar t} = -\alpha\left\vert\omega\right\vert\omega
  892. - \sin\theta,
  893. .. math::
  894. :label: _auto2
  895. \frac{d\theta}{d\bar t} = \omega{\thinspace .}
  896. Then we compute
  897. .. math::
  898. :label: _auto3
  899. \bar S = \omega^2 + \cos\theta{\thinspace .}
  900. The dimensionless air resistance force can also be computed:
  901. .. math::
  902. :label: _auto4
  903. -\alpha|\omega|\omega{\thinspace .}
  904. Since we scaled the force :math:`S` by :math:`mg`, :math:`mg` is the natural force scale,
  905. and the :math:`mg` force itself is then unity.
  906. By updating :math:`\omega` in the first equation, we can use an Euler-Cromer
  907. scheme on Odespy (all other schemes are independent of whether the
  908. :math:`\theta` or :math:`\omega` equation comes first).
  909. Numerical solution
  910. ~~~~~~~~~~~~~~~~~~
  911. An appropriate solver is
  912. .. code-block:: python
  913. def simulate_pendulum(alpha, theta0, dt, T):
  914. import odespy
  915. def f(u, t, alpha):
  916. omega, theta = u
  917. return [-alpha*omega*abs(omega) - sin(theta),
  918. omega]
  919. import numpy as np
  920. Nt = int(round(T/float(dt)))
  921. t = np.linspace(0, Nt*dt, Nt+1)
  922. solver = odespy.RK4(f, f_args=[alpha])
  923. solver.set_initial_condition([0, theta0])
  924. u, t = solver.solve(t,
  925. terminate=lambda u, t, n: abs(u[n,1]) < 1E-3)
  926. omega = u[:,0]
  927. theta = u[:,1]
  928. S = omega**2 + np.cos(theta)
  929. drag = -alpha*np.abs(omega)*omega
  930. return t, theta, omega, S, drag
  931. Animation
  932. ~~~~~~~~~
  933. We can finally traverse the time array and draw a body diagram
  934. at each time level. The resulting sketches are saved to files
  935. ``tmp_%04d.png``, and these files can be combined to videos:
  936. .. code-block:: python
  937. def animate():
  938. # Clean up old plot files
  939. import os, glob
  940. for filename in glob.glob('tmp_*.png') + glob.glob('movie.*'):
  941. os.remove(filename)
  942. # Solve problem
  943. from math import pi, radians, degrees
  944. import numpy as np
  945. alpha = 0.4
  946. period = 2*pi
  947. T = 12*period
  948. dt = period/40
  949. a = 70
  950. theta0 = radians(a)
  951. t, theta, omega, S, drag = simulate_pendulum(alpha, theta0, dt, T)
  952. mg = np.ones(S.size)
  953. # Visualize drag force 5 times as large
  954. drag *= 5
  955. print('NOTE: drag force magnified 5 times!!')
  956. # Draw animation
  957. import time
  958. for time_level, t_ in enumerate(t):
  959. pendulum(theta, S, mg, drag, t_, time_level)
  960. time.sleep(0.2)
  961. # Make videos
  962. prog = 'ffmpeg'
  963. filename = 'tmp_%04d.png'
  964. fps = 6
  965. codecs = {'flv': 'flv', 'mp4': 'libx264',
  966. 'webm': 'libvpx', 'ogg': 'libtheora'}
  967. for ext in codecs:
  968. lib = codecs[ext]
  969. cmd = '%(prog)s -i %(filename)s -r %(fps)s ' % vars()
  970. cmd += '-vcodec %(lib)s movie.%(ext)s' % vars()
  971. print(cmd)
  972. os.system(cmd)
  973. This time we did not use the ``animate`` function from Pysketcher, but
  974. stored each sketch in a file with ``drawing_tool.savefig``. Note that
  975. the argument ``crop=False`` is key: otherwise each figure is cropped and
  976. it makes to sense to combine the images to a video. By default,
  977. Pysketcher crops (removes all exterior whitespace) from figures saved
  978. to file.
  979. .. raw:: html
  980. <div>
  981. <video loop controls width='640' height='365' preload='none'>
  982. <source src='https://github.com/hplgit/pysketcher/raw/master/doc/pub/tutorial/mov-tut/pendulum/movie.mp4' type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'>
  983. <source src='https://github.com/hplgit/pysketcher/raw/master/doc/pub/tutorial/mov-tut/pendulum/movie.webm' type='video/webm; codecs="vp8, vorbis"'>
  984. <source src='https://github.com/hplgit/pysketcher/raw/master/doc/pub/tutorial/mov-tut/pendulum/movie.ogg' type='video/ogg; codecs="theora, vorbis"'>
  985. </video>
  986. </div>
  987. <p><em>The drag force is magnified 5 times (compared to :math:`mg` and :math:`S`!</em></p>
  988. <!-- Issue warning if in a Safari browser -->
  989. <script language="javascript">
  990. if (!!(window.safari)) {
  991. document.write("<div style=\"width: 95%%; padding: 10px; border: 1px solid #100; border-radius: 4px;\"><p><font color=\"red\">The above movie will not play in Safari - use Chrome, Firefox, or Opera.</font></p></div>")}
  992. </script>
  993. .. !split
  994. Basic shapes
  995. ============
  996. This section presents many of the basic shapes in Pysketcher:
  997. ``Axis``, ``Distance_wText``, ``Rectangle``, ``Triangle``, ``Arc``,
  998. ``Spring``, ``Dashpot``, and ``Wavy``.
  999. Each shape is demonstrated with a figure and a
  1000. unit test that shows how the figure is constructed in Python code.
  1001. These demos rely heavily on the method ``draw_dimensions`` in
  1002. the shape classes, which annotates the basic drawing of the shape
  1003. with the various geometric parameters that govern the shape.
  1004. Axis
  1005. ----
  1006. The ``Axis`` object gives the possibility draw a single axis to
  1007. notify a coordinate system. Here is an example where we
  1008. draw :math:`x` and :math:`y` axis of three coordinate systems of different
  1009. rotation:
  1010. |
  1011. |
  1012. .. figure:: Axis.png
  1013. :width: 500
  1014. |
  1015. |
  1016. The corresponding code looks like this:
  1017. .. code-block:: python
  1018. def test_Axis():
  1019. drawing_tool.set_coordinate_system(
  1020. xmin=0, xmax=15, ymin=-7, ymax=8, axis=True,
  1021. instruction_file='tmp_Axis.py')
  1022. # Draw normal x and y axis with origin at (7.5, 2)
  1023. # in the coordinate system of the sketch: [0,15]x[-7,8]
  1024. x_axis = Axis((7.5,2), 5, 'x', rotation_angle=0)
  1025. y_axis = Axis((7.5,2), 5, 'y', rotation_angle=90)
  1026. system = Composition({'x axis': x_axis, 'y axis': y_axis})
  1027. system.draw()
  1028. drawing_tool.display()
  1029. # Rotate this system 40 degrees counter clockwise
  1030. # and draw it with dashed lines
  1031. system.set_linestyle('dashed')
  1032. system.rotate(40, (7.5,2))
  1033. system.draw()
  1034. drawing_tool.display()
  1035. # Rotate this system another 220 degrees and show
  1036. # with dotted lines
  1037. system.set_linestyle('dotted')
  1038. system.rotate(220, (7.5,2))
  1039. system.draw()
  1040. drawing_tool.display()
  1041. drawing_tool.display('Axis')
  1042. Distance with text
  1043. ------------------
  1044. The object ``Distance_wText`` is used to display an arrow, to indicate
  1045. a distance in a sketch, with an additional text in the middle of the arrow.
  1046. The figure
  1047. |
  1048. |
  1049. .. figure:: Distance_wText.png
  1050. :width: 500
  1051. |
  1052. |
  1053. was produced by this code:
  1054. .. code-block:: python
  1055. def test_Distance_wText():
  1056. drawing_tool.set_coordinate_system(
  1057. xmin=0, xmax=10, ymin=0, ymax=6,
  1058. axis=True, instruction_file='tmp_Distance_wText.py')
  1059. fontsize=14
  1060. t = r'$ 2\pi R^2 $' # sample text
  1061. examples = Composition({
  1062. 'a0': Distance_wText((4,5), (8, 5), t, fontsize),
  1063. 'a6': Distance_wText((4,5), (4, 4), t, fontsize),
  1064. 'a1': Distance_wText((0,2), (2, 4.5), t, fontsize),
  1065. 'a2': Distance_wText((0,2), (2, 0), t, fontsize),
  1066. 'a3': Distance_wText((2,4.5), (0, 5.5), t, fontsize),
  1067. 'a4': Distance_wText((8,4), (10, 3), t, fontsize,
  1068. text_spacing=-1./60),
  1069. 'a5': Distance_wText((8,2), (10, 1), t, fontsize,
  1070. text_spacing=-1./40, alignment='right'),
  1071. 'c1': Text_wArrow('text_spacing=-1./60',
  1072. (4, 3.5), (9, 3.2),
  1073. fontsize=10, alignment='left'),
  1074. 'c2': Text_wArrow('text_spacing=-1./40, alignment="right"',
  1075. (4, 0.5), (9, 1.2),
  1076. fontsize=10, alignment='left'),
  1077. })
  1078. examples.draw()
  1079. drawing_tool.display('Distance_wText and text positioning')
  1080. Note the use of ``Text_wArrow`` to write an explaining text with an
  1081. associated arrow, here used to explain how
  1082. the ``text_spacing`` and ``alignment`` arguments can be used to adjust
  1083. the appearance of the text that goes with the distance arrow.
  1084. Rectangle
  1085. ---------
  1086. .. figure:: Rectangle.png
  1087. :width: 500
  1088. |
  1089. |
  1090. The above figure can be produced by the following code.
  1091. .. code-block:: python
  1092. def test_Rectangle():
  1093. L = 3.0
  1094. W = 4.0
  1095. drawing_tool.set_coordinate_system(
  1096. xmin=0, xmax=2*W, ymin=-L/2, ymax=2*L,
  1097. axis=True, instruction_file='tmp_Rectangle.py')
  1098. drawing_tool.set_linecolor('blue')
  1099. drawing_tool.set_grid(True)
  1100. xpos = W/2
  1101. r = Rectangle(lower_left_corner=(xpos,0), width=W, height=L)
  1102. r.draw()
  1103. r.draw_dimensions()
  1104. drawing_tool.display('Rectangle')
  1105. Note that the ``draw_dimension`` method adds explanation of dimensions and various
  1106. important argument in the construction of a shape. It adapts the annotations
  1107. to the geometry of the current shape.
  1108. Triangle
  1109. --------
  1110. .. figure:: Triangle.png
  1111. :width: 500
  1112. |
  1113. |
  1114. The code below produces the figure.
  1115. .. code-block:: python
  1116. def test_Triangle():
  1117. L = 3.0
  1118. W = 4.0
  1119. drawing_tool.set_coordinate_system(
  1120. xmin=0, xmax=2*W, ymin=-L/2, ymax=1.2*L,
  1121. axis=True, instruction_file='tmp_Triangle.py')
  1122. drawing_tool.set_linecolor('blue')
  1123. drawing_tool.set_grid(True)
  1124. xpos = 1
  1125. t = Triangle(p1=(W/2,0), p2=(3*W/2,W/2), p3=(4*W/5.,L))
  1126. t.draw()
  1127. t.draw_dimensions()
  1128. drawing_tool.display('Triangle')
  1129. Here, the ``draw_dimension`` method writes the name of the corners at the
  1130. position of the corners, which does not always look nice (the present figure
  1131. is an example). For a high-quality sketch one would add some spacing
  1132. to the location of the p1, p2, and even p3 texts.
  1133. Arc
  1134. ---
  1135. .. figure:: Arc.png
  1136. :width: 400
  1137. |
  1138. |
  1139. An arc like the one above is produced by
  1140. .. code-block:: python
  1141. def test_Arc():
  1142. L = 4.0
  1143. W = 4.0
  1144. drawing_tool.set_coordinate_system(
  1145. xmin=-W/2, xmax=W, ymin=-L/2, ymax=1.5*L,
  1146. axis=True, instruction_file='tmp_Arc.py')
  1147. drawing_tool.set_linecolor('blue')
  1148. drawing_tool.set_grid(True)
  1149. center = point(0,0)
  1150. radius = L/2
  1151. start_angle = 60
  1152. arc_angle = 45
  1153. a = Arc(center, radius, start_angle, arc_angle)
  1154. a.draw()
  1155. R1 = 1.25*radius
  1156. R2 = 1.5*radius
  1157. R = 2*radius
  1158. a.dimensions = {
  1159. 'start_angle':
  1160. Arc_wText(
  1161. 'start_angle', center, R1, start_angle=0,
  1162. arc_angle=start_angle, text_spacing=1/10.),
  1163. 'arc_angle':
  1164. Arc_wText(
  1165. 'arc_angle', center, R2, start_angle=start_angle,
  1166. arc_angle=arc_angle, text_spacing=1/20.),
  1167. 'r=0':
  1168. Line(center, center +
  1169. point(R*cos(radians(start_angle)),
  1170. R*sin(radians(start_angle)))),
  1171. 'r=start_angle':
  1172. Line(center, center +
  1173. point(R*cos(radians(start_angle+arc_angle)),
  1174. R*sin(radians(start_angle+arc_angle)))),
  1175. 'r=start+arc_angle':
  1176. Line(center, center +
  1177. point(R, 0)).set_linestyle('dashed'),
  1178. 'radius': Distance_wText(center, a(0), 'radius', text_spacing=1/40.),
  1179. 'center': Text('center', center-point(radius/10., radius/10.)),
  1180. }
  1181. for dimension in a.dimensions:
  1182. if dimension.startswith('r='):
  1183. dim = a.dimensions[dimension]
  1184. dim.set_linestyle('dashed')
  1185. dim.set_linewidth(1)
  1186. dim.set_linecolor('black')
  1187. a.draw_dimensions()
  1188. drawing_tool.display('Arc')
  1189. Spring
  1190. ------
  1191. .. figure:: Spring.png
  1192. :width: 800
  1193. |
  1194. |
  1195. The code for making these two springs goes like this:
  1196. .. code-block:: python
  1197. def test_Spring():
  1198. L = 5.0
  1199. W = 2.0
  1200. drawing_tool.set_coordinate_system(
  1201. xmin=0, xmax=7*W, ymin=-L/2, ymax=1.5*L,
  1202. axis=True, instruction_file='tmp_Spring.py')
  1203. drawing_tool.set_linecolor('blue')
  1204. drawing_tool.set_grid(True)
  1205. xpos = W
  1206. s1 = Spring((W,0), L, teeth=True)
  1207. s1_title = Text('Default Spring',
  1208. s1.geometric_features()['end'] + point(0,L/10))
  1209. s1.draw()
  1210. s1_title.draw()
  1211. #s1.draw_dimensions()
  1212. xpos += 3*W
  1213. s2 = Spring(start=(xpos,0), length=L, width=W/2.,
  1214. bar_length=L/6., teeth=False)
  1215. s2.draw()
  1216. s2.draw_dimensions()
  1217. drawing_tool.display('Spring')
  1218. Dashpot
  1219. -------
  1220. .. figure:: Dashpot.png
  1221. :width: 600
  1222. |
  1223. |
  1224. This dashpot is produced by
  1225. .. code-block:: python
  1226. def test_Dashpot():
  1227. L = 5.0
  1228. W = 2.0
  1229. xpos = 0
  1230. drawing_tool.set_coordinate_system(
  1231. xmin=xpos, xmax=xpos+5.5*W, ymin=-L/2, ymax=1.5*L,
  1232. axis=True, instruction_file='tmp_Dashpot.py')
  1233. drawing_tool.set_linecolor('blue')
  1234. drawing_tool.set_grid(True)
  1235. # Default (simple) dashpot
  1236. xpos = 1.5
  1237. d1 = Dashpot(start=(xpos,0), total_length=L)
  1238. d1_title = Text('Dashpot (default)',
  1239. d1.geometric_features()['end'] + point(0,L/10))
  1240. d1.draw()
  1241. d1_title.draw()
  1242. # Dashpot for animation with fixed bar_length, dashpot_length and
  1243. # prescribed piston_pos
  1244. xpos += 2.5*W
  1245. d2 = Dashpot(start=(xpos,0), total_length=1.2*L, width=W/2,
  1246. bar_length=W, dashpot_length=L/2, piston_pos=2*W)
  1247. d2.draw()
  1248. d2.draw_dimensions()
  1249. drawing_tool.display('Dashpot')
  1250. Wavy
  1251. ----
  1252. Looks strange. Fix x axis.
  1253. Stochastic curves
  1254. -----------------
  1255. The ``StochasticWavyCurve`` object offers three precomputed
  1256. graphics that have a random variation:
  1257. |
  1258. |
  1259. .. figure:: StochasticWavyCurve.png
  1260. :width: 600
  1261. |
  1262. |
  1263. The usage is simple. The construction
  1264. .. code-block:: python
  1265. curve = StochasticWavyCurve(curve_no=1, percentage=40)
  1266. picks the second curve (the three are numbered 0, 1, and 2),
  1267. and the first 40% of that curve. In case one desires another extent
  1268. of the axis, one can just scale the coordinates directly as these
  1269. are stored in the arrays ``curve.x[curve_no]`` and
  1270. ``curve.y[curve_no]``.
  1271. .. !split
  1272. Inner workings of the Pysketcher tool
  1273. =====================================
  1274. We shall now explain how we can, quite easily, realize software with
  1275. the capabilities demonstrated in the previous examples. Each object in
  1276. the figure is represented as a class in a class hierarchy. Using
  1277. inheritance, classes can inherit properties from parent classes and
  1278. add new geometric features.
  1279. .. index:: tree data structure
  1280. Class programming is a key technology for realizing Pysketcher.
  1281. As soon as some classes are established, more are easily
  1282. added. Enhanced functionality for all the classes is also easy to
  1283. implement in common, generic code that can immediately be shared by
  1284. all present and future classes. The fundamental data structure
  1285. involved in the ``pysketcher`` package is a hierarchical tree, and much
  1286. of the material on implementation issues targets how to traverse tree
  1287. structures with recursive function calls in object hierarchies. This
  1288. topic is of key relevance in a wide range of other applications as
  1289. well. In total, the inner workings of Pysketcher constitute an
  1290. excellent example on the power of class programming.
  1291. Example of classes for geometric objects
  1292. ----------------------------------------
  1293. We introduce class ``Shape`` as superclass for all specialized objects
  1294. in a figure. This class does not store any data, but provides a
  1295. series of functions that add functionality to all the subclasses.
  1296. This will be shown later.
  1297. Simple geometric objects
  1298. ~~~~~~~~~~~~~~~~~~~~~~~~
  1299. One simple subclass is ``Rectangle``, specified by the coordinates of
  1300. the lower left corner and its width and height:
  1301. .. code-block:: python
  1302. class Rectangle(Shape):
  1303. def __init__(self, lower_left_corner, width, height):
  1304. p = lower_left_corner # short form
  1305. x = [p[0], p[0] + width,
  1306. p[0] + width, p[0], p[0]]
  1307. y = [p[1], p[1], p[1] + height,
  1308. p[1] + height, p[1]]
  1309. self.shapes = {'rectangle': Curve(x,y)}
  1310. Any subclass of ``Shape`` will have a constructor that takes geometric
  1311. information about the shape of the object and creates a dictionary
  1312. ``self.shapes`` with the shape built of simpler shapes. The most
  1313. fundamental shape is ``Curve``, which is just a collection of :math:`(x,y)`
  1314. coordinates in two arrays ``x`` and ``y``. Drawing the ``Curve`` object is
  1315. a matter of plotting ``y`` versus ``x``. For class ``Rectangle`` the ``x``
  1316. and ``y`` arrays contain the corner points of the rectangle in
  1317. counterclockwise direction, starting and ending with in the lower left
  1318. corner.
  1319. Class ``Line`` is also a simple class:
  1320. .. code-block:: python
  1321. class Line(Shape):
  1322. def __init__(self, start, end):
  1323. x = [start[0], end[0]]
  1324. y = [start[1], end[1]]
  1325. self.shapes = {'line': Curve(x, y)}
  1326. Here we only need two points, the start and end point on the line.
  1327. However, we may want to add some useful functionality, e.g., the ability
  1328. to give an :math:`x` coordinate and have the class calculate the
  1329. corresponding :math:`y` coordinate:
  1330. .. code-block:: python
  1331. def __call__(self, x):
  1332. """Given x, return y on the line."""
  1333. x, y = self.shapes['line'].x, self.shapes['line'].y
  1334. self.a = (y[1] - y[0])/(x[1] - x[0])
  1335. self.b = y[0] - self.a*x[0]
  1336. return self.a*x + self.b
  1337. Unfortunately, this is too simplistic because vertical lines cannot be
  1338. handled (infinite ``self.a``). The true source code of ``Line`` therefore
  1339. provides a more general solution at the cost of significantly longer
  1340. code with more tests.
  1341. A circle implies a somewhat increased complexity. Again we represent
  1342. the geometric object by a ``Curve`` object, but this time the ``Curve``
  1343. object needs to store a large number of points on the curve such that
  1344. a plotting program produces a visually smooth curve. The points on
  1345. the circle must be calculated manually in the constructor of class
  1346. ``Circle``. The formulas for points :math:`(x,y)` on a curve with radius :math:`R`
  1347. and center at :math:`(x_0, y_0)` are given by
  1348. .. math::
  1349. x &= x_0 + R\cos (t),\\
  1350. y &= y_0 + R\sin (t),
  1351. where :math:`t\in [0, 2\pi]`. A discrete set of :math:`t` values in this
  1352. interval gives the corresponding set of :math:`(x,y)` coordinates on
  1353. the circle. The user must specify the resolution as the number
  1354. of :math:`t` values. The circle's radius and center must of course
  1355. also be specified.
  1356. We can write the ``Circle`` class as
  1357. .. code-block:: python
  1358. class Circle(Shape):
  1359. def __init__(self, center, radius, resolution=180):
  1360. self.center, self.radius = center, radius
  1361. self.resolution = resolution
  1362. t = linspace(0, 2*pi, resolution+1)
  1363. x0 = center[0]; y0 = center[1]
  1364. R = radius
  1365. x = x0 + R*cos(t)
  1366. y = y0 + R*sin(t)
  1367. self.shapes = {'circle': Curve(x, y)}
  1368. As in class ``Line`` we can offer the possibility to give an angle
  1369. :math:`\theta` (equivalent to :math:`t` in the formulas above)
  1370. and then get the corresponding :math:`x` and :math:`y` coordinates:
  1371. .. code-block:: python
  1372. def __call__(self, theta):
  1373. """Return (x, y) point corresponding to angle theta."""
  1374. return self.center[0] + self.radius*cos(theta), \
  1375. self.center[1] + self.radius*sin(theta)
  1376. There is one flaw with this method: it yields illegal values after
  1377. a translation, scaling, or rotation of the circle.
  1378. A part of a circle, an arc, is a frequent geometric object when
  1379. drawing mechanical systems. The arc is constructed much like
  1380. a circle, but :math:`t` runs in :math:`[\theta_s, \theta_s + \theta_a]`. Giving
  1381. :math:`\theta_s` and :math:`\theta_a` the slightly more descriptive names
  1382. ``start_angle`` and ``arc_angle``, the code looks like this:
  1383. .. code-block:: python
  1384. class Arc(Shape):
  1385. def __init__(self, center, radius,
  1386. start_angle, arc_angle,
  1387. resolution=180):
  1388. self.start_angle = radians(start_angle)
  1389. self.arc_angle = radians(arc_angle)
  1390. t = linspace(self.start_angle,
  1391. self.start_angle + self.arc_angle,
  1392. resolution+1)
  1393. x0 = center[0]; y0 = center[1]
  1394. R = radius
  1395. x = x0 + R*cos(t)
  1396. y = y0 + R*sin(t)
  1397. self.shapes = {'arc': Curve(x, y)}
  1398. Having the ``Arc`` class, a ``Circle`` can alternatively be defined as
  1399. a subclass specializing the arc to a circle:
  1400. .. code-block:: python
  1401. class Circle(Arc):
  1402. def __init__(self, center, radius, resolution=180):
  1403. Arc.__init__(self, center, radius, 0, 360, resolution)
  1404. Class curve
  1405. ~~~~~~~~~~~
  1406. Class ``Curve`` sits on the coordinates to be drawn, but how is that
  1407. done? The constructor of class ``Curve`` just stores the coordinates,
  1408. while a method ``draw`` sends the coordinates to the plotting program to
  1409. make a graph. Or more precisely, to avoid a lot of (e.g.)
  1410. Matplotlib-specific plotting commands in class ``Curve`` we have created
  1411. a small layer with a simple programming interface to plotting
  1412. programs. This makes it straightforward to change from Matplotlib to
  1413. another plotting program. The programming interface is represented by
  1414. the ``drawing_tool`` object and has a few functions:
  1415. * ``plot_curve`` for sending a curve in terms of :math:`x` and :math:`y` coordinates
  1416. to the plotting program,
  1417. * ``set_coordinate_system`` for specifying the graphics area,
  1418. * ``erase`` for deleting all elements of the graph,
  1419. * ``set_grid`` for turning on a grid (convenient while constructing the figure),
  1420. * ``set_instruction_file`` for creating a separate file with all
  1421. plotting commands (Matplotlib commands in our case),
  1422. * a series of ``set_X`` functions where ``X`` is some property like
  1423. ``linecolor``, ``linestyle``, ``linewidth``, ``filled_curves``.
  1424. This is basically all we need to communicate to a plotting program.
  1425. Any class in the ``Shape`` hierarchy inherits ``set_X`` functions for
  1426. setting properties of curves. This information is propagated to
  1427. all other shape objects in the ``self.shapes`` dictionary. Class
  1428. ``Curve`` stores the line properties together with the coordinates
  1429. of its curve and propagates this information to the plotting program.
  1430. When saying ``vehicle.set_linewidth(10)``, all objects that make
  1431. up the ``vehicle`` object will get a ``set_linewidth(10)`` call,
  1432. but only the ``Curve`` object at the end of the chain will actually
  1433. store the information and send it to the plotting program.
  1434. A rough sketch of class ``Curve`` reads
  1435. .. code-block:: python
  1436. class Curve(Shape):
  1437. """General curve as a sequence of (x,y) coordintes."""
  1438. def __init__(self, x, y):
  1439. self.x = asarray(x, dtype=float)
  1440. self.y = asarray(y, dtype=float)
  1441. def draw(self):
  1442. drawing_tool.plot_curve(
  1443. self.x, self.y,
  1444. self.linestyle, self.linewidth, self.linecolor, ...)
  1445. def set_linewidth(self, width):
  1446. self.linewidth = width
  1447. det set_linestyle(self, style):
  1448. self.linestyle = style
  1449. ...
  1450. Compound geometric objects
  1451. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  1452. The simple classes ``Line``, ``Arc``, and ``Circle`` could can the geometric
  1453. shape through just one ``Curve`` object. More complicated shapes are
  1454. built from instances of various subclasses of ``Shape``. Classes used
  1455. for professional drawings soon get quite complex in composition and
  1456. have a lot of geometric details, so here we prefer to make a very
  1457. simple composition: the already drawn vehicle from Figure
  1458. :ref:`sketcher:fig:vehicle0`. That is, instead of composing the drawing
  1459. in a Python program as shown above, we make a subclass ``Vehicle0`` in
  1460. the ``Shape`` hierarchy for doing the same thing.
  1461. The ``Shape`` hierarchy is found in the ``pysketcher`` package, so to use these
  1462. classes or derive a new one, we need to import ``pysketcher``. The constructor
  1463. of class ``Vehicle0`` performs approximately the same statements as
  1464. in the example program we developed for making the drawing in
  1465. Figure :ref:`sketcher:fig:vehicle0`.
  1466. .. code-block:: python
  1467. from pysketcher import *
  1468. class Vehicle0(Shape):
  1469. def __init__(self, w_1, R, L, H):
  1470. wheel1 = Circle(center=(w_1, R), radius=R)
  1471. wheel2 = wheel1.copy()
  1472. wheel2.translate((L,0))
  1473. under = Rectangle(lower_left_corner=(w_1-2*R, 2*R),
  1474. width=2*R + L + 2*R, height=H)
  1475. over = Rectangle(lower_left_corner=(w_1, 2*R + H),
  1476. width=2.5*R, height=1.25*H)
  1477. wheels = Composition(
  1478. {'wheel1': wheel1, 'wheel2': wheel2})
  1479. body = Composition(
  1480. {'under': under, 'over': over})
  1481. vehicle = Composition({'wheels': wheels, 'body': body})
  1482. xmax = w_1 + 2*L + 3*R
  1483. ground = Wall(x=[R, xmax], y=[0, 0], thickness=-0.3*R)
  1484. self.shapes = {'vehicle': vehicle, 'ground': ground}
  1485. Any subclass of ``Shape`` *must* define the ``shapes`` attribute, otherwise
  1486. the inherited ``draw`` method (and a lot of other methods too) will
  1487. not work.
  1488. The painting of the vehicle, as shown in the right part of
  1489. Figure :ref:`sketcher:fig:vehicle0:v2`, could in class ``Vehicle0``
  1490. be offered by a method:
  1491. .. code-block:: python
  1492. def colorful(self):
  1493. wheels = self.shapes['vehicle']['wheels']
  1494. wheels.set_filled_curves('blue')
  1495. wheels.set_linewidth(6)
  1496. wheels.set_linecolor('black')
  1497. under = self.shapes['vehicle']['body']['under']
  1498. under.set_filled_curves('red')
  1499. over = self.shapes['vehicle']['body']['over']
  1500. over.set_filled_curves(pattern='/')
  1501. over.set_linewidth(14)
  1502. The usage of the class is simple: after having set up an appropriate
  1503. coordinate system as previously shown, we can do
  1504. .. code-block:: python
  1505. vehicle = Vehicle0(w_1, R, L, H)
  1506. vehicle.draw()
  1507. drawing_tool.display()
  1508. and go on the make a painted version by
  1509. .. code-block:: python
  1510. drawing_tool.erase()
  1511. vehicle.colorful()
  1512. vehicle.draw()
  1513. drawing_tool.display()
  1514. A complete code defining and using class ``Vehicle0`` is found in the file
  1515. `vehicle2.py <http://tinyurl.com/ot733jn/vehicle2.py>`__.
  1516. The ``pysketcher`` package contains a wide range of classes for various
  1517. geometrical objects, particularly those that are frequently used in
  1518. drawings of mechanical systems.
  1519. Adding functionality via recursion
  1520. ----------------------------------
  1521. .. index:: recursive function calls
  1522. The really powerful feature of our class hierarchy is that we can add
  1523. much functionality to the superclass ``Shape`` and to the "bottom" class
  1524. ``Curve``, and then all other classes for various types of geometrical shapes
  1525. immediately get the new functionality. To explain the idea we may
  1526. look at the ``draw`` method, which all classes in the ``Shape``
  1527. hierarchy must have. The inner workings of the ``draw`` method explain
  1528. the secrets of how a series of other useful operations on figures
  1529. can be implemented.
  1530. Basic principles of recursion
  1531. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  1532. Note that we work with two types of hierarchies in the
  1533. present documentation: one Python *class hierarchy*,
  1534. with ``Shape`` as superclass, and one *object hierarchy* of figure elements
  1535. in a specific figure. A subclass of ``Shape`` stores its figure in the
  1536. ``self.shapes`` dictionary. This dictionary represents the object hierarchy
  1537. of figure elements for that class. We want to make one ``draw`` call
  1538. for an instance, say our class ``Vehicle0``, and then we want this call
  1539. to be propagated to *all* objects that are contained in
  1540. ``self.shapes`` and all is nested subdictionaries. How is this done?
  1541. The natural starting point is to call ``draw`` for each ``Shape`` object
  1542. in the ``self.shapes`` dictionary:
  1543. .. code-block:: python
  1544. def draw(self):
  1545. for shape in self.shapes:
  1546. self.shapes[shape].draw()
  1547. This general method can be provided by class ``Shape`` and inherited in
  1548. subclasses like ``Vehicle0``. Let ``v`` be a ``Vehicle0`` instance.
  1549. Seemingly, a call ``v.draw()`` just calls
  1550. .. code-block:: python
  1551. v.shapes['vehicle'].draw()
  1552. v.shapes['ground'].draw()
  1553. However, in the former call we call the ``draw`` method of a ``Composition`` object
  1554. whose ``self.shapes`` attributed has two elements: ``wheels`` and ``body``.
  1555. Since class ``Composition`` inherits the same ``draw`` method, this method will
  1556. run through ``self.shapes`` and call ``wheels.draw()`` and ``body.draw()``.
  1557. Now, the ``wheels`` object is also a ``Composition`` with the same ``draw``
  1558. method, which will run through ``self.shapes``, now containing
  1559. the ``wheel1`` and ``wheel2`` objects. The ``wheel1`` object is a ``Circle``,
  1560. so calling ``wheel1.draw()`` calls the ``draw`` method in class ``Circle``,
  1561. but this is the same ``draw`` method as shown above. This method will
  1562. therefore traverse the circle's ``shapes`` dictionary, which we have seen
  1563. consists of one ``Curve`` element.
  1564. The ``Curve`` object holds the coordinates to be plotted so here ``draw``
  1565. really needs to do something "physical", namely send the coordinates to
  1566. the plotting program. The ``draw`` method is outlined in the short listing
  1567. of class ``Curve`` shown previously.
  1568. We can go to any of the other shape objects that appear in the figure
  1569. hierarchy and follow their ``draw`` calls in the similar way. Every time,
  1570. a ``draw`` call will invoke a new ``draw`` call, until we eventually hit
  1571. a ``Curve`` object at the "bottom" of the figure hierarchy, and then that part
  1572. of the figure is really plotted (or more precisely, the coordinates
  1573. are sent to a plotting program).
  1574. When a method calls itself, such as ``draw`` does, the calls are known as
  1575. *recursive* and the programming principle is referred to as
  1576. *recursion*. This technique is very often used to traverse hierarchical
  1577. structures like the figure structures we work with here. Even though the
  1578. hierarchy of objects building up a figure are of different types, they
  1579. all inherit the same ``draw`` method and therefore exhibit the same
  1580. behavior with respect to drawing. Only the ``Curve`` object has a different
  1581. ``draw`` method, which does not lead to more recursion.
  1582. Explaining recursion
  1583. ~~~~~~~~~~~~~~~~~~~~
  1584. Understanding recursion is usually a challenge. To get a better idea of
  1585. how recursion works, we have equipped class ``Shape`` with a method ``recurse``
  1586. that just visits all the objects in the ``shapes`` dictionary and prints
  1587. out a message for each object.
  1588. This feature allows us to trace the execution and see exactly where
  1589. we are in the hierarchy and which objects that are visited.
  1590. The ``recurse`` method is very similar to ``draw``:
  1591. .. code-block:: python
  1592. def recurse(self, name, indent=0):
  1593. # print message where we are (name is where we come from)
  1594. for shape in self.shapes:
  1595. # print message about which object to visit
  1596. self.shapes[shape].recurse(indent+2, shape)
  1597. The ``indent`` parameter governs how much the message from this
  1598. ``recurse`` method is intended. We increase ``indent`` by 2 for every
  1599. level in the hierarchy, i.e., every row of objects in Figure
  1600. :ref:`sketcher:fig:Vehicle0:hier2`. This indentation makes it easy to
  1601. see on the printout how far down in the hierarchy we are.
  1602. A typical message written by ``recurse`` when ``name`` is ``'body'`` and
  1603. the ``shapes`` dictionary has the keys ``'over'`` and ``'under'``,
  1604. will be
  1605. .. code-block:: text
  1606. Composition: body.shapes has entries 'over', 'under'
  1607. call body.shapes["over"].recurse("over", 6)
  1608. The number of leading blanks on each line corresponds to the value of
  1609. ``indent``. The code printing out such messages looks like
  1610. .. code-block:: python
  1611. def recurse(self, name, indent=0):
  1612. space = ' '*indent
  1613. print space, '%s: %s.shapes has entries' % \
  1614. (self.__class__.__name__, name), \
  1615. str(list(self.shapes.keys()))[1:-1]
  1616. for shape in self.shapes:
  1617. print space,
  1618. print 'call %s.shapes["%s"].recurse("%s", %d)' % \
  1619. (name, shape, shape, indent+2)
  1620. self.shapes[shape].recurse(shape, indent+2)
  1621. Let us follow a ``v.recurse('vehicle')`` call in detail, ``v`` being
  1622. a ``Vehicle0`` instance. Before looking into the output from ``recurse``,
  1623. let us get an overview of the figure hierarchy in the ``v`` object
  1624. (as produced by ``print v``)
  1625. .. code-block:: text
  1626. ground
  1627. wall
  1628. vehicle
  1629. body
  1630. over
  1631. rectangle
  1632. under
  1633. rectangle
  1634. wheels
  1635. wheel1
  1636. arc
  1637. wheel2
  1638. arc
  1639. The ``recurse`` method performs the same kind of traversal of the
  1640. hierarchy, but writes out and explains a lot more.
  1641. The data structure represented by ``v.shapes`` is known as a *tree*.
  1642. As in physical trees, there is a *root*, here the ``v.shapes``
  1643. dictionary. A graphical illustration of the tree (upside down) is
  1644. shown in Figure :ref:`sketcher:fig:Vehicle0:hier2`.
  1645. From the root there are one or more branches, here two:
  1646. ``ground`` and ``vehicle``. Following the ``vehicle`` branch, it has two new
  1647. branches, ``body`` and ``wheels``. Relationships as in family trees
  1648. are often used to describe the relations in object trees too: we say
  1649. that ``vehicle`` is the parent of ``body`` and that ``body`` is a child of
  1650. ``vehicle``. The term *node* is also often used to describe an element
  1651. in a tree. A node may have several other nodes as *descendants*.
  1652. .. _sketcher:fig:Vehicle0:hier2:
  1653. .. figure:: Vehicle0_hier2.png
  1654. :width: 600
  1655. *Hierarchy of figure elements in an instance of class `Vehicle0`*
  1656. Recursion is the principal programming technique to traverse tree structures.
  1657. Any object in the tree can be viewed as a root of a subtree. For
  1658. example, ``wheels`` is the root of a subtree that branches into
  1659. ``wheel1`` and ``wheel2``. So when processing an object in the tree,
  1660. we imagine we process the root and then recurse into a subtree, but the
  1661. first object we recurse into can be viewed as the root of the subtree, so the
  1662. processing procedure of the parent object can be repeated.
  1663. A recommended next step is to simulate the ``recurse`` method by hand and
  1664. carefully check that what happens in the visits to ``recurse`` is
  1665. consistent with the output listed below. Although tedious, this is
  1666. a major exercise that guaranteed will help to demystify recursion.
  1667. A part of the printout of ``v.recurse('vehicle')`` looks like
  1668. .. code-block:: text
  1669. Vehicle0: vehicle.shapes has entries 'ground', 'vehicle'
  1670. call vehicle.shapes["ground"].recurse("ground", 2)
  1671. Wall: ground.shapes has entries 'wall'
  1672. call ground.shapes["wall"].recurse("wall", 4)
  1673. reached "bottom" object Curve
  1674. call vehicle.shapes["vehicle"].recurse("vehicle", 2)
  1675. Composition: vehicle.shapes has entries 'body', 'wheels'
  1676. call vehicle.shapes["body"].recurse("body", 4)
  1677. Composition: body.shapes has entries 'over', 'under'
  1678. call body.shapes["over"].recurse("over", 6)
  1679. Rectangle: over.shapes has entries 'rectangle'
  1680. call over.shapes["rectangle"].recurse("rectangle", 8)
  1681. reached "bottom" object Curve
  1682. call body.shapes["under"].recurse("under", 6)
  1683. Rectangle: under.shapes has entries 'rectangle'
  1684. call under.shapes["rectangle"].recurse("rectangle", 8)
  1685. reached "bottom" object Curve
  1686. ...
  1687. This example should clearly demonstrate the principle that we
  1688. can start at any object in the tree and do a recursive set
  1689. of calls with that object as root.
  1690. .. _sketcher:scaling:
  1691. Scaling, translating, and rotating a figure
  1692. -------------------------------------------
  1693. With recursion, as explained in the previous section, we can within
  1694. minutes equip *all* classes in the ``Shape`` hierarchy, both present and
  1695. future ones, with the ability to scale the figure, translate it,
  1696. or rotate it. This added functionality requires only a few lines
  1697. of code.
  1698. Scaling
  1699. ~~~~~~~
  1700. We start with the simplest of the three geometric transformations,
  1701. namely scaling. For a ``Curve`` instance containing a set of :math:`n`
  1702. coordinates :math:`(x_i,y_i)` that make up a curve, scaling by a factor :math:`a`
  1703. means that we multiply all the :math:`x` and :math:`y` coordinates by :math:`a`:
  1704. .. math::
  1705. x_i \leftarrow ax_i,\quad y_i\leftarrow ay_i,
  1706. \quad i=0,\ldots,n-1\thinspace .
  1707. Here we apply the arrow as an assignment operator.
  1708. The corresponding Python implementation in
  1709. class ``Curve`` reads
  1710. .. code-block:: python
  1711. class Curve:
  1712. ...
  1713. def scale(self, factor):
  1714. self.x = factor*self.x
  1715. self.y = factor*self.y
  1716. Note here that ``self.x`` and ``self.y`` are Numerical Python arrays,
  1717. so that multiplication by a scalar number ``factor`` is
  1718. a vectorized operation.
  1719. An even more efficient implementation is to make use of in-place
  1720. multiplication in the arrays,
  1721. .. code-block:: python
  1722. class Curve:
  1723. ...
  1724. def scale(self, factor):
  1725. self.x *= factor
  1726. self.y *= factor
  1727. as this saves the creation of temporary arrays like ``factor*self.x``.
  1728. In an instance of a subclass of ``Shape``, the meaning of a method
  1729. ``scale`` is to run through all objects in the dictionary ``shapes`` and
  1730. ask each object to scale itself. This is the same delegation of
  1731. actions to subclass instances as we do in the ``draw`` (or ``recurse``)
  1732. method. All objects, except ``Curve`` instances, can share the same
  1733. implementation of the ``scale`` method. Therefore, we place the ``scale``
  1734. method in the superclass ``Shape`` such that all subclasses inherit the
  1735. method. Since ``scale`` and ``draw`` are so similar, we can easily
  1736. implement the ``scale`` method in class ``Shape`` by copying and editing
  1737. the ``draw`` method:
  1738. .. code-block:: python
  1739. class Shape:
  1740. ...
  1741. def scale(self, factor):
  1742. for shape in self.shapes:
  1743. self.shapes[shape].scale(factor)
  1744. This is all we have to do in order to equip all subclasses of
  1745. ``Shape`` with scaling functionality!
  1746. Any piece of the figure will scale itself, in the same manner
  1747. as it can draw itself.
  1748. Translation
  1749. ~~~~~~~~~~~
  1750. A set of coordinates :math:`(x_i, y_i)` can be translated :math:`v_0` units in
  1751. the :math:`x` direction and :math:`v_1` units in the :math:`y` direction using the formulas
  1752. .. math::
  1753. x_i\leftarrow x_i+v_0,\quad y_i\leftarrow y_i+v_1,
  1754. \quad i=0,\ldots,n-1\thinspace .
  1755. The natural specification of the translation is in terms of the
  1756. vector :math:`v=(v_0,v_1)`.
  1757. The corresponding Python implementation in class ``Curve`` becomes
  1758. .. code-block:: python
  1759. class Curve:
  1760. ...
  1761. def translate(self, v):
  1762. self.x += v[0]
  1763. self.y += v[1]
  1764. The translation operation for a shape object is very similar to the
  1765. scaling and drawing operations. This means that we can implement a
  1766. common method ``translate`` in the superclass ``Shape``. The code
  1767. is parallel to the ``scale`` method:
  1768. .. code-block:: python
  1769. class Shape:
  1770. ....
  1771. def translate(self, v):
  1772. for shape in self.shapes:
  1773. self.shapes[shape].translate(v)
  1774. Rotation
  1775. ~~~~~~~~
  1776. Rotating a figure is more complicated than scaling and translating.
  1777. A counter clockwise rotation of :math:`\theta` degrees for a set of
  1778. coordinates :math:`(x_i,y_i)` is given by
  1779. .. math::
  1780. \bar x_i &\leftarrow x_i\cos\theta - y_i\sin\theta,\\
  1781. \bar y_i &\leftarrow x_i\sin\theta + y_i\cos\theta\thinspace .
  1782. This rotation is performed around the origin. If we want the figure
  1783. to be rotated with respect to a general point :math:`(x,y)`, we need to
  1784. extend the formulas above:
  1785. .. math::
  1786. \bar x_i &\leftarrow x + (x_i -x)\cos\theta - (y_i -y)\sin\theta,\\
  1787. \bar y_i &\leftarrow y + (x_i -x)\sin\theta + (y_i -y)\cos\theta\thinspace .
  1788. The Python implementation in class ``Curve``, assuming that :math:`\theta`
  1789. is given in degrees and not in radians, becomes
  1790. .. code-block:: python
  1791. def rotate(self, angle, center):
  1792. angle = radians(angle)
  1793. x, y = center
  1794. c = cos(angle); s = sin(angle)
  1795. xnew = x + (self.x - x)*c - (self.y - y)*s
  1796. ynew = y + (self.x - x)*s + (self.y - y)*c
  1797. self.x = xnew
  1798. self.y = ynew
  1799. The ``rotate`` method in class ``Shape`` follows the principle of the
  1800. ``draw``, ``scale``, and ``translate`` methods.
  1801. We have already seen the ``rotate`` method in action when animating the
  1802. rolling wheel at the end of the section :ref:`sketcher:vehicle1:anim`.