L4K ︰ Python Turtle《七》

一日小海龜聽聞有人能用派生畫『心』︰

How can I draw a heart using Python?

import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0,2*np.pi, 0.1)
x = 16*np.sin(t)**3
y = 13*np.cos(t)-5*np.cos(2*t)-2*np.cos(3*t)-np.cos(4*t)
plt.plot(x,y)
plt.show()

 

figure_heart

 

雖然『折線』之法尚不純熟,仍想嘗試一番︰

up@up-UP-CHT01:~python3 Python 3.5.2 (default, Nov 17 2016, 17:05:23)  [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import turtle, math >>> turtle.setup(width=1024, height=768) >>> turtle.shape('turtle') >>> turtle.mode('logo') >>>  >>> t = [math.pi/180*n for n in range(361)] >>> x = [80*math.sin(t[n])**3 for n in range(361)] >>> y = [65*math.cos(t[n])- 25*math.cos(2*t[n])-10*math.cos(3*t[n])-5*math.cos(4*t[n]) for n in range(361)] >>> 點距 = [math.sqrt( (y[n+1]-y[n])**2 + (x[n+1]-x[n])**2 ) for n in range(360)] >>> 朝向 = [90 - 180.0/math.pi*math.atan( (y[n+1]-y[n])/(x[n+1]-x[n]) ) for n in range(360)] >>> 起始角度 = 朝向[0] >>> 第一步長度 = 點距[0] >>> 餘步列 = 點距[1:] >>>  >>> def 心動(): ...     保存原向 = turtle.heading() ...     turtle.right(起始角度) ...     turtle.forward(第一步長度) ...     for n in range(359): ...         if 朝向[n+1] > 朝向[n]: ...             turtle.right(朝向[n+1]-朝向[n]) ...         else: ...             turtle.left(朝向[n]-朝向[n+1]) ...         turtle.forward(餘步列[n]) ...     turtle.seth(保存原向) ...  >>> 心動() >>>  </pre>    <span style="color: #003300;">怎曉心動時刻卻惹得心慌︰</span>  <img class="alignnone size-full wp-image-64125" src="http://www.freesandal.org/wp-content/uploads/Python-Turtle-Graphics_心動.png" alt="python-turtle-graphics_%e5%bf%83%e5%8b%95" width="1024" height="796" />     <span style="color: #003300;">苦思冥想之餘,決定以此法畫『○』,探究其因︰</span> <pre class="lang:python decode:true ">>>> xc = [100*math.sin(t[n]) for n in range(361)]  >>> yc = [100*math.cos(t[n]) for n in range(361)] >>> 點距 = [math.sqrt( (yc[n+1]-yc[n])**2 + (xc[n+1]-xc[n])**2 ) for n in range(360)] >>> 朝向 = [90 - 180.0/math.pi*math.atan( (yc[n+1]-yc[n])/(xc[n+1]-xc[n]) ) for n in range(360)] >>> 起始角度 = 朝向[0] >>> 第一步長度 = 點距[0] >>> 餘步列 = 點距[1:] >>> turtle.reset();心動() >>>  </pre>    <span style="color: #003300;">為何幾度頭昏腦脹,朝向錯誤耶??</span>  <img class="alignnone size-full wp-image-64131" src="http://www.freesandal.org/wp-content/uploads/Python-Turtle-Graphics_圓.png" alt="python-turtle-graphics_%e5%9c%93" width="1024" height="796" />     <span style="color: #003300;">仔細推步『圓的困惑』之後,終知萬一x[n+1] - x[n] \approx 0,那麼『斜率』\frac{y[n+1] - y[n]}{x[n+1] - x[n]}前後難測也!!此乃算林傳說之『ill-conditioned』條件數病態乎★★</span> <h1 id="firstHeading" class="firstHeading" lang="en"><span style="color: #ff9900;"><a style="color: #ff9900;" href="https://en.wikipedia.org/wiki/Condition_number">Condition number</a></span></h1> <span style="color: #808080;">In the field of <a style="color: #808080;" title="Numerical analysis" href="https://en.wikipedia.org/wiki/Numerical_analysis">numerical analysis</a>, the <b>condition number</b> of a function with respect to an argument measures how much the output value of the function can change for a small change in the input argument. This is used to measure how sensitive a function is to changes or errors in the input, and how much error in the output results from an error in the input. Very frequently, one is solving the inverse problem - given  <img class="mwe-math-fallback-image-inline" src="https://wikimedia.org/api/rest_v1/media/math/render/svg/cccc04a8c310cc7a356bbba88e8fb884e925aceb" alt="f(x) = y," /> one is solving for <i>x,</i> and thus the condition number of the (local) inverse must be used. In <a style="color: #808080;" title="Linear regression" href="https://en.wikipedia.org/wiki/Linear_regression">linear regression</a> the condition number can be used as a diagnostic for <a style="color: #808080;" title="Multicollinearity" href="https://en.wikipedia.org/wiki/Multicollinearity">multicollinearity</a>.<sup id="cite_ref-1" class="reference"><a style="color: #808080;" href="https://en.wikipedia.org/wiki/Condition_number#cite_note-1">[1]</a></sup><sup id="cite_ref-2" class="reference"><a style="color: #808080;" href="https://en.wikipedia.org/wiki/Condition_number#cite_note-2">[2]</a></sup></span>  <span style="color: #808080;">The condition number is an application of the derivative, and is formally defined as the value of the asymptotic worst-case relative change in output for a relative change in input. The "function" is the solution of a problem and the "arguments" are the data in the problem. The condition number is frequently applied to questions in linear algebra, in which case the derivative is straightforward but the error could be in many different directions, and is thus computed from the geometry of the matrix. More generally, condition numbers can be defined for non-linear functions in several variables.</span>  <span style="color: #808080;">A problem with a low condition number is said to be <b>well-conditioned</b>, while a problem with a high condition number is said to be <b>ill-conditioned</b>. The condition number is a property of the problem. Paired with the problem are any number of algorithms that can be used to solve the problem, that is, to calculate the solution. Some algorithms have a property called <b><a style="color: #808080;" title="Numerical stability" href="https://en.wikipedia.org/wiki/Numerical_stability">backward stability</a></b>. In general, a backward stable algorithm can be expected to accurately solve well-conditioned problems. Numerical analysis textbooks give formulas for the condition numbers of problems and identify the backward stable algorithms.</span>  <span style="color: #808080;">As a rule of thumb, if the condition number  <img class="mwe-math-fallback-image-inline" src="https://wikimedia.org/api/rest_v1/media/math/render/svg/badf4fba0b778d73e22b71492a153fe05cd5ac35" alt="\kappa(A) = 10^k" />, then you may lose up to  <img class="mwe-math-fallback-image-inline" src="https://wikimedia.org/api/rest_v1/media/math/render/svg/c3c9a2c7b599b37105512c5d570edc034056dd40" alt="k" /> digits of accuracy on top of what would be lost to the numerical method due to loss of precision from arithmetic methods.<sup id="cite_ref-Numerical_Mathematics_and_Computing.2C_by_Cheney_and_Kincaid_3-0" class="reference"><a style="color: #808080;" href="https://en.wikipedia.org/wiki/Condition_number#cite_note-Numerical_Mathematics_and_Computing.2C_by_Cheney_and_Kincaid-3">[3]</a></sup> However, the condition number does not give the exact value of the maximum inaccuracy that may occur in the algorithm. It generally just bounds it with an estimate (whose computed value depends on the choice of the norm to measure the inaccuracy).</span>     <span style="color: #003300;">於是勤練不動心『直達』goto 之法︰</span> <pre class="lang:python decode:true ">>>> t = [math.pi/180*n for n in range(361)] >>> x = [80*math.sin(t[n])**3 for n in range(361)] >>> y = [65*math.cos(t[n])- 25*math.cos(2*t[n])-10*math.cos(3*t[n])-5*math.cos(4*t[n]) for n in range(361)] >>> x0 = x[0] >>> xr = x[1:] >>> y0 = y[0] >>> yr = y[1:] >>>  >>> def GOTO(): ...     turtle.penup() ...     turtle.goto(x0, y0) ...     turtle.pendown() ...     for n in range(360): ...         turtle.goto(x[n], y[n]) ...  >>> turtle.reset();GOTO() >>>  </pre>    <img class="alignnone size-full wp-image-64144" src="http://www.freesandal.org/wp-content/uploads/Python-Turtle-Graphics_動心.png" alt="python-turtle-graphics_%e5%8b%95%e5%bf%83" width="1024" height="796" />     <span style="color: #003300;">打算掌萬花尺之訣竅矣☆☆</span>  <div class="wc-shortcodes-row wc-shortcodes-item wc-shortcodes-clearfix"><div class="wc-shortcodes-column wc-shortcodes-content wc-shortcodes-two-third wc-shortcodes-column-first ">  一八八零年代<strong>波蘭</strong>數學家 <strong>Bruno Abakanowicz</strong> 發明了『<strong>螺旋圖</strong>』Spirograph 。一九六四年<strong>英國</strong>工程師<strong>德尼斯‧費舍爾</strong> Denys Fisher 發現使用多種『<strong>大小比值</strong>』不同的兩個內外『<strong>圓形齒輪</strong>』,當『<strong>內小圓形齒輪</strong>』上不同的『<strong>筆洞位置</strong>』在『<strong>外大圓形齒輪</strong>』上『<strong>循著圓周</strong>』轉動時,可以畫出各種美麗的『<strong>內旋輪線</strong>』 hypotrochoid 以及『<strong>外旋輪線</strong> 』epitrochoid。在經過一番齒輪『<strong>大小比值</strong>』與筆洞『<strong>位置比值</strong>』的研究後,費舍爾於隔年一九六五年的<strong>德國</strong> Nuremberg 國際玩具展將之發表上市。由於它所繪出的『<strong>圖案</strong>』令人聯想到『<strong>萬花筒</strong>』 ,所以被我們叫做『<strong>萬花尺</strong>』。這是一個曾經『<strong>流行</strong>』過的『<strong>益智玩具</strong>』,其實它是了解『<strong>周期運動</strong>』組合的『<strong>複雜性</strong>』很好的『<strong>工具</strong>』。萬花尺內旋輪線的參數方程式可以表示為\begin{array}{rcl} x(t)&=&R\left[(1-k)\cos t+lk\cos \frac{1-k}{k}t\right] ,\\[4pt] y(t)&=&R\left[(1-k)\sin t-lk\sin \frac{1-k}{k}t\right] .\\\end{array}此處R是『<strong>外大齒輪</strong>』的半徑,k=\frac{r}{R}是大小齒輪的『<strong>半徑比</strong>』0\le k \le 1,l=\frac{\rho}{r}是筆洞所在位置到『<strong>內小齒輪</strong>』圓心的距離與『<strong>內小齒輪</strong>』半徑的比值0\le l \le 1$。

網路上有一個『Spirograph Art』的網頁,假使讀者有興趣的話,不妨前去看看。

HypotrochoidOutThreeFifths

220px-Ellipse_as_hypotrochoid

200px-Spirograph

120px-Hypotrochoid2_2

120px-Epitrochoid_3