shapes.py 131 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849
  1. from numpy import linspace, sin, cos, pi, array, asarray, ndarray, sqrt, abs
  2. import pprint, copy, glob, os
  3. from math import radians
  4. from MatplotlibDraw import MatplotlibDraw
  5. drawing_tool = MatplotlibDraw()
  6. def point(x, y, check_inside=False):
  7. for obj, name in zip([x, y], ['x', 'y']):
  8. if isinstance(obj, (float,int)):
  9. pass
  10. elif isinstance(obj, ndarray):
  11. if obj.size == 1:
  12. pass
  13. else:
  14. raise TypeError('%s=%s of type %d has length=%d > 1' %
  15. (name, obj, type(obj), obj.size))
  16. else:
  17. raise TypeError('%s=%s is of wrong type %d' %
  18. (name, obj, type(obj)))
  19. if check_inside:
  20. ok, msg = drawing_tool.inside((x,y), exception=True)
  21. if not ok:
  22. print msg
  23. return array((x, y), dtype=float)
  24. def distance(p1, p2):
  25. p1 = arr2D(p1); p2 = arr2D(p2)
  26. d = p2 - p1
  27. return sqrt(d[0]**2 + d[1]**2)
  28. def unit_vec(x, y=None):
  29. """Return unit vector of the vector (x,y), or just x if x is a 2D point."""
  30. if isinstance(x, (float,int)) and isinstance(y, (float,int)):
  31. x = point(x, y)
  32. elif isinstance(x, (list,tuple,ndarray)) and y is None:
  33. return arr2D(x)/sqrt(x[0]**2 + x[1]**2)
  34. else:
  35. raise TypeError('x=%s is %s, must be float or ndarray 2D point' %
  36. (x, type(x)))
  37. def arr2D(x, check_inside=False):
  38. if isinstance(x, (tuple,list,ndarray)):
  39. if len(x) == 2:
  40. pass
  41. else:
  42. raise ValueError('x=%s has length %d, not 2' % (x, len(x)))
  43. else:
  44. raise TypeError('x=%s must be list/tuple/ndarray, not %s' %
  45. (x, type(x)))
  46. if check_inside:
  47. ok, msg = drawing_tool.inside(x, exception=True)
  48. if not ok:
  49. print msg
  50. return asarray(x, dtype=float)
  51. def _is_sequence(seq, length=None,
  52. can_be_None=False, error_message=True):
  53. if can_be_None:
  54. legal_types = (list,tuple,ndarray,None)
  55. else:
  56. legal_types = (list,tuple,ndarray)
  57. if isinstance(seq, legal_types):
  58. if length is not None:
  59. if length == len(seq):
  60. return True
  61. elif error_message:
  62. raise TypeError('%s is %s; must be %s of length %d' %
  63. (str(seq), type(seq),
  64. ', '.join([str(t) for t in legal_types]),
  65. len(seq)))
  66. else:
  67. return False
  68. else:
  69. return True
  70. elif error_message:
  71. raise TypeError('%s is %s, %s; must be %s' %
  72. (str(seq), seq.__class__.__name__, type(seq),
  73. ','.join([str(t)[5:-1] for t in legal_types])))
  74. else:
  75. return False
  76. def is_sequence(*sequences, **kwargs):
  77. length = kwargs.get('length', 2)
  78. can_be_None = kwargs.get('can_be_None', False)
  79. error_message = kwargs.get('error_message', True)
  80. check_inside = kwargs.get('check_inside', False)
  81. for x in sequences:
  82. _is_sequence(x, length=length, can_be_None=can_be_None,
  83. error_message=error_message)
  84. if check_inside:
  85. ok, msg = drawing_tool.inside(x, exception=True)
  86. if not ok:
  87. print msg
  88. def animate(fig, time_points, action, moviefiles=False,
  89. pause_per_frame=0.5, **action_kwargs):
  90. if moviefiles:
  91. # Clean up old frame files
  92. framefilestem = 'tmp_frame_'
  93. framefiles = glob.glob('%s*.png' % framefilestem)
  94. for framefile in framefiles:
  95. os.remove(framefile)
  96. for n, t in enumerate(time_points):
  97. drawing_tool.erase()
  98. action(t, fig, **action_kwargs)
  99. #could demand returning fig, but in-place modifications
  100. #are done anyway
  101. #fig = action(t, fig)
  102. #if fig is None:
  103. # raise TypeError(
  104. # 'animate: action returns None, not fig\n'
  105. # '(a Shape object with the whole figure)')
  106. fig.draw()
  107. drawing_tool.display()
  108. if moviefiles:
  109. drawing_tool.savefig('%s%04d.png' % (framefilestem, n))
  110. if moviefiles:
  111. return '%s%%04d.png' % framefilestem
  112. class Shape:
  113. """
  114. Superclass for drawing different geometric shapes.
  115. Subclasses define shapes, but drawing, rotation, translation,
  116. etc. are done in generic functions in this superclass.
  117. """
  118. def __init__(self):
  119. """
  120. Never to be called from subclasses.
  121. """
  122. raise NotImplementedError(
  123. 'class %s must implement __init__,\nwhich defines '
  124. 'self.shapes as a dict (or list) of Shape objects\n'
  125. 'Do not call Shape.__init__!' % \
  126. self.__class__.__name__)
  127. def set_name(self, name):
  128. self.name = name
  129. return self
  130. def get_name(self):
  131. return self.name if hasattr(self, 'name') else 'no_name'
  132. def __iter__(self):
  133. # We iterate over self.shapes many places, and will
  134. # get here if self.shapes is just a Shape object and
  135. # not the assumed dict/list.
  136. print 'Warning: class %s does not define self.shapes\n'\
  137. 'as a dict of Shape objects'
  138. return [self] # Make the iteration work
  139. def copy(self):
  140. return copy.deepcopy(self)
  141. def __getitem__(self, name):
  142. """
  143. Allow indexing like::
  144. obj1['name1']['name2']
  145. all the way down to ``Curve`` or ``Point`` (``Text``)
  146. objects.
  147. """
  148. if hasattr(self, 'shapes'):
  149. if name in self.shapes:
  150. return self.shapes[name]
  151. else:
  152. for shape in self.shapes:
  153. if isinstance(self.shapes[shape], (Curve,Point)):
  154. # Indexing of Curve/Point/Text is not possible
  155. raise TypeError(
  156. 'Index "%s" (%s) is illegal' %
  157. (name, self.__class__.__name__))
  158. return self.shapes[shape][name]
  159. else:
  160. raise Exception('This is a bug')
  161. def _for_all_shapes(self, func, *args, **kwargs):
  162. if not hasattr(self, 'shapes'):
  163. # When self.shapes is lacking, we either come to
  164. # a special implementation of func or we come here
  165. # because Shape.func is just inherited. This is
  166. # an error if the class is not Curve or Point
  167. if isinstance(self, (Curve, Point)):
  168. return # ok: no shapes and no func
  169. else:
  170. raise AttributeError('class %s has no shapes attribute!' %
  171. self.__class__.__name__)
  172. is_dict = True if isinstance(self.shapes, dict) else False
  173. for k, shape in enumerate(self.shapes):
  174. if is_dict:
  175. shape_name = shape
  176. shape = self.shapes[shape]
  177. else:
  178. shape_name = k # use index as name if list
  179. if not isinstance(shape, Shape):
  180. if isinstance(shape, dict):
  181. raise TypeError(
  182. 'class %s has a self.shapes member "%s" that is just\n'
  183. 'a plain dictionary,\n%s\n'
  184. 'Did you mean to embed this dict in a Composition\n'
  185. 'object?' % (self.__class__.__name__, shape_name,
  186. str(shape)))
  187. elif isinstance(shape, (list,tuple)):
  188. raise TypeError(
  189. 'class %s has self.shapes member "%s" containing\n'
  190. 'a %s object %s,\n'
  191. 'Did you mean to embed this list in a Composition\n'
  192. 'object?' % (self.__class__.__name__, shape_name,
  193. type(shape), str(shape)))
  194. elif shape is None:
  195. raise TypeError(
  196. 'class %s has a self.shapes member "%s" that is None.\n'
  197. 'Some variable name is wrong, or some function\n'
  198. 'did not return the right object...' \
  199. % (self.__class__.__name__, shape_name))
  200. else:
  201. raise TypeError(
  202. 'class %s has a self.shapes member "%s" of %s which '
  203. 'is not a Shape object\n%s' %
  204. (self.__class__.__name__, shape_name, type(shape),
  205. pprint.pformat(self.shapes)))
  206. if isinstance(shape, Curve):
  207. shape.name = shape_name
  208. getattr(shape, func)(*args, **kwargs)
  209. def draw(self):
  210. self._for_all_shapes('draw')
  211. return self
  212. def draw_dimensions(self):
  213. if hasattr(self, 'dimensions'):
  214. for shape in self.dimensions:
  215. self.dimensions[shape].draw()
  216. return self
  217. else:
  218. #raise AttributeError('no self.dimensions dict for defining dimensions of class %s' % self.__classname__.__name__)
  219. return self
  220. def rotate(self, angle, center):
  221. is_sequence(center, length=2)
  222. self._for_all_shapes('rotate', angle, center)
  223. return self
  224. def translate(self, vec):
  225. is_sequence(vec, length=2)
  226. self._for_all_shapes('translate', vec)
  227. return self
  228. def scale(self, factor):
  229. self._for_all_shapes('scale', factor)
  230. return self
  231. def deform(self, displacement_function):
  232. self._for_all_shapes('deform', displacement_function)
  233. return self
  234. def minmax_coordinates(self, minmax=None):
  235. if minmax is None:
  236. minmax = {'xmin': 1E+20, 'xmax': -1E+20,
  237. 'ymin': 1E+20, 'ymax': -1E+20}
  238. self._for_all_shapes('minmax_coordinates', minmax)
  239. return minmax
  240. def recurse(self, name, indent=0):
  241. if not isinstance(self.shapes, dict):
  242. raise TypeError('recurse works only with dict self.shape, not %s' %
  243. type(self.shapes))
  244. space = ' '*indent
  245. print space, '%s: %s.shapes has entries' % \
  246. (self.__class__.__name__, name), \
  247. str(list(self.shapes.keys()))[1:-1]
  248. for shape in self.shapes:
  249. print space,
  250. print 'call %s.shapes["%s"].recurse("%s", %d)' % \
  251. (name, shape, shape, indent+2)
  252. self.shapes[shape].recurse(shape, indent+2)
  253. def graphviz_dot(self, name, classname=True):
  254. if not isinstance(self.shapes, dict):
  255. raise TypeError('recurse works only with dict self.shape, not %s' %
  256. type(self.shapes))
  257. dotfile = name + '.dot'
  258. pngfile = name + '.png'
  259. if classname:
  260. name = r"%s:\n%s" % (self.__class__.__name__, name)
  261. couplings = self._object_couplings(name, classname=classname)
  262. # Insert counter for similar names
  263. from collections import defaultdict
  264. count = defaultdict(lambda: 0)
  265. couplings2 = []
  266. for i in range(len(couplings)):
  267. parent, child = couplings[i]
  268. count[child] += 1
  269. parent += ' (%d)' % count[parent]
  270. child += ' (%d)' % count[child]
  271. couplings2.append((parent, child))
  272. print 'graphviz', couplings, count
  273. # Remove counter for names there are only one of
  274. for i in range(len(couplings)):
  275. parent2, child2 = couplings2[i]
  276. parent, child = couplings[i]
  277. if count[parent] > 1:
  278. parent = parent2
  279. if count[child] > 1:
  280. child = child2
  281. couplings[i] = (parent, child)
  282. print couplings
  283. f = open(dotfile, 'w')
  284. f.write('digraph G {\n')
  285. for parent, child in couplings:
  286. f.write('"%s" -> "%s";\n' % (parent, child))
  287. f.write('}\n')
  288. f.close()
  289. print 'Run dot -Tpng -o %s %s' % (pngfile, dotfile)
  290. def _object_couplings(self, parent, couplings=[], classname=True):
  291. """Find all couplings of parent and child objects in a figure."""
  292. for shape in self.shapes:
  293. if classname:
  294. childname = r"%s:\n%s" % \
  295. (self.shapes[shape].__class__.__name__, shape)
  296. else:
  297. childname = shape
  298. couplings.append((parent, childname))
  299. self.shapes[shape]._object_couplings(childname, couplings,
  300. classname)
  301. return couplings
  302. def set_linestyle(self, style):
  303. styles = ('solid', 'dashed', 'dashdot', 'dotted')
  304. if style not in styles:
  305. raise ValueError('%s: style=%s must be in %s' %
  306. (self.__class__.__name__ + '.set_linestyle:',
  307. style, str(styles)))
  308. self._for_all_shapes('set_linestyle', style)
  309. return self
  310. def set_linewidth(self, width):
  311. if not isinstance(width, int) and width >= 0:
  312. raise ValueError('%s: width=%s must be positive integer' %
  313. (self.__class__.__name__ + '.set_linewidth:',
  314. width))
  315. self._for_all_shapes('set_linewidth', width)
  316. return self
  317. def set_linecolor(self, color):
  318. if color in drawing_tool.line_colors:
  319. color = drawing_tool.line_colors[color]
  320. elif color in drawing_tool.line_colors.values():
  321. pass # color is ok
  322. else:
  323. raise ValueError('%s: invalid color "%s", must be in %s' %
  324. (self.__class__.__name__ + '.set_linecolor:',
  325. color, list(drawing_tool.line_colors.keys())))
  326. self._for_all_shapes('set_linecolor', color)
  327. return self
  328. def set_arrow(self, style):
  329. styles = ('->', '<-', '<->')
  330. if not style in styles:
  331. raise ValueError('%s: style=%s must be in %s' %
  332. (self.__class__.__name__ + '.set_arrow:',
  333. style, styles))
  334. self._for_all_shapes('set_arrow', style)
  335. return self
  336. def set_filled_curves(self, color='', pattern=''):
  337. if color in drawing_tool.line_colors:
  338. color = drawing_tool.line_colors[color]
  339. elif color in drawing_tool.line_colors.values():
  340. pass # color is ok
  341. else:
  342. raise ValueError('%s: invalid color "%s", must be in %s' %
  343. (self.__class__.__name__ + '.set_filled_curves:',
  344. color, list(drawing_tool.line_colors.keys())))
  345. self._for_all_shapes('set_filled_curves', color, pattern)
  346. return self
  347. def set_shadow(self, pixel_displacement=3):
  348. self._for_all_shapes('set_shadow', pixel_displacement)
  349. return self
  350. def show_hierarchy(self, indent=0, format='std'):
  351. """Recursive pretty print of hierarchy of objects."""
  352. if not isinstance(self.shapes, dict):
  353. print 'cannot print hierarchy when %s.shapes is not a dict' % \
  354. self.__class__.__name__
  355. s = ''
  356. if format == 'dict':
  357. s += '{'
  358. for shape in self.shapes:
  359. if format == 'dict':
  360. shape_str = repr(shape) + ':'
  361. elif format == 'plain':
  362. shape_str = shape
  363. else:
  364. shape_str = shape + ':'
  365. if format == 'dict' or format == 'plain':
  366. class_str = ''
  367. else:
  368. class_str = ' (%s)' % \
  369. self.shapes[shape].__class__.__name__
  370. s += '\n%s%s%s %s' % (
  371. ' '*indent,
  372. shape_str,
  373. class_str,
  374. self.shapes[shape].show_hierarchy(indent+4, format))
  375. if format == 'dict':
  376. s += '}'
  377. return s
  378. def __str__(self):
  379. """Display hierarchy with minimum information (just object names)."""
  380. return self.show_hierarchy(format='plain')
  381. def __repr__(self):
  382. """Display hierarchy as a dictionary."""
  383. return self.show_hierarchy(format='dict')
  384. #return pprint.pformat(self.shapes)
  385. class Curve(Shape):
  386. """General curve as a sequence of (x,y) coordintes."""
  387. def __init__(self, x, y):
  388. """
  389. `x`, `y`: arrays holding the coordinates of the curve.
  390. """
  391. self.x = asarray(x, dtype=float)
  392. self.y = asarray(y, dtype=float)
  393. #self.shapes must not be defined in this class
  394. #as self.shapes holds children objects:
  395. #Curve has no children (end leaf of self.shapes tree)
  396. self.linestyle = None
  397. self.linewidth = None
  398. self.linecolor = None
  399. self.fillcolor = None
  400. self.fillpattern = None
  401. self.arrow = None
  402. self.shadow = False
  403. self.name = None # name of object that this Curve represents
  404. def inside_plot_area(self, verbose=True):
  405. """Check that all coordinates are within drawing_tool's area."""
  406. xmin, xmax = self.x.min(), self.x.max()
  407. ymin, ymax = self.y.min(), self.y.max()
  408. t = drawing_tool
  409. inside = True
  410. if not hasattr(t, 'xmin'):
  411. return None # drawing area is not defined
  412. if xmin < t.xmin:
  413. inside = False
  414. if verbose:
  415. print 'x_min=%g < plot area x_min=%g' % (xmin, t.xmin)
  416. if xmax > t.xmax:
  417. inside = False
  418. if verbose:
  419. print 'x_max=%g > plot area x_max=%g' % (xmax, t.xmax)
  420. if ymin < t.ymin:
  421. inside = False
  422. if verbose:
  423. print 'y_min=%g < plot area y_min=%g' % (ymin, t.ymin)
  424. if ymax > t.ymax:
  425. inside = False
  426. if verbose:
  427. print 'y_max=%g > plot area y_max=%g' % (ymax, t.ymax)
  428. return inside
  429. def draw(self):
  430. """
  431. Send the curve to the plotting engine. That is, convert
  432. coordinate information in self.x and self.y, together
  433. with optional settings of linestyles, etc., to
  434. plotting commands for the chosen engine.
  435. """
  436. self.inside_plot_area()
  437. drawing_tool.plot_curve(
  438. self.x, self.y,
  439. self.linestyle, self.linewidth, self.linecolor,
  440. self.arrow, self.fillcolor, self.fillpattern,
  441. self.shadow, self.name)
  442. def rotate(self, angle, center):
  443. """
  444. Rotate all coordinates: `angle` is measured in degrees and
  445. (`x`,`y`) is the "origin" of the rotation.
  446. """
  447. angle = radians(angle)
  448. x, y = center
  449. c = cos(angle); s = sin(angle)
  450. xnew = x + (self.x - x)*c - (self.y - y)*s
  451. ynew = y + (self.x - x)*s + (self.y - y)*c
  452. self.x = xnew
  453. self.y = ynew
  454. return self
  455. def scale(self, factor):
  456. """Scale all coordinates by `factor`: ``x = factor*x``, etc."""
  457. self.x = factor*self.x
  458. self.y = factor*self.y
  459. return self
  460. def translate(self, vec):
  461. """Translate all coordinates by a vector `vec`."""
  462. self.x += vec[0]
  463. self.y += vec[1]
  464. return self
  465. def deform(self, displacement_function):
  466. """Displace all coordinates according to displacement_function(x,y)."""
  467. for i in range(len(self.x)):
  468. self.x[i], self.y[i] = displacement_function(self.x[i], self.y[i])
  469. return self
  470. def minmax_coordinates(self, minmax=None):
  471. if minmax is None:
  472. minmax = {'xmin': [], 'xmax': [], 'ymin': [], 'ymax': []}
  473. minmax['xmin'] = min(self.x.min(), minmax['xmin'])
  474. minmax['xmax'] = max(self.x.max(), minmax['xmax'])
  475. minmax['ymin'] = min(self.y.min(), minmax['ymin'])
  476. minmax['ymax'] = max(self.y.max(), minmax['ymax'])
  477. return minmax
  478. def recurse(self, name, indent=0):
  479. space = ' '*indent
  480. print space, 'reached "bottom" object %s' % \
  481. self.__class__.__name__
  482. def _object_couplings(self, parent, couplings=[], classname=True):
  483. return
  484. def set_linecolor(self, color):
  485. self.linecolor = color
  486. return self
  487. def set_linewidth(self, width):
  488. self.linewidth = width
  489. return self
  490. def set_linestyle(self, style):
  491. self.linestyle = style
  492. return self
  493. def set_arrow(self, style=None):
  494. self.arrow = style
  495. return self
  496. def set_filled_curves(self, color='', pattern=''):
  497. self.fillcolor = color
  498. self.fillpattern = pattern
  499. return self
  500. def set_shadow(self, pixel_displacement=3):
  501. self.shadow = pixel_displacement
  502. return self
  503. def show_hierarchy(self, indent=0, format='std'):
  504. if format == 'dict':
  505. return '"%s"' % str(self)
  506. elif format == 'plain':
  507. return ''
  508. else:
  509. return str(self)
  510. def __str__(self):
  511. """Compact pretty print of a Curve object."""
  512. s = '%d (x,y) coords' % self.x.size
  513. inside = self.inside_plot_area(verbose=False)
  514. if inside is None:
  515. pass # no info about the plotting area
  516. elif not inside:
  517. s += ', some coordinates are outside plotting area!\n'
  518. props = ('linecolor', 'linewidth', 'linestyle', 'arrow',
  519. 'fillcolor', 'fillpattern')
  520. for prop in props:
  521. value = getattr(self, prop)
  522. if value is not None:
  523. s += ' %s=%s' % (prop, repr(value))
  524. return s
  525. def __repr__(self):
  526. return str(self)
  527. class Spline(Shape):
  528. # Note: UnivariateSpline interpolation may not work if
  529. # the x[i] points are far from uniformly spaced
  530. def __init__(self, x, y, degree=3, resolution=501):
  531. from scipy.interpolate import UnivariateSpline
  532. self.smooth = UnivariateSpline(x, y, s=0, k=degree)
  533. self.xcoor = linspace(x[0], x[-1], resolution)
  534. ycoor = self.smooth(self.xcoor)
  535. self.shapes = {'smooth': Curve(self.xcoor, ycoor)}
  536. def geometric_features(self):
  537. s = self.shapes['smooth']
  538. return {'start': point(s.x[0], s.y[0]),
  539. 'end': point(s.x[-1], s.y[-1]),
  540. 'interval': [s.x[0], s.x[-1]]}
  541. def __call__(self, x):
  542. return self.smooth(x)
  543. # Can easily find the derivative and the integral as
  544. # self.smooth.derivative(n=1) and self.smooth.antiderivative()
  545. class SketchyFunc1(Spline):
  546. """
  547. A typical function curve used to illustrate an "arbitrary" function.
  548. """
  549. domain = [1, 6]
  550. def __init__(self, name=None, name_pos='start',
  551. xmin=1, xmax=6, ymin=2.4, ymax=5):
  552. x = array([1, 2, 3, 4, 5, 6])
  553. y = array([5, 3.5, 3.8, 3, 2.5, 2.4])
  554. # Scale x and y
  555. x = xmin - x.min() + x*(xmax - xmin)/(x.max()-x.min())
  556. y = ymin - y.min() + y*(ymax - ymin)/(y.max()-y.min())
  557. Spline.__init__(self, x, y)
  558. self.shapes['smooth'].set_linecolor('black')
  559. if name is not None:
  560. self.shapes['name'] = Text(name, self.geometric_features()[name_pos] + point(0,0.1))
  561. class SketchyFunc3(Spline):
  562. """
  563. A typical function curve used to illustrate an "arbitrary" function.
  564. """
  565. domain = [0, 6]
  566. def __init__(self, name=None, name_pos='start',
  567. xmin=0, xmax=6, ymin=0.5, ymax=3.8):
  568. x = array([0, 2, 3, 4, 5, 6])
  569. #y = array([2, 3.5, 3.8, 2, 2.5, 2.6])
  570. y = array([0.5, 3.5, 3.8, 2, 2.5, 3.5])
  571. # Scale x and y
  572. x = xmin - x.min() + x*(xmax - xmin)/(x.max()-x.min())
  573. y = ymin - y.min() + y*(ymax - ymin)/(y.max()-y.min())
  574. Spline.__init__(self, x, y)
  575. self.shapes['smooth'].set_linecolor('black')
  576. if name is not None:
  577. self.shapes['name'] = Text(name, self.geometric_features()[name_pos] + point(0,0.1))
  578. class SketchyFunc4(Spline):
  579. """
  580. A typical function curve used to illustrate an "arbitrary" function.
  581. Can be a companion function to SketchyFunc3.
  582. """
  583. domain = [1, 6]
  584. def __init__(self, name=None, name_pos='start',
  585. xmin=0, xmax=6, ymin=0.5, ymax=1.8):
  586. x = array([0, 2, 3, 4, 5, 6])
  587. y = array([1.5, 1.3, 0.7, 0.5, 0.6, 0.8])
  588. # Scale x and y
  589. x = xmin - x.min() + x*(xmax - xmin)/(x.max()-x.min())
  590. y = ymin - y.min() + y*(ymax - ymin)/(y.max()-y.min())
  591. Spline.__init__(self, x, y)
  592. self.shapes['smooth'].set_linecolor('black')
  593. if name is not None:
  594. self.shapes['name'] = Text(name, self.geometric_features()[name_pos] + point(0,0.1))
  595. class SketchyFunc2(Shape):
  596. """
  597. A typical function curve used to illustrate an "arbitrary" function.
  598. """
  599. domain = [0, 2.25]
  600. def __init__(self, name=None, name_pos='end',
  601. xmin=0, xmax=2.25, ymin=0.046679703125, ymax=1.259375):
  602. a = 0; b = 2.25
  603. resolution = 100
  604. x = linspace(a, b, resolution+1)
  605. f = self # for calling __call__
  606. y = f(x)
  607. # Scale x and y
  608. x = xmin - x.min() + x*(xmax - xmin)/(x.max()-x.min())
  609. y = ymin - y.min() + y*(ymax - ymin)/(y.max()-y.min())
  610. self.shapes = {'smooth': Curve(x, y)}
  611. self.shapes['smooth'].set_linecolor('black')
  612. pos = point(a, f(a)) if name_pos == 'start' else point(b, f(b))
  613. if name is not None:
  614. self.shapes['name'] = Text(name, pos + point(0,0.1))
  615. def __call__(self, x):
  616. return 0.5+x*(2-x)*(0.9-x) # on [0, 2.25]
  617. class Point(Shape):
  618. """A point (x,y) which can be rotated, translated, and scaled."""
  619. def __init__(self, x, y):
  620. self.x, self.y = x, y
  621. #self.shapes is not needed in this class
  622. def __add__(self, other):
  623. if isinstance(other, (list,tuple)):
  624. other = Point(other)
  625. return Point(self.x+other.x, self.y+other.y)
  626. # class Point is an abstract class - only subclasses are useful
  627. # and must implement draw
  628. def draw(self):
  629. raise NotImplementedError(
  630. 'class %s must implement the draw method' %
  631. self.__class__.__name__)
  632. def rotate(self, angle, center):
  633. """Rotate point an `angle` (in degrees) around (`x`,`y`)."""
  634. angle = angle*pi/180
  635. x, y = center
  636. c = cos(angle); s = sin(angle)
  637. xnew = x + (self.x - x)*c - (self.y - y)*s
  638. ynew = y + (self.x - x)*s + (self.y - y)*c
  639. self.x = xnew
  640. self.y = ynew
  641. return self
  642. def scale(self, factor):
  643. """Scale point coordinates by `factor`: ``x = factor*x``, etc."""
  644. self.x = factor*self.x
  645. self.y = factor*self.y
  646. return self
  647. def translate(self, vec):
  648. """Translate point by a vector `vec`."""
  649. self.x += vec[0]
  650. self.y += vec[1]
  651. return self
  652. def deform(self, displacement_function):
  653. """Displace coordinates according to displacement_function(x,y)."""
  654. for i in range(len(self.x)):
  655. self.x, self.y = displacement_function(self.x, self.y)
  656. return self
  657. def minmax_coordinates(self, minmax=None):
  658. if minmax is None:
  659. minmax = {'xmin': [], 'xmax': [], 'ymin': [], 'ymax': []}
  660. minmax['xmin'] = min(self.x, minmax['xmin'])
  661. minmax['xmax'] = max(self.x, minmax['xmax'])
  662. minmax['ymin'] = min(self.y, minmax['ymin'])
  663. minmax['ymax'] = max(self.y, minmax['ymax'])
  664. return minmax
  665. def recurse(self, name, indent=0):
  666. space = ' '*indent
  667. print space, 'reached "bottom" object %s' % \
  668. self.__class__.__name__
  669. def _object_couplings(self, parent, couplings=[], classname=True):
  670. return
  671. # No need for set_linecolor etc since self._for_all_shapes, which
  672. # is always called for these functions, makes a test and stops
  673. # calls if self.shapes is missing and the object is Point or Curve
  674. def show_hierarchy(self, indent=0, format='std'):
  675. s = '%s at (%g,%g)' % (self.__class__.__name__, self.x, self.y)
  676. if format == 'dict':
  677. return '"%s"' % s
  678. elif format == 'plain':
  679. return ''
  680. else:
  681. return s
  682. # no need to store input data as they are invalid after rotations etc.
  683. class Rectangle(Shape):
  684. """
  685. Rectangle specified by the point `lower_left_corner`, `width`,
  686. and `height`.
  687. """
  688. def __init__(self, lower_left_corner, width, height):
  689. is_sequence(lower_left_corner)
  690. p = arr2D(lower_left_corner) # short form
  691. x = [p[0], p[0] + width,
  692. p[0] + width, p[0], p[0]]
  693. y = [p[1], p[1], p[1] + height,
  694. p[1] + height, p[1]]
  695. self.shapes = {'rectangle': Curve(x,y)}
  696. # Dimensions
  697. dims = {
  698. 'width': Distance_wText(p + point(0, -height/5.),
  699. p + point(width, -height/5.),
  700. 'width'),
  701. 'height': Distance_wText(p + point(width + width/5., 0),
  702. p + point(width + width/5., height),
  703. 'height'),
  704. 'lower_left_corner': Text_wArrow('lower_left_corner',
  705. p - point(width/5., height/5.), p)
  706. }
  707. self.dimensions = dims
  708. def geometric_features(self):
  709. """
  710. Return dictionary with
  711. ==================== =============================================
  712. Attribute Description
  713. ==================== =============================================
  714. lower_left Lower left corner point.
  715. upper_left Upper left corner point.
  716. lower_right Lower right corner point.
  717. upper_right Upper right corner point.
  718. lower_mid Middle point on lower side.
  719. upper_mid Middle point on upper side.
  720. center Center point
  721. ==================== =============================================
  722. """
  723. r = self.shapes['rectangle']
  724. d = {'lower_left': point(r.x[0], r.y[0]),
  725. 'lower_right': point(r.x[1], r.y[1]),
  726. 'upper_right': point(r.x[2], r.y[2]),
  727. 'upper_left': point(r.x[3], r.y[3])}
  728. d['lower_mid'] = 0.5*(d['lower_left'] + d['lower_right'])
  729. d['upper_mid'] = 0.5*(d['upper_left'] + d['upper_right'])
  730. d['left_mid'] = 0.5*(d['lower_left'] + d['upper_left'])
  731. d['right_mid'] = 0.5*(d['lower_right'] + d['upper_right'])
  732. d['center'] = point(d['lower_mid'][0], d['left_mid'][1])
  733. return d
  734. class Triangle(Shape):
  735. """
  736. Triangle defined by its three vertices p1, p2, and p3.
  737. Recorded geometric features:
  738. ==================== =============================================
  739. Attribute Description
  740. ==================== =============================================
  741. p1, p2, p3 Corners as given to the constructor.
  742. ==================== =============================================
  743. """
  744. def __init__(self, p1, p2, p3):
  745. is_sequence(p1, p2, p3)
  746. x = [p1[0], p2[0], p3[0], p1[0]]
  747. y = [p1[1], p2[1], p3[1], p1[1]]
  748. self.shapes = {'triangle': Curve(x,y)}
  749. # Dimensions
  750. self.dimensions = {'p1': Text('p1', p1),
  751. 'p2': Text('p2', p2),
  752. 'p3': Text('p3', p3)}
  753. def geometric_features(self):
  754. t = self.shapes['triangle']
  755. return {'p1': point(t.x[0], t.y[0]),
  756. 'p2': point(t.x[1], t.y[1]),
  757. 'p3': point(t.x[2], t.y[2])}
  758. class Line(Shape):
  759. def __init__(self, start, end):
  760. is_sequence(start, end, length=2)
  761. x = [start[0], end[0]]
  762. y = [start[1], end[1]]
  763. self.shapes = {'line': Curve(x, y)}
  764. def geometric_features(self):
  765. line = self.shapes['line']
  766. return {'start': point(line.x[0], line.y[0]),
  767. 'end': point(line.x[1], line.y[1]),}
  768. def compute_formulas(self):
  769. x, y = self.shapes['line'].x, self.shapes['line'].y
  770. # Define equations for line:
  771. # y = a*x + b, x = c*y + d
  772. try:
  773. self.a = (y[1] - y[0])/(x[1] - x[0])
  774. self.b = y[0] - self.a*x[0]
  775. except ZeroDivisionError:
  776. # Vertical line, y is not a function of x
  777. self.a = None
  778. self.b = None
  779. try:
  780. if self.a is None:
  781. self.c = 0
  782. else:
  783. self.c = 1/float(self.a)
  784. if self.b is None:
  785. self.d = x[1]
  786. except ZeroDivisionError:
  787. # Horizontal line, x is not a function of y
  788. self.c = None
  789. self.d = None
  790. def compute_formulas(self):
  791. x, y = self.shapes['line'].x, self.shapes['line'].y
  792. tol = 1E-14
  793. # Define equations for line:
  794. # y = a*x + b, x = c*y + d
  795. if abs(x[1] - x[0]) > tol:
  796. self.a = (y[1] - y[0])/(x[1] - x[0])
  797. self.b = y[0] - self.a*x[0]
  798. else:
  799. # Vertical line, y is not a function of x
  800. self.a = None
  801. self.b = None
  802. if self.a is None:
  803. self.c = 0
  804. elif abs(self.a) > tol:
  805. self.c = 1/float(self.a)
  806. self.d = x[1]
  807. else: # self.a is 0
  808. # Horizontal line, x is not a function of y
  809. self.c = None
  810. self.d = None
  811. def __call__(self, x=None, y=None):
  812. """Given x, return y on the line, or given y, return x."""
  813. self.compute_formulas()
  814. if x is not None and self.a is not None:
  815. return self.a*x + self.b
  816. elif y is not None and self.c is not None:
  817. return self.c*y + self.d
  818. else:
  819. raise ValueError(
  820. 'Line.__call__(x=%s, y=%s) not meaningful' % \
  821. (x, y))
  822. def new_interval(self, x=None, y=None):
  823. """Redefine current Line to cover interval in x or y."""
  824. if x is not None:
  825. is_sequence(x, length=2)
  826. xL, xR = x
  827. new_line = Line((xL, self(x=xL)), (xR, self(x=xR)))
  828. elif y is not None:
  829. is_sequence(y, length=2)
  830. yL, yR = y
  831. new_line = Line((xL, self(y=xL)), (xR, self(y=xR)))
  832. self.shapes['line'] = new_line['line']
  833. return self
  834. # First implementation of class Circle
  835. class Circle(Shape):
  836. def __init__(self, center, radius, resolution=180):
  837. self.center, self.radius = center, radius
  838. self.resolution = resolution
  839. t = linspace(0, 2*pi, resolution+1)
  840. x0 = center[0]; y0 = center[1]
  841. R = radius
  842. x = x0 + R*cos(t)
  843. y = y0 + R*sin(t)
  844. self.shapes = {'circle': Curve(x, y)}
  845. def __call__(self, theta):
  846. """
  847. Return (x, y) point corresponding to angle theta.
  848. Not valid after a translation, rotation, or scaling.
  849. """
  850. return self.center[0] + self.radius*cos(theta), \
  851. self.center[1] + self.radius*sin(theta)
  852. class Arc(Shape):
  853. def __init__(self, center, radius,
  854. start_angle, arc_angle,
  855. resolution=180):
  856. is_sequence(center)
  857. # Must record some parameters for __call__
  858. self.center = arr2D(center)
  859. self.radius = radius
  860. self.start_angle = radians(start_angle)
  861. self.arc_angle = radians(arc_angle)
  862. t = linspace(self.start_angle,
  863. self.start_angle + self.arc_angle,
  864. resolution+1)
  865. x0 = center[0]; y0 = center[1]
  866. R = radius
  867. x = x0 + R*cos(t)
  868. y = y0 + R*sin(t)
  869. self.shapes = {'arc': Curve(x, y)}
  870. # Cannot set dimensions (Arc_wText recurses into this
  871. # constructor forever). Set in test_Arc instead.
  872. # Stored geometric features
  873. def geometric_features(self):
  874. a = self.shapes['arc']
  875. m = len(a.x)/2 # mid point in array
  876. d = {'start': point(a.x[0], a.y[0]),
  877. 'end': point(a.x[-1], a.y[-1]),
  878. 'mid': point(a.x[m], a.y[m])}
  879. return d
  880. def __call__(self, theta):
  881. """
  882. Return (x,y) point at start_angle + theta.
  883. Not valid after translation, rotation, or scaling.
  884. """
  885. theta = radians(theta)
  886. t = self.start_angle + theta
  887. x0 = self.center[0]
  888. y0 = self.center[1]
  889. R = self.radius
  890. x = x0 + R*cos(t)
  891. y = y0 + R*sin(t)
  892. return (x, y)
  893. # Alternative for small arcs: Parabola
  894. class Parabola(Shape):
  895. def __init__(self, start, mid, stop, resolution=21):
  896. self.p1, self.p2, self.p3 = start, mid, stop
  897. # y as function of x? (no point on line x=const?)
  898. tol = 1E-14
  899. if abs(self.p1[0] - self.p2[0]) > 1E-14 and \
  900. abs(self.p2[0] - self.p3[0]) > 1E-14 and \
  901. abs(self.p3[0] - self.p1[0]) > 1E-14:
  902. self.y_of_x = True
  903. else:
  904. self.y_of_x = False
  905. # x as function of y? (no point on line y=const?)
  906. tol = 1E-14
  907. if abs(self.p1[1] - self.p2[1]) > 1E-14 and \
  908. abs(self.p2[1] - self.p3[1]) > 1E-14 and \
  909. abs(self.p3[1] - self.p1[1]) > 1E-14:
  910. self.x_of_y = True
  911. else:
  912. self.x_of_y = False
  913. if self.y_of_x:
  914. x = linspace(start[0], end[0], resolution)
  915. y = self(x=x)
  916. elif self.x_of_y:
  917. y = linspace(start[1], end[1], resolution)
  918. x = self(y=y)
  919. else:
  920. raise ValueError(
  921. 'Parabola: two or more points lie on x=const '
  922. 'or y=const - not allowed')
  923. self.shapes = {'parabola': Curve(x, y)}
  924. def __call__(self, x=None, y=None):
  925. if x is not None and self.y_of_x:
  926. return self._L2x(self.p1, self.p2)*self.p3[1] + \
  927. self._L2x(self.p2, self.p3)*self.p1[1] + \
  928. self._L2x(self.p3, self.p1)*self.p2[1]
  929. elif y is not None and self.x_of_y:
  930. return self._L2y(self.p1, self.p2)*self.p3[0] + \
  931. self._L2y(self.p2, self.p3)*self.p1[0] + \
  932. self._L2y(self.p3, self.p1)*self.p2[0]
  933. else:
  934. raise ValueError(
  935. 'Parabola.__call__(x=%s, y=%s) not meaningful' % \
  936. (x, y))
  937. def _L2x(self, x, pi, pj, pk):
  938. return (x - pi[0])*(x - pj[0])/((pk[0] - pi[0])*(pk[0] - pj[0]))
  939. def _L2y(self, y, pi, pj, pk):
  940. return (y - pi[1])*(y - pj[1])/((pk[1] - pi[1])*(pk[1] - pj[1]))
  941. class Circle(Arc):
  942. def __init__(self, center, radius, resolution=180):
  943. Arc.__init__(self, center, radius, 0, 360, resolution)
  944. class Wall(Shape):
  945. def __init__(self, x, y, thickness, pattern='/', transparent=False):
  946. is_sequence(x, y, length=len(x))
  947. if isinstance(x[0], (tuple,list,ndarray)):
  948. # x is list of curves
  949. x1 = concatenate(x)
  950. else:
  951. x1 = asarray(x, float)
  952. if isinstance(y[0], (tuple,list,ndarray)):
  953. # x is list of curves
  954. y1 = concatenate(y)
  955. else:
  956. y1 = asarray(y, float)
  957. self.x1 = x1; self.y1 = y1
  958. # Displaced curve (according to thickness)
  959. x2 = x1
  960. y2 = y1 + thickness
  961. # Combine x1,y1 with x2,y2 reversed
  962. from numpy import concatenate
  963. x = concatenate((x1, x2[-1::-1]))
  964. y = concatenate((y1, y2[-1::-1]))
  965. wall = Curve(x, y)
  966. wall.set_filled_curves(color='white', pattern=pattern)
  967. x = [x1[-1]] + x2[-1::-1].tolist() + [x1[0]]
  968. y = [y1[-1]] + y2[-1::-1].tolist() + [y1[0]]
  969. self.shapes = {'wall': wall}
  970. from collections import OrderedDict
  971. self.shapes = OrderedDict()
  972. self.shapes['wall'] = wall
  973. if transparent:
  974. white_eraser = Curve(x, y)
  975. white_eraser.set_linecolor('white')
  976. self.shapes['eraser'] = white_eraser
  977. def geometric_features(self):
  978. d = {'start': point(self.x1[0], self.y1[0]),
  979. 'end': point(self.x1[-1], self.y1[-1])}
  980. return d
  981. class Wall2(Shape):
  982. def __init__(self, x, y, thickness, pattern='/'):
  983. is_sequence(x, y, length=len(x))
  984. if isinstance(x[0], (tuple,list,ndarray)):
  985. # x is list of curves
  986. x1 = concatenate(x)
  987. else:
  988. x1 = asarray(x, float)
  989. if isinstance(y[0], (tuple,list,ndarray)):
  990. # x is list of curves
  991. y1 = concatenate(y)
  992. else:
  993. y1 = asarray(y, float)
  994. self.x1 = x1; self.y1 = y1
  995. # Displaced curve (according to thickness)
  996. x2 = x1.copy()
  997. y2 = y1.copy()
  998. def displace(idx, idx_m, idx_p):
  999. # Find tangent and normal
  1000. tangent = point(x1[idx_m], y1[idx_m]) - point(x1[idx_p], y1[idx_p])
  1001. tangent = unit_vec(tangent)
  1002. normal = point(tangent[1], -tangent[0])
  1003. # Displace length "thickness" in "positive" normal direction
  1004. displaced_pt = point(x1[idx], y1[idx]) + thickness*normal
  1005. x2[idx], y2[idx] = displaced_pt
  1006. for i in range(1, len(x1)-1):
  1007. displace(i-1, i+1, i) # centered difference for normal comp.
  1008. # One-sided differences at the end points
  1009. i = 0
  1010. displace(i, i+1, i)
  1011. i = len(x1)-1
  1012. displace(i-1, i, i)
  1013. # Combine x1,y1 with x2,y2 reversed
  1014. from numpy import concatenate
  1015. x = concatenate((x1, x2[-1::-1]))
  1016. y = concatenate((y1, y2[-1::-1]))
  1017. wall = Curve(x, y)
  1018. wall.set_filled_curves(color='white', pattern=pattern)
  1019. x = [x1[-1]] + x2[-1::-1].tolist() + [x1[0]]
  1020. y = [y1[-1]] + y2[-1::-1].tolist() + [y1[0]]
  1021. self.shapes['wall'] = wall
  1022. def geometric_features(self):
  1023. d = {'start': point(self.x1[0], self.y1[0]),
  1024. 'end': point(self.x1[-1], self.y1[-1])}
  1025. return d
  1026. class VelocityProfile(Shape):
  1027. def __init__(self, start, height, profile, num_arrows, scaling=1):
  1028. # vx, vy = profile(y)
  1029. shapes = {}
  1030. # Draw left line
  1031. shapes['start line'] = Line(start, (start[0], start[1]+height))
  1032. # Draw velocity arrows
  1033. dy = float(height)/(num_arrows-1)
  1034. x = start[0]
  1035. y = start[1]
  1036. r = profile(y) # Test on return type
  1037. if not isinstance(r, (list,tuple,ndarray)) and len(r) != 2:
  1038. raise TypeError('VelocityProfile constructor: profile(y) function must return velocity vector (vx,vy), not %s' % type(r))
  1039. for i in range(num_arrows):
  1040. y = start[1] + i*dy
  1041. vx, vy = profile(y)
  1042. if abs(vx) < 1E-8:
  1043. continue
  1044. vx *= scaling
  1045. vy *= scaling
  1046. arr = Arrow1((x,y), (x+vx, y+vy), '->')
  1047. shapes['arrow%d' % i] = arr
  1048. # Draw smooth profile
  1049. xs = []
  1050. ys = []
  1051. n = 100
  1052. dy = float(height)/n
  1053. for i in range(n+2):
  1054. y = start[1] + i*dy
  1055. vx, vy = profile(y)
  1056. vx *= scaling
  1057. vy *= scaling
  1058. xs.append(x+vx)
  1059. ys.append(y+vy)
  1060. shapes['smooth curve'] = Curve(xs, ys)
  1061. self.shapes = shapes
  1062. class Arrow1(Shape):
  1063. """Draw an arrow as Line with arrow."""
  1064. def __init__(self, start, end, style='->'):
  1065. arrow = Line(start, end)
  1066. arrow.set_arrow(style)
  1067. # Note:
  1068. self.shapes = {'arrow': arrow}
  1069. def geometric_features(self):
  1070. return self.shapes['arrow'].geometric_features()
  1071. class Arrow3(Shape):
  1072. """
  1073. Build a vertical line and arrow head from Line objects.
  1074. Then rotate `rotation_angle`.
  1075. """
  1076. def __init__(self, start, length, rotation_angle=0):
  1077. self.bottom = start
  1078. self.length = length
  1079. self.angle = rotation_angle
  1080. top = (self.bottom[0], self.bottom[1] + self.length)
  1081. main = Line(self.bottom, top)
  1082. #head_length = self.length/8.0
  1083. head_length = drawing_tool.xrange/50.
  1084. head_degrees = radians(30)
  1085. head_left_pt = (top[0] - head_length*sin(head_degrees),
  1086. top[1] - head_length*cos(head_degrees))
  1087. head_right_pt = (top[0] + head_length*sin(head_degrees),
  1088. top[1] - head_length*cos(head_degrees))
  1089. head_left = Line(head_left_pt, top)
  1090. head_right = Line(head_right_pt, top)
  1091. head_left.set_linestyle('solid')
  1092. head_right.set_linestyle('solid')
  1093. self.shapes = {'line': main, 'head left': head_left,
  1094. 'head right': head_right}
  1095. # rotate goes through self.shapes so self.shapes
  1096. # must be initialized first
  1097. self.rotate(rotation_angle, start)
  1098. def geometric_features(self):
  1099. return self.shapes['line'].geometric_features()
  1100. class Text(Point):
  1101. """
  1102. Place `text` at the (x,y) point `position`, with the given
  1103. fontsize (0 indicates that the default fontsize set in drawing_tool
  1104. is to be used). The text is centered around `position` if `alignment` is
  1105. 'center'; if 'left', the text starts at `position`, and if
  1106. 'right', the right and of the text is located at `position`.
  1107. """
  1108. def __init__(self, text, position, alignment='center', fontsize=0):
  1109. is_sequence(position)
  1110. is_sequence(position, length=2, can_be_None=True)
  1111. self.text = text
  1112. self.position = position
  1113. self.alignment = alignment
  1114. self.fontsize = fontsize
  1115. Point.__init__(self, position[0], position[1])
  1116. #no need for self.shapes here
  1117. def draw(self):
  1118. drawing_tool.text(self.text, (self.x, self.y),
  1119. self.alignment, self.fontsize)
  1120. def __str__(self):
  1121. return 'text "%s" at (%g,%g)' % (self.text, self.x, self.y)
  1122. def __repr__(self):
  1123. return str(self)
  1124. class Text_wArrow(Text):
  1125. """
  1126. As class Text, but an arrow is drawn from the mid part of the text
  1127. to some point `arrow_tip`.
  1128. """
  1129. def __init__(self, text, position, arrow_tip,
  1130. alignment='center', fontsize=0):
  1131. is_sequence(arrow_tip, length=2, can_be_None=True)
  1132. is_sequence(position)
  1133. self.arrow_tip = arrow_tip
  1134. Text.__init__(self, text, position, alignment, fontsize)
  1135. def draw(self):
  1136. drawing_tool.text(self.text, self.position,
  1137. self.alignment, self.fontsize,
  1138. self.arrow_tip)
  1139. def __str__(self):
  1140. return 'annotation "%s" at (%g,%g) with arrow to (%g,%g)' % \
  1141. (self.text, self.x, self.y,
  1142. self.arrow_tip[0], self.arrow_tip[1])
  1143. def __repr__(self):
  1144. return str(self)
  1145. class Axis(Shape):
  1146. def __init__(self, start, length, label,
  1147. rotation_angle=0, fontsize=0,
  1148. label_spacing=1./45, label_alignment='left'):
  1149. """
  1150. Draw axis from start with `length` to the right
  1151. (x axis). Place label at the end of the arrow tip.
  1152. Then return `rotation_angle` (in degrees).
  1153. The `label_spacing` denotes the space between the label
  1154. and the arrow tip as a fraction of the length of the plot
  1155. in x direction. With `label_alignment` one can place
  1156. the axis label text such that the arrow tip is to the 'left',
  1157. 'right', or 'center' with respect to the text field.
  1158. The `label_spacing` and `label_alignment` parameters can
  1159. be used to fine-tune the location of the label.
  1160. """
  1161. # Arrow is vertical arrow, make it horizontal
  1162. arrow = Arrow3(start, length, rotation_angle=-90)
  1163. arrow.rotate(rotation_angle, start)
  1164. if isinstance(label_spacing, (list,tuple)) and len(label_spacing) == 2:
  1165. x_spacing = drawing_tool.xrange*label_spacing[0]
  1166. y_spacing = drawing_tool.yrange*label_spacing[1]
  1167. elif isinstance(label_spacing, (int,float)):
  1168. # just x spacing
  1169. x_spacing = drawing_tool.xrange*label_spacing
  1170. y_spacing = 0
  1171. # should increase spacing for downward pointing axis
  1172. label_pos = [start[0] + length + x_spacing, start[1] + y_spacing]
  1173. label = Text(label, position=label_pos, fontsize=fontsize)
  1174. label.rotate(rotation_angle, start)
  1175. self.shapes = {'arrow': arrow, 'label': label}
  1176. def geometric_features(self):
  1177. return self.shapes['arrow'].geometric_features()
  1178. # Maybe Axis3 with label below/above?
  1179. class Force(Arrow1):
  1180. """
  1181. Indication of a force by an arrow and a text (symbol). Draw an
  1182. arrow, starting at `start` and with the tip at `end`. The symbol
  1183. is placed at `text_pos`, which can be 'start', 'end' or the
  1184. coordinates of a point. If 'end' or 'start', the text is placed at
  1185. a distance `text_spacing` times the width of the total plotting
  1186. area away from the specified point.
  1187. """
  1188. def __init__(self, start, end, text, text_spacing=1./60,
  1189. fontsize=0, text_pos='start', text_alignment='center'):
  1190. Arrow1.__init__(self, start, end, style='->')
  1191. spacing = drawing_tool.xrange*text_spacing
  1192. start, end = arr2D(start), arr2D(end)
  1193. # Two cases: label at bottom of line or top, need more
  1194. # spacing if bottom
  1195. downward = (end-start)[1] < 0
  1196. upward = not downward # for easy code reading
  1197. if isinstance(text_pos, str):
  1198. if text_pos == 'start':
  1199. spacing_dir = unit_vec(start - end)
  1200. if upward:
  1201. spacing *= 1.7
  1202. text_pos = start + spacing*spacing_dir
  1203. elif text_pos == 'end':
  1204. spacing_dir = unit_vec(end - start)
  1205. if downward:
  1206. spacing *= 1.7
  1207. text_pos = end + spacing*spacing_dir
  1208. self.shapes['text'] = Text(text, text_pos, fontsize=fontsize,
  1209. alignment=text_alignment)
  1210. def geometric_features(self):
  1211. d = Arrow1.geometric_features(self)
  1212. d['symbol_location'] = self.shapes['text'].position
  1213. return d
  1214. class Axis2(Force):
  1215. def __init__(self, start, length, label,
  1216. rotation_angle=0, fontsize=0,
  1217. label_spacing=1./45, label_alignment='left'):
  1218. direction = point(cos(radians(rotation_angle)),
  1219. sin(radians(rotation_angle)))
  1220. Force.__init__(start=start, end=length*direction, text=label,
  1221. text_spacing=label_spacing,
  1222. fontsize=fontsize, text_pos='end',
  1223. text_alignment=label_alignment)
  1224. # Substitute text by label for axis
  1225. self.shapes['label'] = self.shapes['text']
  1226. del self.shapes['text']
  1227. # geometric features from Force is ok
  1228. class Gravity(Axis):
  1229. """Downward-pointing gravity arrow with the symbol g."""
  1230. def __init__(self, start, length, fontsize=0):
  1231. Axis.__init__(self, start, length, '$g$', below=False,
  1232. rotation_angle=-90, label_spacing=1./30,
  1233. fontsize=fontsize)
  1234. self.shapes['arrow'].set_linecolor('black')
  1235. class Gravity(Force):
  1236. """Downward-pointing gravity arrow with the symbol g."""
  1237. def __init__(self, start, length, text='$g$', fontsize=0):
  1238. Force.__init__(self, start, (start[0], start[1]-length),
  1239. text, text_spacing=1./60,
  1240. fontsize=0, text_pos='end')
  1241. self.shapes['arrow'].set_linecolor('black')
  1242. class Distance_wText(Shape):
  1243. """
  1244. Arrow <-> with text (usually a symbol) at the midpoint, used for
  1245. identifying a some distance in a figure. The text is placed
  1246. slightly to the right of vertical-like arrows, with text displaced
  1247. `text_spacing` times to total distance in x direction of the plot
  1248. area. The text is by default aligned 'left' in this case. For
  1249. horizontal-like arrows, the text is placed the same distance
  1250. above, but aligned 'center' by default (when `alignment` is None).
  1251. """
  1252. def __init__(self, start, end, text, fontsize=0, text_spacing=1/60.,
  1253. alignment=None, text_pos='mid'):
  1254. start = arr2D(start)
  1255. end = arr2D(end)
  1256. # Decide first if we have a vertical or horizontal arrow
  1257. vertical = abs(end[0]-start[0]) < 2*abs(end[1]-start[1])
  1258. if vertical:
  1259. # Assume end above start
  1260. if end[1] < start[1]:
  1261. start, end = end, start
  1262. if alignment is None:
  1263. alignment = 'left'
  1264. else: # horizontal arrow
  1265. # Assume start to the right of end
  1266. if start[0] < end[0]:
  1267. start, end = end, start
  1268. if alignment is None:
  1269. alignment = 'center'
  1270. tangent = end - start
  1271. # Tangeng goes always to the left and upward
  1272. normal = unit_vec([tangent[1], -tangent[0]])
  1273. mid = 0.5*(start + end) # midpoint of start-end line
  1274. if text_pos == 'mid':
  1275. text_pos = mid + normal*drawing_tool.xrange*text_spacing
  1276. text = Text(text, text_pos, fontsize=fontsize,
  1277. alignment=alignment)
  1278. else:
  1279. is_sequence(text_pos, length=2)
  1280. text = Text_wArrow(text, text_pos, mid, alignment='left',
  1281. fontsize=fontsize)
  1282. arrow = Arrow1(start, end, style='<->')
  1283. arrow.set_linecolor('black')
  1284. arrow.set_linewidth(1)
  1285. self.shapes = {'arrow': arrow, 'text': text}
  1286. def geometric_features(self):
  1287. d = self.shapes['arrow'].geometric_features()
  1288. d['text_position'] = self.shapes['text'].position
  1289. return d
  1290. class Arc_wText(Shape):
  1291. def __init__(self, text, center, radius,
  1292. start_angle, arc_angle, fontsize=0,
  1293. resolution=180, text_spacing=1/60.):
  1294. arc = Arc(center, radius, start_angle, arc_angle,
  1295. resolution)
  1296. mid = arr2D(arc(arc_angle/2.))
  1297. normal = unit_vec(mid - arr2D(center))
  1298. text_pos = mid + normal*drawing_tool.xrange*text_spacing
  1299. self.shapes = {'arc': arc,
  1300. 'text': Text(text, text_pos, fontsize=fontsize)}
  1301. class Composition(Shape):
  1302. def __init__(self, shapes):
  1303. """shapes: list or dict of Shape objects."""
  1304. if isinstance(shapes, (tuple,list)):
  1305. # Convert to dict using the type of the list element as key
  1306. # (add a counter to make the keys unique)
  1307. shapes = {s.__class__.__name__ + '_' + str(i): s
  1308. for i, s in enumerate(shapes)}
  1309. self.shapes = shapes
  1310. # can make help methods: Line.midpoint, Line.normal(pt, dir='left') -> (x,y)
  1311. # list annotations in each class? contains extra annotations for explaining
  1312. # important parameters to the constructor, e.g., Line.annotations holds
  1313. # start and end as Text objects. Shape.demo calls shape.draw and
  1314. # for annotation in self.demo: annotation.draw() YES!
  1315. # Can make overall demo of classes by making objects and calling demo
  1316. # Could include demo fig in each constructor
  1317. class SimplySupportedBeam(Shape):
  1318. def __init__(self, pos, size):
  1319. pos = arr2D(pos)
  1320. P0 = (pos[0] - size/2., pos[1]-size)
  1321. P1 = (pos[0] + size/2., pos[1]-size)
  1322. triangle = Triangle(P0, P1, pos)
  1323. gap = size/5.
  1324. h = size/4. # height of rectangle
  1325. P2 = (P0[0], P0[1]-gap-h)
  1326. rectangle = Rectangle(P2, size, h).set_filled_curves(pattern='/')
  1327. self.shapes = {'triangle': triangle, 'rectangle': rectangle}
  1328. self.dimensions = {'pos': Text('pos', pos),
  1329. 'size': Distance_wText((P2[0], P2[1]-size),
  1330. (P2[0]+size, P2[1]-size),
  1331. 'size')}
  1332. def geometric_features(self):
  1333. t = self.shapes['triangle']
  1334. r = self.shapes['rectangle']
  1335. d = {'pos': point(t.x[2], t.y[2]), # "p2"/pos
  1336. 'mid_support': r.geometric_features()['lower_mid']}
  1337. return d
  1338. class ConstantBeamLoad(Shape):
  1339. """
  1340. Downward-pointing arrows indicating a vertical load.
  1341. The arrows are of equal length and filling a rectangle
  1342. specified as in the :class:`Rectangle` class.
  1343. Recorded geometric features:
  1344. ==================== =============================================
  1345. Attribute Description
  1346. ==================== =============================================
  1347. mid_point Middle point at the top of the row of
  1348. arrows (often used for positioning a text).
  1349. ==================== =============================================
  1350. """
  1351. def __init__(self, lower_left_corner, width, height, num_arrows=10):
  1352. box = Rectangle(lower_left_corner, width, height)
  1353. self.shapes = {'box': box}
  1354. dx = float(width)/(num_arrows-1)
  1355. y_top = lower_left_corner[1] + height
  1356. y_tip = lower_left_corner[1]
  1357. for i in range(num_arrows):
  1358. x = lower_left_corner[0] + i*dx
  1359. self.shapes['arrow%d' % i] = Arrow1((x, y_top), (x, y_tip))
  1360. def geometric_features(self):
  1361. return {'mid_top': self.shapes['box'].geometric_features()['upper_mid']}
  1362. class Moment(Arc_wText):
  1363. def __init__(self, text, center, radius,
  1364. left=True, counter_clockwise=True,
  1365. fontsize=0, text_spacing=1/60.):
  1366. style = '->' if counter_clockwise else '<-'
  1367. start_angle = 90 if left else -90
  1368. Arc_wText.__init__(self, text, center, radius,
  1369. start_angle=start_angle,
  1370. arc_angle=180, fontsize=fontsize,
  1371. text_spacing=text_spacing,
  1372. resolution=180)
  1373. self.shapes['arc'].set_arrow(style)
  1374. class Wheel(Shape):
  1375. def __init__(self, center, radius, inner_radius=None, nlines=10):
  1376. if inner_radius is None:
  1377. inner_radius = radius/5.0
  1378. outer = Circle(center, radius)
  1379. inner = Circle(center, inner_radius)
  1380. lines = []
  1381. # Draw nlines+1 since the first and last coincide
  1382. # (then nlines lines will be visible)
  1383. t = linspace(0, 2*pi, self.nlines+1)
  1384. Ri = inner_radius; Ro = radius
  1385. x0 = center[0]; y0 = center[1]
  1386. xinner = x0 + Ri*cos(t)
  1387. yinner = y0 + Ri*sin(t)
  1388. xouter = x0 + Ro*cos(t)
  1389. youter = y0 + Ro*sin(t)
  1390. lines = [Line((xi,yi),(xo,yo)) for xi, yi, xo, yo in \
  1391. zip(xinner, yinner, xouter, youter)]
  1392. self.shapes = {'inner': inner, 'outer': outer,
  1393. 'spokes': Composition(
  1394. {'spoke%d' % i: lines[i]
  1395. for i in range(len(lines))})}
  1396. class SineWave(Shape):
  1397. def __init__(self, xstart, xstop,
  1398. wavelength, amplitude, mean_level):
  1399. self.xstart = xstart
  1400. self.xstop = xstop
  1401. self.wavelength = wavelength
  1402. self.amplitude = amplitude
  1403. self.mean_level = mean_level
  1404. npoints = (self.xstop - self.xstart)/(self.wavelength/61.0)
  1405. x = linspace(self.xstart, self.xstop, npoints)
  1406. k = 2*pi/self.wavelength # frequency
  1407. y = self.mean_level + self.amplitude*sin(k*x)
  1408. self.shapes = {'waves': Curve(x,y)}
  1409. class Spring(Shape):
  1410. """
  1411. Specify a *vertical* spring, starting at `start` and with `length`
  1412. as total vertical length. In the middle of the spring there are
  1413. `num_windings` circular windings to illustrate the spring. If
  1414. `teeth` is true, the spring windings look like saw teeth,
  1415. otherwise the windings are smooth circles. The parameters `width`
  1416. (total width of spring) and `bar_length` (length of first and last
  1417. bar are given sensible default values if they are not specified
  1418. (these parameters can later be extracted as attributes, see table
  1419. below).
  1420. """
  1421. spring_fraction = 1./2 # fraction of total length occupied by spring
  1422. def __init__(self, start, length, width=None, bar_length=None,
  1423. num_windings=11, teeth=False):
  1424. B = start
  1425. n = num_windings - 1 # n counts teeth intervals
  1426. if n <= 6:
  1427. n = 7
  1428. # n must be odd:
  1429. if n % 2 == 0:
  1430. n = n+1
  1431. L = length
  1432. if width is None:
  1433. w = L/10.
  1434. else:
  1435. w = width/2.0
  1436. s = bar_length
  1437. # [0, x, L-x, L], f = (L-2*x)/L
  1438. # x = L*(1-f)/2.
  1439. # B: start point
  1440. # w: half-width
  1441. # L: total length
  1442. # s: length of first bar
  1443. # P0: start of dashpot (B[0]+s)
  1444. # P1: end of dashpot
  1445. # P2: end point
  1446. shapes = {}
  1447. if s is None:
  1448. f = Spring.spring_fraction
  1449. s = L*(1-f)/2. # start of spring
  1450. self.bar_length = s # record
  1451. self.width = 2*w
  1452. P0 = (B[0], B[1] + s)
  1453. P1 = (B[0], B[1] + L-s)
  1454. P2 = (B[0], B[1] + L)
  1455. if s >= L:
  1456. raise ValueError('length of first bar: %g is larger than total length: %g' % (s, L))
  1457. shapes['bar1'] = Line(B, P0)
  1458. spring_length = L - 2*s
  1459. t = spring_length/n # height increment per winding
  1460. if teeth:
  1461. resolution = 4
  1462. else:
  1463. resolution = 90
  1464. q = linspace(0, n, n*resolution + 1)
  1465. x = P0[0] + w*sin(2*pi*q)
  1466. y = P0[1] + q*t
  1467. shapes['sprial'] = Curve(x, y)
  1468. shapes['bar2'] = Line(P1,P2)
  1469. self.shapes = shapes
  1470. # Dimensions
  1471. start = Text_wArrow('start', (B[0]-1.5*w,B[1]-1.5*w), B)
  1472. width = Distance_wText((B[0]-w, B[1]-3.5*w), (B[0]+w, B[1]-3.5*w),
  1473. 'width')
  1474. length = Distance_wText((B[0]+3*w, B[1]), (B[0]+3*w, B[1]+L),
  1475. 'length')
  1476. num_windings = Text_wArrow('num_windings',
  1477. (B[0]+2*w,P2[1]+w),
  1478. (B[0]+1.2*w, B[1]+L/2.))
  1479. blength1 = Distance_wText((B[0]-2*w, B[1]), (B[0]-2*w, P0[1]),
  1480. 'bar_length',
  1481. text_pos=(P0[0]-7*w, P0[1]+w))
  1482. blength2 = Distance_wText((P1[0]-2*w, P1[1]), (P2[0]-2*w, P2[1]),
  1483. 'bar_length',
  1484. text_pos=(P2[0]-7*w, P2[1]+w))
  1485. dims = {'start': start, 'width': width, 'length': length,
  1486. 'num_windings': num_windings, 'bar_length1': blength1,
  1487. 'bar_length2': blength2}
  1488. self.dimensions = dims
  1489. def geometric_features(self):
  1490. """
  1491. Recorded geometric features:
  1492. ==================== =============================================
  1493. Attribute Description
  1494. ==================== =============================================
  1495. start Start point of spring.
  1496. end End point of spring.
  1497. width Total width of spring.
  1498. bar_length Length of first (and last) bar part.
  1499. ==================== =============================================
  1500. """
  1501. b1 = self.shapes['bar1']
  1502. d = {'start': b1.geometric_features()['start'],
  1503. 'end': self.shapes['bar2'].geometric_features()['end'],
  1504. 'bar_length': self.bar_length,
  1505. 'width': self.width}
  1506. return d
  1507. class Dashpot(Shape):
  1508. """
  1509. Specify a vertical dashpot of height `total_length` and `start` as
  1510. bottom/starting point. The first bar part has length `bar_length`.
  1511. Then comes the dashpot as a rectangular construction of total
  1512. width `width` and height `dashpot_length`. The position of the
  1513. piston inside the rectangular dashpot area is given by
  1514. `piston_pos`, which is the distance between the first bar (given
  1515. by `bar_length`) to the piston.
  1516. If some of `dashpot_length`, `bar_length`, `width` or `piston_pos`
  1517. are not given, suitable default values are calculated. Their
  1518. values can be extracted as keys in the dict returned from
  1519. ``geometric_features``.
  1520. """
  1521. dashpot_fraction = 1./2 # fraction of total_length
  1522. piston_gap_fraction = 1./6 # fraction of width
  1523. piston_thickness_fraction = 1./8 # fraction of dashplot_length
  1524. def __init__(self, start, total_length, bar_length=None,
  1525. width=None, dashpot_length=None, piston_pos=None):
  1526. B = start
  1527. L = total_length
  1528. if width is None:
  1529. w = L/10. # total width 1/5 of length
  1530. else:
  1531. w = width/2.0
  1532. s = bar_length
  1533. # [0, x, L-x, L], f = (L-2*x)/L
  1534. # x = L*(1-f)/2.
  1535. # B: start point
  1536. # w: half-width
  1537. # L: total length
  1538. # s: length of first bar
  1539. # P0: start of dashpot (B[0]+s)
  1540. # P1: end of dashpot
  1541. # P2: end point
  1542. shapes = {}
  1543. # dashpot is P0-P1 in y and width 2*w
  1544. if dashpot_length is None:
  1545. if s is None:
  1546. f = Dashpot.dashpot_fraction
  1547. s = L*(1-f)/2. # default
  1548. P1 = (B[0], B[1]+L-s)
  1549. dashpot_length = f*L
  1550. else:
  1551. if s is None:
  1552. f = 1./2 # the bar lengths are taken as f*dashpot_length
  1553. s = f*dashpot_length # default
  1554. P1 = (B[0], B[1]+s+dashpot_length)
  1555. P0 = (B[0], B[1]+s)
  1556. P2 = (B[0], B[1]+L)
  1557. if P2[1] > P1[1] > P0[1]:
  1558. pass # ok
  1559. else:
  1560. raise ValueError('Dashpot has inconsistent dimensions! start: %g, dashpot begin: %g, dashpot end: %g, very end: %g' % (B[1], P0[1], P1[1], P2[1]))
  1561. shapes['line start'] = Line(B, P0)
  1562. shapes['pot'] = Curve([P1[0]-w, P0[0]-w, P0[0]+w, P1[0]+w],
  1563. [P1[1], P0[1], P0[1], P1[1]])
  1564. piston_thickness = dashpot_length*Dashpot.piston_thickness_fraction
  1565. if piston_pos is None:
  1566. piston_pos = 1/3.*dashpot_length
  1567. if piston_pos < 0:
  1568. piston_pos = 0
  1569. elif piston_pos > dashpot_length:
  1570. piston_pos = dashpot_length - piston_thickness
  1571. abs_piston_pos = P0[1] + piston_pos
  1572. gap = w*Dashpot.piston_gap_fraction
  1573. shapes['piston'] = Composition(
  1574. {'line': Line(P2, (B[0], abs_piston_pos + piston_thickness)),
  1575. 'rectangle': Rectangle((B[0] - w+gap, abs_piston_pos),
  1576. 2*w-2*gap, piston_thickness),
  1577. })
  1578. shapes['piston']['rectangle'].set_filled_curves(pattern='X')
  1579. self.shapes = shapes
  1580. self.bar_length = s
  1581. self.width = 2*w
  1582. self.piston_pos = piston_pos
  1583. self.dashpot_length = dashpot_length
  1584. # Dimensions
  1585. start = Text_wArrow('start', (B[0]-1.5*w,B[1]-1.5*w), B)
  1586. width = Distance_wText((B[0]-w, B[1]-3.5*w), (B[0]+w, B[1]-3.5*w),
  1587. 'width')
  1588. dplength = Distance_wText((B[0]+2*w, P0[1]), (B[0]+2*w, P1[1]),
  1589. 'dashpot_length', text_pos=(B[0]+w,B[1]-w))
  1590. blength = Distance_wText((B[0]-2*w, B[1]), (B[0]-2*w, P0[1]),
  1591. 'bar_length', text_pos=(B[0]-6*w,P0[1]-w))
  1592. ppos = Distance_wText((B[0]-2*w, P0[1]), (B[0]-2*w, P0[1]+piston_pos),
  1593. 'piston_pos', text_pos=(B[0]-6*w,P0[1]+piston_pos-w))
  1594. tlength = Distance_wText((B[0]+4*w, B[1]), (B[0]+4*w, B[1]+L),
  1595. 'total_length',
  1596. text_pos=(B[0]+4.5*w, B[1]+L-2*w))
  1597. line = Line((B[0]+w, abs_piston_pos), (B[0]+7*w, abs_piston_pos)).set_linestyle('dashed').set_linecolor('black').set_linewidth(1)
  1598. pp = Text('abs_piston_pos', (B[0]+7*w, abs_piston_pos), alignment='left')
  1599. dims = {'start': start, 'width': width, 'dashpot_length': dplength,
  1600. 'bar_length': blength, 'total_length': tlength,
  1601. 'piston_pos': ppos,}
  1602. #'abs_piston_pos': Composition({'line': line, 'text': pp})}
  1603. self.dimensions = dims
  1604. def geometric_features(self):
  1605. """
  1606. Recorded geometric features:
  1607. ==================== =============================================
  1608. Attribute Description
  1609. ==================== =============================================
  1610. start Start point of dashpot.
  1611. end End point of dashpot.
  1612. bar_length Length of first bar (from start to spring).
  1613. dashpot_length Length of dashpot middle part.
  1614. width Total width of dashpot.
  1615. piston_pos Position of piston in dashpot, relative to
  1616. start[1] + bar_length.
  1617. ==================== =============================================
  1618. """
  1619. d = {'start': self.shapes['line start'].geometric_features()['start'],
  1620. 'end': self.shapes['piston']['line'].geometric_features()['start'],
  1621. 'bar_length': self.bar_length,
  1622. 'piston_pos': self.piston_pos,
  1623. 'width': self.width,
  1624. 'dashpot_length': self.dashpot_length,
  1625. }
  1626. return d
  1627. class Wavy(Shape):
  1628. """
  1629. A wavy graph consisting of a user-given main curve y=f(x) with
  1630. additional sinusoidal waves of given (constant) amplitude,
  1631. but varying wavelength (a characteristic wavelength is specified).
  1632. """
  1633. def __init__(self, main_curve, interval, wavelength_of_perturbations,
  1634. amplitude_of_perturbations, smoothness):
  1635. """
  1636. ============================ ====================================
  1637. Name Description
  1638. ============================ ====================================
  1639. main_curve f(x) Python function
  1640. interval interval for main_curve
  1641. wavelength_of_perturbations dominant wavelength perturbed waves
  1642. amplitude_of_perturbations amplitude of perturbed waves
  1643. smoothness in [0, 1]: smooth=0, rough=1
  1644. ============================ ====================================
  1645. """
  1646. xmin, xmax = interval
  1647. L = wavelength_of_perturbations
  1648. k_0 = 2*pi/L # main frequency of waves
  1649. k_p = k_0*0.5
  1650. k_k = k_0/2*smoothness
  1651. A_0 = amplitude_of_perturbations
  1652. A_p = 0.3*A_0
  1653. A_k = k_0/2
  1654. x = linspace(xmin, xmax, 2001)
  1655. def w(x):
  1656. A = A_0 + A_p*sin(A_k*x)
  1657. k = k_0 + k_p*sin(k_k*x)
  1658. y = main_curve(x) + A*sin(k*x)
  1659. return y
  1660. self.shapes = {'wavy': Curve(x, w(x))}
  1661. # Use closure w to define __call__ - then we do not need
  1662. # to store all the parameters A_0, A_k, etc. as attributes
  1663. self.__call__ = w
  1664. class StochasticWavyCurve:
  1665. """
  1666. Precomputed stochastic wavy graphs.
  1667. There are three graphs with different look.
  1668. Curve 0:
  1669. ----------------------------------------------------------------------
  1670. |
  1671. |
  1672. *|
  1673. * |
  1674. * |
  1675. * |
  1676. * |
  1677. * |
  1678. * |
  1679. * |
  1680. * |
  1681. * |
  1682. * |
  1683. * |
  1684. |*
  1685. | *
  1686. | *
  1687. | *
  1688. | *
  1689. | *
  1690. | *
  1691. | *
  1692. | *
  1693. | *
  1694. | *
  1695. | *
  1696. | *
  1697. | *
  1698. | *
  1699. | *
  1700. | *
  1701. | *
  1702. | *
  1703. | *
  1704. | *
  1705. | *
  1706. | *
  1707. | *
  1708. | *
  1709. | *
  1710. | *
  1711. | *
  1712. | *
  1713. | *
  1714. | *
  1715. | *
  1716. | *
  1717. | *
  1718. | *
  1719. | *
  1720. | *
  1721. | *
  1722. | *
  1723. | *
  1724. | *
  1725. | *
  1726. | *
  1727. | *
  1728. | *
  1729. | *
  1730. | *
  1731. | *
  1732. | *
  1733. | *
  1734. | *
  1735. | *
  1736. | *
  1737. | *
  1738. | *
  1739. | *
  1740. | *
  1741. | *
  1742. |*
  1743. *|
  1744. * |
  1745. * |
  1746. * |
  1747. * |
  1748. * |
  1749. * |
  1750. * |
  1751. * |
  1752. * |
  1753. * |
  1754. * |
  1755. * |
  1756. * |
  1757. * |
  1758. * |
  1759. |*
  1760. | *
  1761. | *
  1762. | *
  1763. | *
  1764. | *
  1765. | *
  1766. | *
  1767. | *
  1768. | *
  1769. | *
  1770. | *
  1771. | *
  1772. | *
  1773. | *
  1774. | *
  1775. | *
  1776. | *
  1777. | *
  1778. | *
  1779. | *
  1780. | *
  1781. | *
  1782. | *
  1783. | *
  1784. | *
  1785. | *
  1786. | *
  1787. | *
  1788. | *
  1789. | *
  1790. | *
  1791. | *
  1792. | *
  1793. | *
  1794. | *
  1795. | *
  1796. | *
  1797. | *
  1798. | *
  1799. | *
  1800. | *
  1801. | *
  1802. | *
  1803. | *
  1804. |
  1805. * |
  1806. * |
  1807. * |
  1808. * |
  1809. * |
  1810. * |
  1811. * |
  1812. * |
  1813. * |
  1814. * |
  1815. * |
  1816. * |
  1817. * |
  1818. * |
  1819. * |
  1820. * |
  1821. * |
  1822. * |
  1823. * |
  1824. * |
  1825. * |
  1826. * |
  1827. * |
  1828. * |
  1829. * |
  1830. * |
  1831. * |
  1832. * |
  1833. * |
  1834. * |
  1835. Curve 2:
  1836. ----------------------------------------------------------------------
  1837. |
  1838. |
  1839. |
  1840. |*
  1841. |*
  1842. |*
  1843. |
  1844. |
  1845. *|
  1846. |*
  1847. | *
  1848. | *
  1849. | *
  1850. | *
  1851. | *
  1852. | *
  1853. | *
  1854. | *
  1855. | *
  1856. | *
  1857. | *
  1858. | *
  1859. | *
  1860. | *
  1861. | *
  1862. | *
  1863. | *
  1864. | *
  1865. | *
  1866. | *
  1867. | *
  1868. | *
  1869. | *
  1870. | *
  1871. | *
  1872. | *
  1873. | *
  1874. | *
  1875. | *
  1876. | *
  1877. | *
  1878. | *
  1879. | *
  1880. | *
  1881. | *
  1882. | *
  1883. | *
  1884. | *
  1885. | *
  1886. | *
  1887. | *
  1888. | *
  1889. | *
  1890. | *
  1891. | *
  1892. | *
  1893. | *
  1894. | *
  1895. | *
  1896. | *
  1897. | *
  1898. |
  1899. * |
  1900. * |
  1901. * |
  1902. * |
  1903. * |
  1904. * |
  1905. * |
  1906. * |
  1907. * |
  1908. * |
  1909. * |
  1910. * |
  1911. * |
  1912. * |
  1913. * |
  1914. * |
  1915. * |
  1916. * |
  1917. * |
  1918. * |
  1919. * |
  1920. * |
  1921. * |
  1922. * |
  1923. * |
  1924. * |
  1925. * |
  1926. * |
  1927. * |
  1928. |
  1929. | *
  1930. | *
  1931. | *
  1932. | *
  1933. | *
  1934. | *
  1935. | *
  1936. | *
  1937. | *
  1938. | *
  1939. | *
  1940. | *
  1941. | *
  1942. | *
  1943. | *
  1944. | *
  1945. | *
  1946. | *
  1947. | *
  1948. | *
  1949. | *
  1950. | *
  1951. |*
  1952. |*
  1953. |
  1954. |
  1955. |
  1956. |*
  1957. | *
  1958. | *
  1959. |*
  1960. |
  1961. *|
  1962. |*
  1963. | *
  1964. | *
  1965. | *
  1966. | *
  1967. | *
  1968. | *
  1969. | *
  1970. | *
  1971. | *
  1972. | *
  1973. | *
  1974. | *
  1975. | *
  1976. | *
  1977. | *
  1978. | *
  1979. | *
  1980. | *
  1981. | *
  1982. | *
  1983. | *
  1984. | *
  1985. | *
  1986. | *
  1987. | *
  1988. | *
  1989. | *
  1990. | *
  1991. | *
  1992. | *
  1993. | *
  1994. | *
  1995. | *
  1996. | *
  1997. | *
  1998. | *
  1999. | *
  2000. | *
  2001. | *
  2002. Curve 2:
  2003. ----------------------------------------------------------------------
  2004. |
  2005. |
  2006. |
  2007. |
  2008. |*
  2009. | *
  2010. | *
  2011. | *
  2012. | *
  2013. | *
  2014. | *
  2015. | *
  2016. | *
  2017. | *
  2018. | *
  2019. | *
  2020. | *
  2021. | *
  2022. | *
  2023. | *
  2024. | *
  2025. | *
  2026. | *
  2027. | *
  2028. | *
  2029. | *
  2030. | *
  2031. | *
  2032. |*
  2033. |
  2034. * |
  2035. * |
  2036. * |
  2037. * |
  2038. * |
  2039. * |
  2040. * |
  2041. * |
  2042. * |
  2043. * |
  2044. |*
  2045. | *
  2046. | *
  2047. | *
  2048. | *
  2049. | *
  2050. | *
  2051. | *
  2052. | *
  2053. | *
  2054. | *
  2055. | *
  2056. | *
  2057. | *
  2058. | *
  2059. | *
  2060. | *
  2061. | *
  2062. | *
  2063. | *
  2064. *|
  2065. * |
  2066. * |
  2067. * |
  2068. * |
  2069. * |
  2070. * |
  2071. * |
  2072. * |
  2073. * |
  2074. * |
  2075. * |
  2076. * |
  2077. * |
  2078. * |
  2079. * |
  2080. * |
  2081. |
  2082. | *
  2083. | *
  2084. | *
  2085. | *
  2086. | *
  2087. | *
  2088. | *
  2089. | *
  2090. | *
  2091. | *
  2092. | *
  2093. | *
  2094. | *
  2095. | *
  2096. | *
  2097. | *
  2098. | *
  2099. | *
  2100. | *
  2101. | *
  2102. | *
  2103. | *
  2104. |*
  2105. *|
  2106. * |
  2107. * |
  2108. * |
  2109. * |
  2110. * |
  2111. * |
  2112. * |
  2113. * |
  2114. * |
  2115. * |
  2116. * |
  2117. * |
  2118. * |
  2119. * |
  2120. * |
  2121. * |
  2122. * |
  2123. * |
  2124. * |
  2125. * |
  2126. * |
  2127. * |
  2128. * |
  2129. * |
  2130. * |
  2131. * |
  2132. * |
  2133. * |
  2134. * |
  2135. * |
  2136. * |
  2137. * |
  2138. * |
  2139. * |
  2140. * |
  2141. * |
  2142. * |
  2143. * |
  2144. * |
  2145. * |
  2146. * |
  2147. * |
  2148. * |
  2149. * |
  2150. * |
  2151. * |
  2152. * |
  2153. * |
  2154. * |
  2155. * |
  2156. * |
  2157. * |
  2158. * |
  2159. * |
  2160. * |
  2161. *|
  2162. |*
  2163. | *
  2164. | *
  2165. | *
  2166. | *
  2167. | *
  2168. | *
  2169. See also hplgit.github.io/pysketcher/doc/src/tut/fig-tut/StochasticWavyCurve.png (and .pdf)
  2170. """
  2171. # The curves were generated by the script generate_road_profiles.py and
  2172. # the code below were generated by plot_roads.py. Both scripts are
  2173. # found doc/src/src-bumpy in the repo git@github.com:hplgit/bumpy.git
  2174. def __init__(self, curve_no=0, percentage=100):
  2175. """
  2176. ============= ===================================================
  2177. Argument Explanation
  2178. ============= ===================================================
  2179. curve_no 0, 1, or 2: chooses one out of three shapes.
  2180. percentage The percentage of the defined curve to be used.
  2181. ============= ===================================================
  2182. """
  2183. self._define_curves()
  2184. self.curve_no = curve_no
  2185. m = int(len(self.x)/float(percentage)*100)
  2186. self.shapes = {'wavy': Curve(self.x[:m], self.y[curve_no][:m])}
  2187. def __call__(self, x):
  2188. raise NotImplementedError
  2189. def _define_curves(self):
  2190. self.x = array([0.0000, 0.0606, 0.1212, 0.1818, 0.2424, 0.3030, 0.3636, 0.4242, 0.4848, 0.5455, 0.6061, 0.6667, 0.7273, 0.7879, 0.8485, 0.9091, 0.9697, 1.0303, 1.0909, 1.1515, 1.2121, 1.2727, 1.3333, 1.3939, 1.4545, 1.5152, 1.5758, 1.6364, 1.6970, 1.7576, 1.8182, 1.8788, 1.9394, 2.0000, 2.0606, 2.1212, 2.1818, 2.2424, 2.3030, 2.3636, 2.4242, 2.4848, 2.5455, 2.6061, 2.6667, 2.7273, 2.7879, 2.8485, 2.9091, 2.9697, 3.0303, 3.0909, 3.1515, 3.2121, 3.2727, 3.3333, 3.3939, 3.4545, 3.5152, 3.5758, 3.6364, 3.6970, 3.7576, 3.8182, 3.8788, 3.9394, 4.0000, 4.0606, 4.1212, 4.1818, 4.2424, 4.3030, 4.3636, 4.4242, 4.4848, 4.5455, 4.6061, 4.6667, 4.7273, 4.7879, 4.8485, 4.9091, 4.9697, 5.0303, 5.0909, 5.1515, 5.2121, 5.2727, 5.3333, 5.3939, 5.4545, 5.5152, 5.5758, 5.6364, 5.6970, 5.7576, 5.8182, 5.8788, 5.9394, 6.0000, 6.0606, 6.1212, 6.1818, 6.2424, 6.3030, 6.3636, 6.4242, 6.4848, 6.5455, 6.6061, 6.6667, 6.7273, 6.7879, 6.8485, 6.9091, 6.9697, 7.0303, 7.0909, 7.1515, 7.2121, 7.2727, 7.3333, 7.3939, 7.4545, 7.5152, 7.5758, 7.6364, 7.6970, 7.7576, 7.8182, 7.8788, 7.9394, 8.0000, 8.0606, 8.1212, 8.1818, 8.2424, 8.3030, 8.3636, 8.4242, 8.4848, 8.5455, 8.6061, 8.6667, 8.7273, 8.7879, 8.8485, 8.9091, 8.9697, 9.0303, 9.0909, 9.1515, 9.2121, 9.2727, 9.3333, 9.3939, 9.4545, 9.5152, 9.5758, 9.6364, 9.6970, 9.7576, 9.8182, 9.8788, 9.9394, 10.0000, 10.0606, 10.1212, 10.1818, 10.2424, 10.3030, 10.3636, 10.4242, 10.4848, 10.5455, 10.6061, 10.6667, 10.7273, 10.7879, 10.8485, 10.9091, 10.9697, 11.0303, 11.0909, 11.1515, 11.2121, 11.2727, 11.3333, 11.3939, 11.4545, 11.5152, 11.5758, 11.6364, 11.6970, 11.7576, 11.8182, 11.8788, 11.9394, 12.0000, 12.0606, 12.1212, 12.1818, 12.2424, 12.3030, 12.3636, 12.4242, 12.4848, 12.5455, 12.6061, 12.6667, 12.7273, 12.7879, 12.8485, 12.9091, 12.9697, 13.0303, 13.0909, 13.1515, 13.2121, 13.2727, 13.3333, 13.3939, 13.4545, 13.5152, 13.5758, 13.6364, 13.6970, 13.7576, 13.8182, 13.8788, 13.9394, 14.0000, 14.0606, 14.1212, 14.1818, 14.2424, 14.3030, 14.3636, 14.4242, 14.4848, 14.5455, 14.6061, 14.6667, 14.7273, 14.7879, 14.8485, 14.9091, 14.9697, 15.0303, 15.0909, 15.1515, 15.2121, 15.2727, 15.3333, 15.3939, 15.4545, 15.5152, 15.5758, 15.6364, 15.6970, 15.7576, 15.8182, 15.8788, 15.9394, 16.0000, 16.0606, 16.1212, 16.1818, 16.2424, 16.3030, 16.3636, 16.4242, 16.4848, 16.5455, 16.6061, 16.6667, 16.7273, 16.7879, 16.8485, 16.9091, 16.9697, 17.0303, 17.0909, 17.1515, 17.2121, 17.2727, 17.3333, 17.3939, 17.4545, 17.5152, 17.5758, 17.6364, 17.6970, 17.7576, 17.8182, 17.8788, 17.9394, 18.0000, 18.0606, 18.1212, 18.1818, 18.2424, 18.3030, 18.3636, 18.4242, 18.4848, 18.5455, 18.6061, 18.6667, 18.7273, 18.7879, 18.8485, 18.9091, 18.9697, 19.0303, 19.0909, 19.1515, 19.2121, 19.2727, 19.3333, 19.3939, 19.4545, 19.5152, 19.5758, 19.6364, 19.6970, 19.7576, 19.8182, 19.8788, 19.9394, 20.0000, 20.0606, 20.1212, 20.1818, 20.2424, 20.3030, 20.3636, 20.4242, 20.4848, 20.5455, 20.6061, 20.6667, 20.7273, 20.7879, 20.8485, 20.9091, 20.9697, 21.0303, 21.0909, 21.1515, 21.2121, 21.2727, 21.3333, 21.3939, 21.4545, 21.5152, 21.5758, 21.6364, 21.6970, 21.7576, 21.8182, 21.8788, 21.9394, 22.0000, 22.0606, 22.1212, 22.1818, 22.2424, 22.3030, 22.3636, 22.4242, 22.4848, 22.5455, 22.6061, 22.6667, 22.7273, 22.7879, 22.8485, 22.9091, 22.9697, 23.0303, 23.0909, 23.1515, 23.2121, 23.2727, 23.3333, 23.3939, 23.4545, 23.5152, 23.5758, 23.6364, 23.6970, 23.7576, 23.8182, 23.8788, 23.9394, 24.0000, 24.0606, 24.1212, 24.1818, 24.2424, 24.3030, 24.3636, 24.4242, 24.4848, 24.5455, 24.6061, 24.6667, 24.7273, 24.7879, 24.8485, 24.9091, 24.9697, 25.0303, 25.0909, 25.1515, 25.2121, 25.2727, 25.3333, 25.3939, 25.4545, 25.5152, 25.5758, 25.6364, 25.6970, 25.7576, 25.8182, 25.8788, 25.9394, 26.0000, 26.0606, 26.1212, 26.1818, 26.2424, 26.3030, 26.3636, 26.4242, 26.4848, 26.5455, 26.6061, 26.6667, 26.7273, 26.7879, 26.8485, 26.9091, 26.9697, 27.0303, 27.0909, 27.1515, 27.2121, 27.2727, 27.3333, 27.3939, 27.4545, 27.5152, 27.5758, 27.6364, 27.6970, 27.7576, 27.8182, 27.8788, 27.9394, 28.0000, 28.0606, 28.1212, 28.1818, 28.2424, 28.3030, 28.3636, 28.4242, 28.4848, 28.5455, 28.6061, 28.6667, 28.7273, 28.7879, 28.8485, 28.9091, 28.9697, 29.0303, 29.0909, 29.1515, 29.2121, 29.2727, 29.3333, 29.3939, 29.4545, 29.5152, 29.5758, 29.6364, 29.6970, 29.7576, 29.8182, 29.8788, 29.9394, 30.0000, 30.0606, 30.1212, 30.1818, 30.2424, 30.3030, 30.3636, 30.4242, 30.4848, 30.5455, 30.6061, 30.6667, 30.7273, 30.7879, 30.8485, 30.9091, 30.9697, 31.0303, 31.0909, 31.1515, 31.2121, 31.2727, 31.3333, 31.3939, 31.4545, 31.5152, 31.5758, 31.6364, 31.6970, 31.7576, 31.8182, 31.8788, 31.9394, 32.0000, 32.0606, 32.1212, 32.1818, 32.2424, 32.3030, 32.3636, 32.4242, 32.4848, 32.5455, 32.6061, 32.6667, 32.7273, 32.7879, 32.8485, 32.9091, 32.9697, 33.0303, 33.0909, 33.1515, 33.2121, 33.2727, 33.3333, 33.3939, 33.4545, 33.5152, 33.5758, 33.6364, 33.6970, 33.7576, 33.8182, 33.8788, 33.9394, 34.0000, 34.0606, 34.1212, 34.1818, 34.2424, 34.3030, 34.3636, 34.4242, 34.4848, 34.5455, 34.6061, 34.6667, 34.7273, 34.7879, 34.8485, 34.9091, 34.9697, 35.0303, 35.0909, 35.1515, 35.2121, 35.2727, 35.3333, 35.3939, 35.4545, 35.5152, 35.5758, 35.6364, 35.6970, 35.7576, 35.8182, 35.8788, 35.9394, 36.0000, 36.0606, 36.1212, 36.1818, 36.2424, 36.3030, 36.3636, 36.4242, 36.4848, 36.5455, 36.6061, 36.6667, 36.7273, 36.7879, 36.8485, 36.9091, 36.9697, 37.0303, 37.0909, 37.1515, 37.2121, 37.2727, 37.3333, 37.3939, 37.4545, 37.5152, 37.5758, 37.6364, 37.6970, 37.7576, 37.8182, 37.8788, 37.9394, 38.0000, 38.0606, 38.1212, 38.1818, 38.2424, 38.3030, 38.3636, 38.4242, 38.4848, 38.5455, 38.6061, 38.6667, 38.7273, 38.7879, 38.8485, 38.9091, 38.9697, 39.0303, 39.0909, 39.1515, 39.2121, 39.2727, 39.3333, 39.3939, 39.4545, 39.5152, 39.5758, 39.6364, 39.6970, 39.7576, 39.8182, 39.8788, 39.9394, 40.0000, 40.0606, 40.1212, 40.1818, 40.2424, 40.3030, 40.3636, 40.4242, 40.4848, 40.5455, 40.6061, 40.6667, 40.7273, 40.7879, 40.8485, 40.9091, 40.9697, 41.0303, 41.0909, 41.1515, 41.2121, 41.2727, 41.3333, 41.3939, 41.4545, 41.5152, 41.5758, 41.6364, 41.6970, 41.7576, 41.8182, 41.8788, 41.9394, 42.0000, 42.0606, 42.1212, 42.1818, 42.2424, 42.3030, 42.3636, 42.4242, 42.4848, 42.5455, 42.6061, 42.6667, 42.7273, 42.7879, 42.8485, 42.9091, 42.9697, 43.0303, 43.0909, 43.1515, 43.2121, 43.2727, 43.3333, 43.3939, 43.4545, 43.5152, 43.5758, 43.6364, 43.6970, 43.7576, 43.8182, 43.8788, 43.9394, 44.0000, 44.0606, 44.1212, 44.1818, 44.2424, 44.3030, 44.3636, 44.4242, 44.4848, 44.5455, 44.6061, 44.6667, 44.7273, 44.7879, 44.8485, 44.9091, 44.9697, 45.0303, 45.0909, 45.1515, 45.2121, 45.2727, 45.3333, 45.3939, 45.4545, 45.5152, 45.5758, 45.6364, 45.6970, 45.7576, 45.8182, 45.8788, 45.9394, 46.0000, 46.0606, 46.1212, 46.1818, 46.2424, 46.3030, 46.3636, 46.4242, 46.4848, 46.5455, 46.6061, 46.6667, 46.7273, 46.7879, 46.8485, 46.9091, 46.9697, 47.0303, 47.0909, 47.1515, 47.2121, 47.2727, 47.3333, 47.3939, 47.4545, 47.5152, 47.5758, 47.6364, 47.6970, 47.7576, 47.8182, 47.8788, 47.9394, 48.0000, 48.0606, 48.1212, 48.1818, 48.2424, 48.3030, 48.3636, 48.4242, 48.4848, 48.5455, 48.6061, 48.6667, 48.7273, 48.7879, 48.8485, 48.9091, 48.9697, 49.0303, 49.0909, 49.1515, 49.2121, 49.2727, 49.3333, 49.3939, 49.4545, 49.5152, 49.5758, 49.6364, 49.6970, 49.7576, 49.8182, 49.8788, 49.9394, ])
  2191. self.y = [None]*3
  2192. self.y[0] = array([0.0000, 0.0005, 0.0006, 0.0004, -0.0004, -0.0007, -0.0022, -0.0027, -0.0036, -0.0042, -0.0050, -0.0049, -0.0060, -0.0072, -0.0085, -0.0092, -0.0104, -0.0116, -0.0133, -0.0148, -0.0160, -0.0177, -0.0186, -0.0191, -0.0192, -0.0187, -0.0187, -0.0187, -0.0192, -0.0198, -0.0201, -0.0208, -0.0216, -0.0227, -0.0242, -0.0260, -0.0277, -0.0299, -0.0319, -0.0328, -0.0333, -0.0338, -0.0347, -0.0360, -0.0363, -0.0365, -0.0370, -0.0373, -0.0364, -0.0355, -0.0343, -0.0329, -0.0317, -0.0312, -0.0309, -0.0306, -0.0301, -0.0290, -0.0275, -0.0259, -0.0238, -0.0222, -0.0200, -0.0176, -0.0154, -0.0130, -0.0108, -0.0081, -0.0046, -0.0001, 0.0035, 0.0061, 0.0083, 0.0105, 0.0130, 0.0156, 0.0170, 0.0181, 0.0196, 0.0212, 0.0231, 0.0247, 0.0262, 0.0277, 0.0293, 0.0309, 0.0325, 0.0336, 0.0348, 0.0360, 0.0378, 0.0401, 0.0423, 0.0443, 0.0457, 0.0473, 0.0488, 0.0500, 0.0511, 0.0518, 0.0528, 0.0534, 0.0547, 0.0561, 0.0577, 0.0585, 0.0594, 0.0606, 0.0611, 0.0614, 0.0617, 0.0612, 0.0607, 0.0608, 0.0603, 0.0599, 0.0588, 0.0577, 0.0557, 0.0543, 0.0532, 0.0520, 0.0505, 0.0496, 0.0499, 0.0490, 0.0489, 0.0496, 0.0504, 0.0504, 0.0509, 0.0512, 0.0512, 0.0504, 0.0499, 0.0498, 0.0493, 0.0491, 0.0483, 0.0478, 0.0474, 0.0468, 0.0462, 0.0460, 0.0462, 0.0467, 0.0472, 0.0476, 0.0483, 0.0491, 0.0502, 0.0510, 0.0504, 0.0503, 0.0514, 0.0527, 0.0538, 0.0547, 0.0554, 0.0561, 0.0561, 0.0558, 0.0548, 0.0540, 0.0531, 0.0524, 0.0516, 0.0513, 0.0511, 0.0520, 0.0519, 0.0513, 0.0512, 0.0525, 0.0535, 0.0545, 0.0552, 0.0566, 0.0577, 0.0591, 0.0602, 0.0605, 0.0609, 0.0615, 0.0627, 0.0638, 0.0644, 0.0652, 0.0661, 0.0670, 0.0678, 0.0692, 0.0706, 0.0729, 0.0757, 0.0786, 0.0805, 0.0825, 0.0846, 0.0870, 0.0897, 0.0921, 0.0947, 0.0968, 0.0997, 0.1018, 0.1027, 0.1025, 0.1018, 0.1004, 0.1000, 0.0994, 0.0980, 0.0972, 0.0960, 0.0941, 0.0927, 0.0916, 0.0902, 0.0896, 0.0890, 0.0892, 0.0896, 0.0908, 0.0919, 0.0922, 0.0937, 0.0948, 0.0957, 0.0960, 0.0961, 0.0963, 0.0965, 0.0970, 0.0983, 0.0994, 0.0997, 0.0993, 0.0984, 0.0965, 0.0951, 0.0934, 0.0916, 0.0897, 0.0870, 0.0840, 0.0813, 0.0791, 0.0766, 0.0751, 0.0730, 0.0707, 0.0683, 0.0644, 0.0616, 0.0592, 0.0562, 0.0545, 0.0531, 0.0519, 0.0504, 0.0490, 0.0468, 0.0451, 0.0432, 0.0414, 0.0403, 0.0394, 0.0386, 0.0380, 0.0370, 0.0364, 0.0367, 0.0374, 0.0385, 0.0390, 0.0390, 0.0381, 0.0380, 0.0377, 0.0381, 0.0380, 0.0377, 0.0374, 0.0376, 0.0378, 0.0380, 0.0382, 0.0385, 0.0381, 0.0377, 0.0373, 0.0367, 0.0365, 0.0358, 0.0351, 0.0342, 0.0336, 0.0334, 0.0326, 0.0322, 0.0329, 0.0327, 0.0321, 0.0310, 0.0297, 0.0293, 0.0290, 0.0283, 0.0279, 0.0272, 0.0271, 0.0271, 0.0279, 0.0282, 0.0302, 0.0325, 0.0351, 0.0375, 0.0393, 0.0406, 0.0416, 0.0422, 0.0428, 0.0430, 0.0434, 0.0443, 0.0447, 0.0457, 0.0465, 0.0479, 0.0494, 0.0514, 0.0527, 0.0539, 0.0557, 0.0571, 0.0572, 0.0563, 0.0539, 0.0504, 0.0469, 0.0441, 0.0412, 0.0385, 0.0359, 0.0334, 0.0308, 0.0282, 0.0260, 0.0232, 0.0211, 0.0196, 0.0180, 0.0169, 0.0154, 0.0137, 0.0121, 0.0105, 0.0088, 0.0067, 0.0044, 0.0027, -0.0000, -0.0024, -0.0048, -0.0066, -0.0082, -0.0111, -0.0136, -0.0158, -0.0179, -0.0201, -0.0218, -0.0235, -0.0242, -0.0245, -0.0236, -0.0231, -0.0237, -0.0237, -0.0233, -0.0229, -0.0233, -0.0239, -0.0241, -0.0244, -0.0247, -0.0251, -0.0259, -0.0270, -0.0288, -0.0295, -0.0305, -0.0311, -0.0322, -0.0327, -0.0343, -0.0352, -0.0361, -0.0358, -0.0362, -0.0365, -0.0365, -0.0358, -0.0353, -0.0348, -0.0355, -0.0365, -0.0373, -0.0373, -0.0375, -0.0365, -0.0345, -0.0327, -0.0322, -0.0327, -0.0335, -0.0337, -0.0337, -0.0348, -0.0359, -0.0361, -0.0364, -0.0371, -0.0366, -0.0361, -0.0358, -0.0353, -0.0348, -0.0345, -0.0335, -0.0320, -0.0300, -0.0281, -0.0257, -0.0233, -0.0208, -0.0179, -0.0146, -0.0104, -0.0062, -0.0031, -0.0007, 0.0023, 0.0049, 0.0077, 0.0099, 0.0125, 0.0147, 0.0177, 0.0202, 0.0232, 0.0264, 0.0291, 0.0322, 0.0346, 0.0365, 0.0386, 0.0403, 0.0415, 0.0425, 0.0428, 0.0436, 0.0447, 0.0457, 0.0465, 0.0475, 0.0494, 0.0516, 0.0534, 0.0555, 0.0574, 0.0591, 0.0616, 0.0638, 0.0655, 0.0660, 0.0657, 0.0656, 0.0650, 0.0647, 0.0637, 0.0623, 0.0613, 0.0611, 0.0611, 0.0618, 0.0633, 0.0652, 0.0665, 0.0677, 0.0690, 0.0700, 0.0712, 0.0713, 0.0710, 0.0709, 0.0695, 0.0675, 0.0655, 0.0626, 0.0598, 0.0566, 0.0533, 0.0501, 0.0470, 0.0437, 0.0405, 0.0372, 0.0342, 0.0323, 0.0305, 0.0289, 0.0267, 0.0250, 0.0229, 0.0204, 0.0183, 0.0164, 0.0152, 0.0148, 0.0141, 0.0135, 0.0139, 0.0148, 0.0160, 0.0179, 0.0193, 0.0210, 0.0234, 0.0266, 0.0291, 0.0314, 0.0337, 0.0358, 0.0375, 0.0395, 0.0412, 0.0417, 0.0424, 0.0428, 0.0428, 0.0418, 0.0415, 0.0398, 0.0373, 0.0347, 0.0328, 0.0318, 0.0301, 0.0284, 0.0258, 0.0240, 0.0214, 0.0188, 0.0168, 0.0148, 0.0137, 0.0122, 0.0109, 0.0101, 0.0093, 0.0093, 0.0096, 0.0089, 0.0104, 0.0123, 0.0141, 0.0150, 0.0159, 0.0165, 0.0174, 0.0185, 0.0205, 0.0225, 0.0247, 0.0269, 0.0290, 0.0308, 0.0331, 0.0357, 0.0379, 0.0392, 0.0405, 0.0423, 0.0440, 0.0465, 0.0484, 0.0503, 0.0506, 0.0506, 0.0504, 0.0504, 0.0513, 0.0521, 0.0537, 0.0555, 0.0577, 0.0595, 0.0619, 0.0636, 0.0654, 0.0666, 0.0674, 0.0672, 0.0667, 0.0664, 0.0660, 0.0648, 0.0645, 0.0642, 0.0645, 0.0651, 0.0653, 0.0643, 0.0629, 0.0621, 0.0607, 0.0597, 0.0585, 0.0574, 0.0553, 0.0539, 0.0525, 0.0516, 0.0508, 0.0505, 0.0500, 0.0500, 0.0490, 0.0475, 0.0464, 0.0452, 0.0440, 0.0427, 0.0416, 0.0403, 0.0393, 0.0377, 0.0360, 0.0349, 0.0343, 0.0332, 0.0325, 0.0313, 0.0305, 0.0287, 0.0263, 0.0237, 0.0211, 0.0194, 0.0182, 0.0180, 0.0184, 0.0186, 0.0186, 0.0192, 0.0198, 0.0203, 0.0200, 0.0196, 0.0177, 0.0156, 0.0130, 0.0110, 0.0087, 0.0064, 0.0049, 0.0029, 0.0011, -0.0016, -0.0042, -0.0064, -0.0080, -0.0100, -0.0118, -0.0133, -0.0147, -0.0160, -0.0170, -0.0176, -0.0188, -0.0206, -0.0209, -0.0201, -0.0200, -0.0198, -0.0191, -0.0181, -0.0170, -0.0165, -0.0161, -0.0159, -0.0157, -0.0156, -0.0156, -0.0150, -0.0137, -0.0131, -0.0128, -0.0129, -0.0124, -0.0119, -0.0106, -0.0096, -0.0086, -0.0084, -0.0075, -0.0069, -0.0070, -0.0075, -0.0092, -0.0109, -0.0124, -0.0137, -0.0140, -0.0146, -0.0146, -0.0144, -0.0144, -0.0155, -0.0160, -0.0167, -0.0174, -0.0187, -0.0198, -0.0208, -0.0217, -0.0226, -0.0229, -0.0226, -0.0219, -0.0194, -0.0173, -0.0155, -0.0143, -0.0129, -0.0120, -0.0126, -0.0137, -0.0148, -0.0153, -0.0166, -0.0182, -0.0197, -0.0216, -0.0230, -0.0244, -0.0255, -0.0270, -0.0275, -0.0280, -0.0277, -0.0276, -0.0272, -0.0272, -0.0276, -0.0284, -0.0292, -0.0300, -0.0299, -0.0305, -0.0315, -0.0316, -0.0313, -0.0320, -0.0324, -0.0326, -0.0339, -0.0354, -0.0368, -0.0376, -0.0379, -0.0381, -0.0377, -0.0373, -0.0374, -0.0380, -0.0382, -0.0394, -0.0402, -0.0415, -0.0429, -0.0439, -0.0442, -0.0455, -0.0472, -0.0487, -0.0504, -0.0515, -0.0537, -0.0555, -0.0570, -0.0583, -0.0589, -0.0601, -0.0606, -0.0614, -0.0617, -0.0621, -0.0620, -0.0614, -0.0616, -0.0618, -0.0620, -0.0622, -0.0628, -0.0623, -0.0617, -0.0602, -0.0590, -0.0584, -0.0577, -0.0572, -0.0555, -0.0536, -0.0511, -0.0488, -0.0468, -0.0448, -0.0427, -0.0401, -0.0378, -0.0358, ])
  2193. self.y[1] = array([0.0000, 0.0002, 0.0002, 0.0001, 0.0001, 0.0003, 0.0008, 0.0009, 0.0009, 0.0015, 0.0019, 0.0027, 0.0033, 0.0037, 0.0041, 0.0052, 0.0055, 0.0050, 0.0048, 0.0054, 0.0054, 0.0059, 0.0061, 0.0060, 0.0054, 0.0050, 0.0047, 0.0042, 0.0033, 0.0031, 0.0027, 0.0021, 0.0015, 0.0008, -0.0002, -0.0011, -0.0015, -0.0015, -0.0021, -0.0025, -0.0027, -0.0013, 0.0005, 0.0030, 0.0049, 0.0074, 0.0099, 0.0119, 0.0142, 0.0166, 0.0186, 0.0205, 0.0229, 0.0247, 0.0266, 0.0286, 0.0307, 0.0327, 0.0346, 0.0368, 0.0379, 0.0393, 0.0409, 0.0434, 0.0457, 0.0478, 0.0499, 0.0520, 0.0534, 0.0544, 0.0561, 0.0576, 0.0587, 0.0593, 0.0597, 0.0599, 0.0594, 0.0588, 0.0588, 0.0595, 0.0609, 0.0628, 0.0651, 0.0673, 0.0693, 0.0721, 0.0760, 0.0798, 0.0837, 0.0881, 0.0931, 0.0977, 0.1023, 0.1076, 0.1127, 0.1175, 0.1223, 0.1264, 0.1302, 0.1333, 0.1373, 0.1408, 0.1441, 0.1472, 0.1497, 0.1516, 0.1523, 0.1527, 0.1532, 0.1537, 0.1542, 0.1549, 0.1551, 0.1543, 0.1536, 0.1532, 0.1524, 0.1512, 0.1497, 0.1484, 0.1473, 0.1460, 0.1445, 0.1429, 0.1411, 0.1398, 0.1395, 0.1395, 0.1388, 0.1386, 0.1380, 0.1369, 0.1355, 0.1341, 0.1319, 0.1293, 0.1266, 0.1240, 0.1215, 0.1189, 0.1160, 0.1135, 0.1111, 0.1079, 0.1049, 0.1016, 0.0995, 0.0972, 0.0948, 0.0927, 0.0914, 0.0904, 0.0892, 0.0878, 0.0862, 0.0848, 0.0829, 0.0809, 0.0792, 0.0776, 0.0760, 0.0754, 0.0740, 0.0724, 0.0700, 0.0680, 0.0668, 0.0658, 0.0655, 0.0653, 0.0648, 0.0636, 0.0613, 0.0589, 0.0573, 0.0559, 0.0552, 0.0558, 0.0571, 0.0587, 0.0602, 0.0608, 0.0613, 0.0607, 0.0601, 0.0595, 0.0595, 0.0590, 0.0591, 0.0598, 0.0603, 0.0603, 0.0610, 0.0610, 0.0607, 0.0603, 0.0601, 0.0607, 0.0608, 0.0616, 0.0627, 0.0640, 0.0653, 0.0662, 0.0669, 0.0673, 0.0667, 0.0654, 0.0646, 0.0635, 0.0624, 0.0604, 0.0583, 0.0562, 0.0547, 0.0537, 0.0524, 0.0522, 0.0516, 0.0515, 0.0518, 0.0515, 0.0504, 0.0501, 0.0501, 0.0501, 0.0498, 0.0494, 0.0489, 0.0485, 0.0483, 0.0484, 0.0477, 0.0472, 0.0468, 0.0471, 0.0463, 0.0461, 0.0460, 0.0466, 0.0461, 0.0454, 0.0448, 0.0445, 0.0439, 0.0437, 0.0439, 0.0438, 0.0430, 0.0423, 0.0418, 0.0415, 0.0409, 0.0400, 0.0386, 0.0376, 0.0370, 0.0357, 0.0351, 0.0354, 0.0356, 0.0356, 0.0362, 0.0366, 0.0380, 0.0385, 0.0378, 0.0381, 0.0390, 0.0390, 0.0388, 0.0386, 0.0379, 0.0366, 0.0362, 0.0364, 0.0365, 0.0363, 0.0363, 0.0359, 0.0354, 0.0348, 0.0354, 0.0350, 0.0337, 0.0314, 0.0282, 0.0258, 0.0240, 0.0230, 0.0227, 0.0227, 0.0221, 0.0214, 0.0201, 0.0188, 0.0179, 0.0173, 0.0165, 0.0155, 0.0133, 0.0109, 0.0082, 0.0052, 0.0028, 0.0003, -0.0026, -0.0062, -0.0095, -0.0120, -0.0151, -0.0180, -0.0209, -0.0233, -0.0262, -0.0293, -0.0316, -0.0337, -0.0361, -0.0385, -0.0419, -0.0445, -0.0470, -0.0494, -0.0515, -0.0534, -0.0561, -0.0590, -0.0619, -0.0643, -0.0666, -0.0688, -0.0710, -0.0733, -0.0753, -0.0774, -0.0795, -0.0822, -0.0837, -0.0851, -0.0865, -0.0876, -0.0892, -0.0892, -0.0893, -0.0896, -0.0901, -0.0905, -0.0909, -0.0909, -0.0901, -0.0890, -0.0884, -0.0880, -0.0873, -0.0871, -0.0872, -0.0860, -0.0858, -0.0863, -0.0865, -0.0864, -0.0865, -0.0871, -0.0887, -0.0904, -0.0924, -0.0941, -0.0962, -0.0980, -0.0999, -0.1014, -0.1021, -0.1029, -0.1037, -0.1041, -0.1052, -0.1056, -0.1065, -0.1070, -0.1075, -0.1073, -0.1079, -0.1080, -0.1078, -0.1075, -0.1074, -0.1078, -0.1082, -0.1087, -0.1083, -0.1079, -0.1087, -0.1096, -0.1101, -0.1105, -0.1114, -0.1121, -0.1127, -0.1135, -0.1137, -0.1133, -0.1131, -0.1126, -0.1115, -0.1109, -0.1099, -0.1097, -0.1092, -0.1081, -0.1066, -0.1057, -0.1050, -0.1047, -0.1040, -0.1030, -0.1018, -0.1004, -0.0988, -0.0972, -0.0955, -0.0946, -0.0938, -0.0925, -0.0905, -0.0886, -0.0864, -0.0838, -0.0805, -0.0776, -0.0748, -0.0720, -0.0686, -0.0655, -0.0621, -0.0588, -0.0555, -0.0520, -0.0477, -0.0434, -0.0389, -0.0353, -0.0323, -0.0287, -0.0247, -0.0211, -0.0179, -0.0149, -0.0123, -0.0094, -0.0065, -0.0042, -0.0020, -0.0001, 0.0016, 0.0028, 0.0044, 0.0072, 0.0097, 0.0109, 0.0120, 0.0127, 0.0135, 0.0140, 0.0151, 0.0147, 0.0158, 0.0161, 0.0162, 0.0154, 0.0154, 0.0162, 0.0172, 0.0186, 0.0204, 0.0214, 0.0213, 0.0210, 0.0211, 0.0208, 0.0204, 0.0202, 0.0201, 0.0187, 0.0179, 0.0167, 0.0164, 0.0167, 0.0170, 0.0180, 0.0188, 0.0199, 0.0204, 0.0213, 0.0209, 0.0203, 0.0198, 0.0186, 0.0183, 0.0181, 0.0168, 0.0163, 0.0161, 0.0160, 0.0154, 0.0157, 0.0162, 0.0166, 0.0171, 0.0171, 0.0179, 0.0186, 0.0196, 0.0205, 0.0229, 0.0254, 0.0280, 0.0305, 0.0327, 0.0343, 0.0354, 0.0361, 0.0367, 0.0363, 0.0361, 0.0355, 0.0353, 0.0351, 0.0342, 0.0335, 0.0328, 0.0326, 0.0324, 0.0325, 0.0330, 0.0333, 0.0333, 0.0327, 0.0326, 0.0328, 0.0326, 0.0327, 0.0329, 0.0328, 0.0332, 0.0331, 0.0325, 0.0315, 0.0311, 0.0305, 0.0302, 0.0300, 0.0289, 0.0275, 0.0260, 0.0246, 0.0234, 0.0230, 0.0216, 0.0211, 0.0205, 0.0191, 0.0172, 0.0147, 0.0131, 0.0112, 0.0094, 0.0082, 0.0062, 0.0045, 0.0041, 0.0035, 0.0037, 0.0035, 0.0033, 0.0033, 0.0037, 0.0034, 0.0036, 0.0033, 0.0028, 0.0021, 0.0015, 0.0008, 0.0004, 0.0002, -0.0002, -0.0008, -0.0001, 0.0007, 0.0015, 0.0020, 0.0020, 0.0028, 0.0037, 0.0045, 0.0052, 0.0060, 0.0063, 0.0074, 0.0083, 0.0092, 0.0095, 0.0095, 0.0096, 0.0093, 0.0100, 0.0097, 0.0093, 0.0084, 0.0071, 0.0050, 0.0044, 0.0031, 0.0017, 0.0005, -0.0004, -0.0018, -0.0026, -0.0026, -0.0020, -0.0018, -0.0010, 0.0003, 0.0024, 0.0046, 0.0066, 0.0090, 0.0110, 0.0126, 0.0143, 0.0157, 0.0167, 0.0171, 0.0179, 0.0195, 0.0223, 0.0254, 0.0287, 0.0320, 0.0359, 0.0400, 0.0439, 0.0470, 0.0491, 0.0515, 0.0535, 0.0563, 0.0585, 0.0605, 0.0620, 0.0635, 0.0654, 0.0670, 0.0687, 0.0701, 0.0728, 0.0746, 0.0766, 0.0785, 0.0800, 0.0812, 0.0817, 0.0822, 0.0820, 0.0814, 0.0814, 0.0817, 0.0820, 0.0823, 0.0826, 0.0826, 0.0831, 0.0833, 0.0846, 0.0851, 0.0853, 0.0855, 0.0858, 0.0859, 0.0864, 0.0868, 0.0876, 0.0884, 0.0890, 0.0894, 0.0895, 0.0888, 0.0882, 0.0883, 0.0884, 0.0892, 0.0903, 0.0915, 0.0917, 0.0916, 0.0908, 0.0900, 0.0891, 0.0881, 0.0869, 0.0849, 0.0825, 0.0805, 0.0789, 0.0773, 0.0761, 0.0750, 0.0738, 0.0729, 0.0717, 0.0712, 0.0714, 0.0718, 0.0727, 0.0737, 0.0735, 0.0733, 0.0723, 0.0706, 0.0683, 0.0666, 0.0651, 0.0636, 0.0620, 0.0599, 0.0580, 0.0563, 0.0551, 0.0540, 0.0526, 0.0516, 0.0503, 0.0492, 0.0482, 0.0469, 0.0468, 0.0471, 0.0471, 0.0471, 0.0483, 0.0490, 0.0486, 0.0489, 0.0495, 0.0510, 0.0523, 0.0538, 0.0547, 0.0552, 0.0556, 0.0569, 0.0573, 0.0578, 0.0583, 0.0579, 0.0579, 0.0580, 0.0583, 0.0585, 0.0594, 0.0601, 0.0598, 0.0594, 0.0597, 0.0602, 0.0611, 0.0612, 0.0618, 0.0620, 0.0626, 0.0635, 0.0637, 0.0645, 0.0649, 0.0657, 0.0667, 0.0680, 0.0688, 0.0693, 0.0698, 0.0702, 0.0699, 0.0703, 0.0702, 0.0705, 0.0716, 0.0725, 0.0730, 0.0736, 0.0736, 0.0734, 0.0735, 0.0739, 0.0736, 0.0728, 0.0720, 0.0717, 0.0715, 0.0709, 0.0704, 0.0707, 0.0704, 0.0695, 0.0690, 0.0687, 0.0667, 0.0648, 0.0629, 0.0620, 0.0620, 0.0618, 0.0621, 0.0623, 0.0633, 0.0637, 0.0637, 0.0638, 0.0639, 0.0639, 0.0642, 0.0650, 0.0653, 0.0657, 0.0661, ])
  2194. self.y[2] = array([0.0000, 0.0001, -0.0002, -0.0003, 0.0004, 0.0014, 0.0021, 0.0025, 0.0025, 0.0021, 0.0018, 0.0022, 0.0016, 0.0018, 0.0018, 0.0021, 0.0027, 0.0034, 0.0046, 0.0060, 0.0076, 0.0080, 0.0084, 0.0090, 0.0100, 0.0104, 0.0098, 0.0097, 0.0100, 0.0100, 0.0105, 0.0117, 0.0124, 0.0128, 0.0133, 0.0133, 0.0133, 0.0132, 0.0132, 0.0136, 0.0144, 0.0161, 0.0179, 0.0196, 0.0222, 0.0251, 0.0265, 0.0279, 0.0287, 0.0291, 0.0297, 0.0305, 0.0316, 0.0328, 0.0340, 0.0361, 0.0382, 0.0408, 0.0425, 0.0442, 0.0460, 0.0474, 0.0489, 0.0502, 0.0512, 0.0517, 0.0526, 0.0525, 0.0525, 0.0521, 0.0508, 0.0498, 0.0487, 0.0478, 0.0472, 0.0461, 0.0446, 0.0434, 0.0415, 0.0399, 0.0388, 0.0374, 0.0360, 0.0351, 0.0339, 0.0320, 0.0308, 0.0294, 0.0286, 0.0278, 0.0255, 0.0224, 0.0194, 0.0170, 0.0147, 0.0131, 0.0123, 0.0121, 0.0110, 0.0106, 0.0100, 0.0090, 0.0089, 0.0093, 0.0100, 0.0111, 0.0132, 0.0159, 0.0179, 0.0195, 0.0207, 0.0220, 0.0229, 0.0242, 0.0261, 0.0279, 0.0290, 0.0300, 0.0303, 0.0309, 0.0316, 0.0328, 0.0335, 0.0332, 0.0323, 0.0314, 0.0300, 0.0283, 0.0268, 0.0248, 0.0225, 0.0201, 0.0172, 0.0143, 0.0119, 0.0104, 0.0088, 0.0071, 0.0059, 0.0051, 0.0038, 0.0027, 0.0017, 0.0009, 0.0007, -0.0002, -0.0008, -0.0020, -0.0034, -0.0055, -0.0069, -0.0082, -0.0086, -0.0085, -0.0089, -0.0088, -0.0092, -0.0099, -0.0110, -0.0121, -0.0137, -0.0150, -0.0157, -0.0160, -0.0155, -0.0149, -0.0140, -0.0139, -0.0134, -0.0135, -0.0136, -0.0138, -0.0145, -0.0158, -0.0155, -0.0155, -0.0153, -0.0154, -0.0159, -0.0157, -0.0152, -0.0143, -0.0140, -0.0138, -0.0142, -0.0148, -0.0154, -0.0154, -0.0147, -0.0136, -0.0122, -0.0107, -0.0102, -0.0091, -0.0082, -0.0063, -0.0042, -0.0028, -0.0007, 0.0015, 0.0038, 0.0054, 0.0075, 0.0093, 0.0110, 0.0137, 0.0156, 0.0170, 0.0184, 0.0195, 0.0199, 0.0206, 0.0209, 0.0207, 0.0201, 0.0198, 0.0196, 0.0191, 0.0192, 0.0193, 0.0196, 0.0201, 0.0206, 0.0216, 0.0229, 0.0253, 0.0278, 0.0304, 0.0321, 0.0341, 0.0356, 0.0367, 0.0372, 0.0379, 0.0383, 0.0394, 0.0396, 0.0397, 0.0391, 0.0382, 0.0362, 0.0346, 0.0334, 0.0325, 0.0325, 0.0325, 0.0323, 0.0316, 0.0321, 0.0328, 0.0338, 0.0352, 0.0366, 0.0378, 0.0389, 0.0401, 0.0403, 0.0406, 0.0419, 0.0425, 0.0424, 0.0421, 0.0415, 0.0407, 0.0406, 0.0411, 0.0413, 0.0419, 0.0420, 0.0416, 0.0415, 0.0402, 0.0388, 0.0377, 0.0368, 0.0357, 0.0353, 0.0351, 0.0348, 0.0347, 0.0340, 0.0326, 0.0314, 0.0306, 0.0295, 0.0288, 0.0275, 0.0257, 0.0234, 0.0209, 0.0188, 0.0169, 0.0151, 0.0132, 0.0114, 0.0093, 0.0076, 0.0055, 0.0026, -0.0008, -0.0038, -0.0069, -0.0097, -0.0118, -0.0132, -0.0148, -0.0164, -0.0183, -0.0202, -0.0213, -0.0228, -0.0253, -0.0280, -0.0299, -0.0315, -0.0329, -0.0343, -0.0349, -0.0364, -0.0373, -0.0383, -0.0391, -0.0400, -0.0401, -0.0404, -0.0414, -0.0422, -0.0425, -0.0425, -0.0420, -0.0419, -0.0412, -0.0412, -0.0408, -0.0400, -0.0396, -0.0397, -0.0400, -0.0401, -0.0405, -0.0412, -0.0410, -0.0401, -0.0389, -0.0377, -0.0372, -0.0369, -0.0364, -0.0356, -0.0347, -0.0345, -0.0346, -0.0353, -0.0359, -0.0367, -0.0376, -0.0385, -0.0387, -0.0391, -0.0401, -0.0400, -0.0398, -0.0395, -0.0391, -0.0385, -0.0383, -0.0377, -0.0374, -0.0371, -0.0361, -0.0357, -0.0355, -0.0342, -0.0324, -0.0303, -0.0280, -0.0257, -0.0232, -0.0205, -0.0179, -0.0147, -0.0113, -0.0091, -0.0059, -0.0019, 0.0024, 0.0071, 0.0120, 0.0177, 0.0219, 0.0248, 0.0273, 0.0290, 0.0302, 0.0318, 0.0325, 0.0331, 0.0334, 0.0344, 0.0360, 0.0387, 0.0414, 0.0437, 0.0456, 0.0471, 0.0491, 0.0511, 0.0526, 0.0536, 0.0550, 0.0570, 0.0597, 0.0622, 0.0647, 0.0672, 0.0696, 0.0710, 0.0725, 0.0748, 0.0777, 0.0806, 0.0832, 0.0854, 0.0874, 0.0893, 0.0906, 0.0914, 0.0927, 0.0937, 0.0944, 0.0959, 0.0970, 0.0970, 0.0967, 0.0955, 0.0945, 0.0934, 0.0927, 0.0921, 0.0916, 0.0909, 0.0901, 0.0893, 0.0884, 0.0881, 0.0877, 0.0870, 0.0870, 0.0871, 0.0872, 0.0867, 0.0860, 0.0845, 0.0821, 0.0800, 0.0775, 0.0752, 0.0728, 0.0703, 0.0676, 0.0655, 0.0637, 0.0618, 0.0593, 0.0558, 0.0518, 0.0483, 0.0445, 0.0414, 0.0384, 0.0365, 0.0349, 0.0338, 0.0330, 0.0323, 0.0320, 0.0319, 0.0320, 0.0323, 0.0324, 0.0324, 0.0322, 0.0327, 0.0328, 0.0320, 0.0314, 0.0295, 0.0279, 0.0258, 0.0243, 0.0229, 0.0211, 0.0195, 0.0176, 0.0163, 0.0151, 0.0136, 0.0123, 0.0108, 0.0088, 0.0064, 0.0041, 0.0021, 0.0002, -0.0020, -0.0051, -0.0078, -0.0106, -0.0137, -0.0169, -0.0195, -0.0218, -0.0235, -0.0253, -0.0269, -0.0281, -0.0290, -0.0299, -0.0315, -0.0328, -0.0336, -0.0344, -0.0352, -0.0354, -0.0353, -0.0358, -0.0355, -0.0347, -0.0344, -0.0338, -0.0335, -0.0324, -0.0319, -0.0319, -0.0322, -0.0334, -0.0351, -0.0365, -0.0381, -0.0401, -0.0424, -0.0449, -0.0472, -0.0491, -0.0506, -0.0522, -0.0533, -0.0544, -0.0553, -0.0553, -0.0557, -0.0558, -0.0563, -0.0573, -0.0587, -0.0602, -0.0617, -0.0636, -0.0666, -0.0699, -0.0734, -0.0760, -0.0791, -0.0817, -0.0840, -0.0857, -0.0869, -0.0881, -0.0882, -0.0884, -0.0888, -0.0893, -0.0889, -0.0876, -0.0869, -0.0858, -0.0851, -0.0836, -0.0825, -0.0813, -0.0807, -0.0800, -0.0802, -0.0805, -0.0807, -0.0813, -0.0818, -0.0819, -0.0822, -0.0829, -0.0825, -0.0829, -0.0838, -0.0840, -0.0836, -0.0824, -0.0810, -0.0799, -0.0789, -0.0785, -0.0785, -0.0774, -0.0770, -0.0761, -0.0755, -0.0752, -0.0746, -0.0744, -0.0741, -0.0729, -0.0718, -0.0710, -0.0694, -0.0673, -0.0649, -0.0627, -0.0606, -0.0592, -0.0582, -0.0571, -0.0555, -0.0539, -0.0527, -0.0516, -0.0508, -0.0503, -0.0508, -0.0518, -0.0527, -0.0536, -0.0544, -0.0545, -0.0541, -0.0547, -0.0553, -0.0561, -0.0573, -0.0587, -0.0600, -0.0608, -0.0616, -0.0624, -0.0626, -0.0631, -0.0638, -0.0630, -0.0621, -0.0612, -0.0603, -0.0594, -0.0577, -0.0564, -0.0551, -0.0535, -0.0511, -0.0485, -0.0455, -0.0424, -0.0396, -0.0374, -0.0360, -0.0351, -0.0343, -0.0336, -0.0324, -0.0311, -0.0296, -0.0276, -0.0259, -0.0240, -0.0223, -0.0212, -0.0205, -0.0201, -0.0203, -0.0214, -0.0232, -0.0261, -0.0285, -0.0304, -0.0320, -0.0342, -0.0365, -0.0381, -0.0394, -0.0416, -0.0434, -0.0443, -0.0450, -0.0465, -0.0482, -0.0490, -0.0497, -0.0508, -0.0506, -0.0509, -0.0514, -0.0522, -0.0525, -0.0532, -0.0547, -0.0553, -0.0575, -0.0597, -0.0629, -0.0666, -0.0702, -0.0737, -0.0773, -0.0805, -0.0835, -0.0847, -0.0875, -0.0893, -0.0913, -0.0929, -0.0935, -0.0943, -0.0949, -0.0957, -0.0962, -0.0977, -0.0994, -0.1011, -0.1031, -0.1057, -0.1086, -0.1114, -0.1141, -0.1158, -0.1174, -0.1197, -0.1219, -0.1234, -0.1250, -0.1267, -0.1273, -0.1270, -0.1268, -0.1263, -0.1257, -0.1240, -0.1220, -0.1210, -0.1197, -0.1197, -0.1188, -0.1171, -0.1147, -0.1122, -0.1101, -0.1077, -0.1052, -0.1031, -0.1020, -0.1008, -0.0983, -0.0960, -0.0934, -0.0908, -0.0889, -0.0865, -0.0836, -0.0799, -0.0767, -0.0740, -0.0716, -0.0692, -0.0663, -0.0628, -0.0596, -0.0567, -0.0535, -0.0508, -0.0472, -0.0435, -0.0394, -0.0347, -0.0301, -0.0253, -0.0210, -0.0165, -0.0126, -0.0095, -0.0072, -0.0046, -0.0020, -0.0001, 0.0023, 0.0050, 0.0077, 0.0102, 0.0135, 0.0175, 0.0212, 0.0245, 0.0280, 0.0309, 0.0336, 0.0365, 0.0397, 0.0433, 0.0474, 0.0511, 0.0546, 0.0574, 0.0602, 0.0634, 0.0663, 0.0695, 0.0729, 0.0752, 0.0762, 0.0773, 0.0781, 0.0790, 0.0806, 0.0836, 0.0857, 0.0879, 0.0896, 0.0920, 0.0949, 0.0975, 0.1002, ])
  2195. # COMPOSITE types:
  2196. # MassSpringForce: Line(horizontal), Spring, Rectangle, Arrow/Line(w/arrow)
  2197. # must be easy to find the tip of the arrow
  2198. # Maybe extra dict: self.name['mass'] = Rectangle object - YES!
  2199. def test_Axis():
  2200. drawing_tool.set_coordinate_system(
  2201. xmin=0, xmax=15, ymin=-7, ymax=8, axis=True,
  2202. instruction_file='tmp_Axis.py')
  2203. x_axis = Axis((7.5,2), 5, 'x', rotation_angle=0)
  2204. y_axis = Axis((7.5,2), 5, 'y', rotation_angle=90)
  2205. system = Composition({'x axis': x_axis, 'y axis': y_axis})
  2206. system.draw()
  2207. drawing_tool.display()
  2208. system.set_linestyle('dashed')
  2209. system.rotate(40, (7.5,2))
  2210. system.draw()
  2211. drawing_tool.display()
  2212. system.set_linestyle('dotted')
  2213. system.rotate(220, (7.5,2))
  2214. system.draw()
  2215. drawing_tool.display()
  2216. drawing_tool.display('Axis')
  2217. drawing_tool.savefig('tmp_Axis.png')
  2218. print repr(system)
  2219. def test_Distance_wText():
  2220. drawing_tool.set_coordinate_system(xmin=0, xmax=10,
  2221. ymin=0, ymax=6,
  2222. axis=True,
  2223. instruction_file='tmp_Distance_wText.py')
  2224. #drawing_tool.arrow_head_width = 0.1
  2225. fontsize=14
  2226. t = r'$ 2\pi R^2 $'
  2227. dims2 = Composition({
  2228. 'a0': Distance_wText((4,5), (8, 5), t, fontsize),
  2229. 'a6': Distance_wText((4,5), (4, 4), t, fontsize),
  2230. 'a1': Distance_wText((0,2), (2, 4.5), t, fontsize),
  2231. 'a2': Distance_wText((0,2), (2, 0), t, fontsize),
  2232. 'a3': Distance_wText((2,4.5), (0, 5.5), t, fontsize),
  2233. 'a4': Distance_wText((8,4), (10, 3), t, fontsize,
  2234. text_spacing=-1./60),
  2235. 'a5': Distance_wText((8,2), (10, 1), t, fontsize,
  2236. text_spacing=-1./40, alignment='right'),
  2237. 'c1': Text_wArrow('text_spacing=-1./60',
  2238. (4, 3.5), (9, 3.2),
  2239. fontsize=10, alignment='left'),
  2240. 'c2': Text_wArrow('text_spacing=-1./40, alignment="right"',
  2241. (4, 0.5), (9, 1.2),
  2242. fontsize=10, alignment='left'),
  2243. })
  2244. dims2.draw()
  2245. drawing_tool.display('Distance_wText and text positioning')
  2246. drawing_tool.savefig('tmp_Distance_wText.png')
  2247. def test_Rectangle():
  2248. L = 3.0
  2249. W = 4.0
  2250. drawing_tool.set_coordinate_system(xmin=0, xmax=2*W,
  2251. ymin=-L/2, ymax=2*L,
  2252. axis=True,
  2253. instruction_file='tmp_Rectangle.py')
  2254. drawing_tool.set_linecolor('blue')
  2255. drawing_tool.set_grid(True)
  2256. xpos = W/2
  2257. r = Rectangle(lower_left_corner=(xpos,0), width=W, height=L)
  2258. r.draw()
  2259. r.draw_dimensions()
  2260. drawing_tool.display('Rectangle')
  2261. drawing_tool.savefig('tmp_Rectangle.png')
  2262. def test_Triangle():
  2263. L = 3.0
  2264. W = 4.0
  2265. drawing_tool.set_coordinate_system(xmin=0, xmax=2*W,
  2266. ymin=-L/2, ymax=1.2*L,
  2267. axis=True,
  2268. instruction_file='tmp_Triangle.py')
  2269. drawing_tool.set_linecolor('blue')
  2270. drawing_tool.set_grid(True)
  2271. xpos = 1
  2272. t = Triangle(p1=(W/2,0), p2=(3*W/2,W/2), p3=(4*W/5.,L))
  2273. t.draw()
  2274. t.draw_dimensions()
  2275. drawing_tool.display('Triangle')
  2276. drawing_tool.savefig('tmp_Triangle.png')
  2277. def test_Arc():
  2278. L = 4.0
  2279. W = 4.0
  2280. drawing_tool.set_coordinate_system(xmin=-W/2, xmax=W,
  2281. ymin=-L/2, ymax=1.5*L,
  2282. axis=True,
  2283. instruction_file='tmp_Arc.py')
  2284. drawing_tool.set_linecolor('blue')
  2285. drawing_tool.set_grid(True)
  2286. center = point(0,0)
  2287. radius = L/2
  2288. start_angle = 60
  2289. arc_angle = 45
  2290. a = Arc(center, radius, start_angle, arc_angle)
  2291. a.set_arrow('->')
  2292. a.draw()
  2293. R1 = 1.25*radius
  2294. R2 = 1.5*radius
  2295. R = 2*radius
  2296. a.dimensions = {
  2297. 'start_angle': Arc_wText(
  2298. 'start_angle', center, R1, start_angle=0,
  2299. arc_angle=start_angle, text_spacing=1/10.),
  2300. 'arc_angle': Arc_wText(
  2301. 'arc_angle', center, R2, start_angle=start_angle,
  2302. arc_angle=arc_angle, text_spacing=1/20.),
  2303. 'r=0': Line(center, center +
  2304. point(R*cos(radians(start_angle)),
  2305. R*sin(radians(start_angle)))),
  2306. 'r=start_angle': Line(center, center +
  2307. point(R*cos(radians(start_angle+arc_angle)),
  2308. R*sin(radians(start_angle+arc_angle)))),
  2309. 'r=start+arc_angle': Line(center, center +
  2310. point(R, 0)).set_linestyle('dashed'),
  2311. 'radius': Distance_wText(center, a(0), 'radius', text_spacing=1/40.),
  2312. 'center': Text('center', center-point(radius/10., radius/10.)),
  2313. }
  2314. for dimension in a.dimensions:
  2315. dim = a.dimensions[dimension]
  2316. dim.set_linestyle('dashed')
  2317. dim.set_linewidth(1)
  2318. dim.set_linecolor('black')
  2319. a.draw_dimensions()
  2320. drawing_tool.display('Arc')
  2321. drawing_tool.savefig('tmp_Arc.png')
  2322. def test_Spring():
  2323. L = 5.0
  2324. W = 2.0
  2325. drawing_tool.set_coordinate_system(xmin=0, xmax=7*W,
  2326. ymin=-L/2, ymax=1.5*L,
  2327. axis=True,
  2328. instruction_file='tmp_Spring.py')
  2329. drawing_tool.set_linecolor('blue')
  2330. drawing_tool.set_grid(True)
  2331. xpos = W
  2332. s1 = Spring((W,0), L, teeth=True)
  2333. s1_title = Text('Default Spring', s1.geometric_features()['end'] + point(0,L/10))
  2334. s1.draw()
  2335. s1_title.draw()
  2336. #s1.draw_dimensions()
  2337. xpos += 3*W
  2338. s2 = Spring(start=(xpos,0), length=L, width=W/2.,
  2339. bar_length=L/6., teeth=False)
  2340. s2.draw()
  2341. s2.draw_dimensions()
  2342. drawing_tool.display('Spring')
  2343. drawing_tool.savefig('tmp_Spring.png')
  2344. def test_Dashpot():
  2345. L = 5.0
  2346. W = 2.0
  2347. xpos = 0
  2348. drawing_tool.set_coordinate_system(xmin=xpos, xmax=xpos+5.5*W,
  2349. ymin=-L/2, ymax=1.5*L,
  2350. axis=True,
  2351. instruction_file='tmp_Dashpot.py')
  2352. drawing_tool.set_linecolor('blue')
  2353. drawing_tool.set_grid(True)
  2354. # Default (simple) dashpot
  2355. xpos = 1.5
  2356. d1 = Dashpot(start=(xpos,0), total_length=L)
  2357. d1_title = Text('Dashpot (default)', d1.geometric_features()['end'] + point(0,L/10))
  2358. d1.draw()
  2359. d1_title.draw()
  2360. # Dashpot for animation with fixed bar_length, dashpot_length and
  2361. # prescribed piston_pos
  2362. xpos += 2.5*W
  2363. d2 = Dashpot(start=(xpos,0), total_length=1.2*L, width=W/2,
  2364. bar_length=W, dashpot_length=L/2, piston_pos=2*W)
  2365. d2.draw()
  2366. d2.draw_dimensions()
  2367. drawing_tool.display('Dashpot')
  2368. drawing_tool.savefig('tmp_Dashpot.png')
  2369. def test_Wavy():
  2370. drawing_tool.set_coordinate_system(xmin=0, xmax=1.5,
  2371. ymin=-0.5, ymax=5,
  2372. axis=True,
  2373. instruction_file='tmp_Wavy.py')
  2374. w = Wavy(main_curve=lambda x: 1 + sin(2*x),
  2375. interval=[0,1.5],
  2376. wavelength_of_perturbations=0.3,
  2377. amplitude_of_perturbations=0.1,
  2378. smoothness=0.05)
  2379. w.draw()
  2380. drawing_tool.display('Wavy')
  2381. drawing_tool.savefig('tmp_Wavy.png')
  2382. def diff_files(files1, files2, mode='HTML'):
  2383. import difflib, time
  2384. n = 3
  2385. for fromfile, tofile in zip(files1, files2):
  2386. fromdate = time.ctime(os.stat(fromfile).st_mtime)
  2387. todate = time.ctime(os.stat(tofile).st_mtime)
  2388. fromlines = open(fromfile, 'U').readlines()
  2389. tolines = open(tofile, 'U').readlines()
  2390. diff_html = difflib.HtmlDiff().\
  2391. make_file(fromlines,tolines,
  2392. fromfile,tofile,context=True,numlines=n)
  2393. diff_plain = difflib.unified_diff(fromlines, tolines, fromfile, tofile, fromdate, todate, n=n)
  2394. filename_plain = fromfile + '.diff.txt'
  2395. filename_html = fromfile + '.diff.html'
  2396. if os.path.isfile(filename_plain):
  2397. os.remove(filename_plain)
  2398. if os.path.isfile(filename_html):
  2399. os.remove(filename_html)
  2400. f = open(filename_plain, 'w')
  2401. f.writelines(diff_plain)
  2402. f.close()
  2403. size = os.path.getsize(filename_plain)
  2404. if size > 4:
  2405. print 'found differences:', fromfile, tofile
  2406. f = open(filename_html, 'w')
  2407. f.writelines(diff_html)
  2408. f.close()
  2409. def test_test():
  2410. os.chdir('test')
  2411. funcs = [name for name in globals() if name.startswith('test_') and callable(globals()[name])]
  2412. funcs.remove('test_test')
  2413. new_files = []
  2414. res_files = []
  2415. for func in funcs:
  2416. mplfile = func.replace('test_', 'tmp_') + '.py'
  2417. #exec(func + '()')
  2418. new_files.append(mplfile)
  2419. resfile = mplfile.replace('tmp_', 'res_')
  2420. res_files.append(resfile)
  2421. diff_files(new_files, res_files)
  2422. def _test1():
  2423. set_coordinate_system(xmin=0, xmax=10, ymin=0, ymax=10)
  2424. l1 = Line((0,0), (1,1))
  2425. l1.draw()
  2426. input(': ')
  2427. c1 = Circle((5,2), 1)
  2428. c2 = Circle((6,2), 1)
  2429. w1 = Wheel((7,2), 1)
  2430. c1.draw()
  2431. c2.draw()
  2432. w1.draw()
  2433. hardcopy()
  2434. display() # show the plot
  2435. def _test2():
  2436. set_coordinate_system(xmin=0, xmax=10, ymin=0, ymax=10)
  2437. l1 = Line((0,0), (1,1))
  2438. l1.draw()
  2439. input(': ')
  2440. c1 = Circle((5,2), 1)
  2441. c2 = Circle((6,2), 1)
  2442. w1 = Wheel((7,2), 1)
  2443. filled_curves(True)
  2444. set_linecolor('blue')
  2445. c1.draw()
  2446. set_linecolor('aqua')
  2447. c2.draw()
  2448. filled_curves(False)
  2449. set_linecolor('red')
  2450. w1.draw()
  2451. hardcopy()
  2452. display() # show the plot
  2453. def _test3():
  2454. """Test example from the book."""
  2455. set_coordinate_system(xmin=0, xmax=10, ymin=0, ymax=10)
  2456. l1 = Line(start=(0,0), stop=(1,1)) # define line
  2457. l1.draw() # make plot data
  2458. r1 = Rectangle(lower_left_corner=(0,1), width=3, height=5)
  2459. r1.draw()
  2460. Circle(center=(5,7), radius=1).draw()
  2461. Wheel(center=(6,2), radius=2, inner_radius=0.5, nlines=7).draw()
  2462. hardcopy()
  2463. display()
  2464. def _test4():
  2465. """Second example from the book."""
  2466. set_coordinate_system(xmin=0, xmax=10, ymin=0, ymax=10)
  2467. r1 = Rectangle(lower_left_corner=(0,1), width=3, height=5)
  2468. c1 = Circle(center=(5,7), radius=1)
  2469. w1 = Wheel(center=(6,2), radius=2, inner_radius=0.5, nlines=7)
  2470. c2 = Circle(center=(7,7), radius=1)
  2471. filled_curves(True)
  2472. c1.draw()
  2473. set_linecolor('blue')
  2474. r1.draw()
  2475. set_linecolor('aqua')
  2476. c2.draw()
  2477. # Add thick aqua line around rectangle:
  2478. filled_curves(False)
  2479. set_linewidth(4)
  2480. r1.draw()
  2481. set_linecolor('red')
  2482. # Draw wheel with thick lines:
  2483. w1.draw()
  2484. hardcopy('tmp_colors')
  2485. display()
  2486. def _test5():
  2487. set_coordinate_system(xmin=0, xmax=10, ymin=0, ymax=10)
  2488. c = 6. # center point of box
  2489. w = 2. # size of box
  2490. L = 3
  2491. r1 = Rectangle((c-w/2, c-w/2), w, w)
  2492. l1 = Line((c,c-w/2), (c,c-w/2-L))
  2493. linecolor('blue')
  2494. filled_curves(True)
  2495. r1.draw()
  2496. linecolor('aqua')
  2497. filled_curves(False)
  2498. l1.draw()
  2499. hardcopy()
  2500. display() # show the plot
  2501. def rolling_wheel(total_rotation_angle):
  2502. """Animation of a rotating wheel."""
  2503. set_coordinate_system(xmin=0, xmax=10, ymin=0, ymax=10)
  2504. import time
  2505. center = (6,2)
  2506. radius = 2.0
  2507. angle = 2.0
  2508. pngfiles = []
  2509. w1 = Wheel(center=center, radius=radius, inner_radius=0.5, nlines=7)
  2510. for i in range(int(total_rotation_angle/angle)):
  2511. w1.draw()
  2512. print 'XXXX BIG PROBLEM WITH ANIMATE!!!'
  2513. display()
  2514. filename = 'tmp_%03d' % i
  2515. pngfiles.append(filename + '.png')
  2516. hardcopy(filename)
  2517. time.sleep(0.3) # pause
  2518. L = radius*angle*pi/180 # translation = arc length
  2519. w1.rotate(angle, center)
  2520. w1.translate((-L, 0))
  2521. center = (center[0] - L, center[1])
  2522. erase()
  2523. cmd = 'convert -delay 50 -loop 1000 %s tmp_movie.gif' \
  2524. % (' '.join(pngfiles))
  2525. print 'converting PNG files to animated GIF:\n', cmd
  2526. import commands
  2527. failure, output = commands.getstatusoutput(cmd)
  2528. if failure: print 'Could not run', cmd
  2529. if __name__ == '__main__':
  2530. #rolling_wheel(40)
  2531. #_test1()
  2532. #_test3()
  2533. funcs = [
  2534. #test_Axis,
  2535. test_inclined_plane,
  2536. ]
  2537. for func in funcs:
  2538. func()
  2539. raw_input('Type Return: ')