JavaScript图形实例:黄金螺旋线
<p><span style="font-size: 14px"> 黄金螺旋线是根据斐波那契数列画出来的螺旋曲线。自然界中存在许多黄金螺旋线的图案,是自然界最完美的经典黄金比例。例如,如图1所示的海螺身上的曲线,图2所示的漩涡,图3所示的人耳。</span></p><p style="text-align: center"><span style="font-size: 14px"><img src="https://img2020.cnblogs.com/blog/1485495/202006/1485495-20200628091047340-595370040.png"></span></p>
<p align="center">图1 海螺</p>
<p align="center"><img src="https://img2020.cnblogs.com/blog/1485495/202006/1485495-20200628091000064-1289596460.png"></p>
<p align="center">图2 漩涡</p>
<p align="center"> <img src="https://img2020.cnblogs.com/blog/1485495/202006/1485495-20200628090951250-1962982321.png"></p>
<p align="center">图3 人耳</p>
<p> 又如,名画蒙娜丽莎的微笑整个画面所呈现的数学美,如图4所示。</p>
<p align="center"> <img src="https://img2020.cnblogs.com/blog/1485495/202006/1485495-20200628090939994-948444319.png"></p>
<p align="center">图4 蒙娜丽莎的微笑</p>
<p> 黄金螺旋线的绘制思想是:以斐波那契数为边长的正方形拼成长方形,然后在正方形里面画一个90度的弧线,连接起来的弧线就是黄金螺旋线。</p>
<p> 例如,先绘制一个边长为8的正方形,再在边长为8的正方形左侧贴着其底边的左沿线作出一个边长为5的正方形,如图5;接着按照图5的样子分别作出边长为3和边长为2的正方形,最后作出两个边长为1的正方形。这6个正方形构成一个长为13、宽为8的长方形。 </p>
<p><img src="https://img2020.cnblogs.com/blog/1485495/202006/1485495-20200628090930354-1933225074.png"></p>
<p align="center">图5 6个正方形构成一个长为13、宽为8的长方形</p>
<p> 在每个正方形内画出圆弧,顺次连接起来作出一条如图6所示的黄金螺旋线。 </p>
<p align="center"><img src="https://img2020.cnblogs.com/blog/1485495/202006/1485495-20200628090919438-958255910.png"></p>
<p align="center">图6 作出的黄金螺旋线<em id="__mceDel"> </em></p>
<p style="margin-left: 30px">为在Canvas画布中作出如图6所示的黄金螺旋线,编写如下的HTML代码。</p>
<p style="margin-left: 30px"><!DOCTYPE html></p>
<p style="margin-left: 30px"><head></p>
<p style="margin-left: 30px"><title>黄金螺旋线</title></p>
<p style="margin-left: 30px"><script type="text/javascript"></p>
<p style="margin-left: 30px"> function draw(id)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> var canvas=document.getElementById(id);</p>
<p style="margin-left: 30px"> if (canvas==null)</p>
<p style="margin-left: 30px"> return false;</p>
<p style="margin-left: 30px"> var context=canvas.getContext('2d');</p>
<p style="margin-left: 30px"> context.fillStyle="red";</p>
<p style="margin-left: 30px"> context.fillRect(0,0,400,300);</p>
<p style="margin-left: 30px"> context.lineWidth=3;</p>
<p style="margin-left: 30px"> context.translate(80,100);</p>
<p style="margin-left: 30px"> var r=20</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> context.arc(5*r,2*r,1*r,Math.PI,Math.PI*2,true);</p>
<p style="margin-left: 30px"> context.arc(4*r,2*r,2*r,0,Math.PI*3/2,true);</p>
<p style="margin-left: 30px"> context.arc(4*r,3*r,3*r,Math.PI*3/2,Math.PI,true);</p>
<p style="margin-left: 30px"> context.arc(6*r,3*r,5*r,Math.PI,Math.PI/2,true);</p>
<p style="margin-left: 30px"> context.arc(6*r,0,8*r,Math.PI/2,Math.PI*2,true);</p>
<p style="margin-left: 30px"> context.strokeStyle = 'yellow';</p>
<p style="margin-left: 30px"> context.stroke();</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"></script></p>
<p style="margin-left: 30px"></head></p>
<p style="margin-left: 30px"><body onload="draw('myCanvas');"></p>
<p style="margin-left: 30px"><canvas id="myCanvas" width="400" height="300"></canvas></p>
<p style="margin-left: 30px"></body></p>
<p style="margin-left: 30px"></html></p>
<p> 将上述HTML代码保存到一个html文本文件中,再在浏览器中打开包含这段HTML代码的html文件,可以看到在浏览器窗口中绘制出一条黄金螺旋线,如图7所示。</p>
<p align="center"> <img src="https://img2020.cnblogs.com/blog/1485495/202006/1485495-20200628090908882-943524094.png"></p>
<p align="center">图7 黄金螺旋线</p>
<p style="margin-left: 30px">为在Canvas画布中作出如图4所示的黄金螺旋线,编写如下的HTML代码。</p>
<p style="margin-left: 30px"><!DOCTYPE html></p>
<p style="margin-left: 30px"><head></p>
<p style="margin-left: 30px"><title>黄金螺旋线</title></p>
<p style="margin-left: 30px"><script type="text/javascript"></p>
<p style="margin-left: 30px"> function draw(id)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> var canvas=document.getElementById(id);</p>
<p style="margin-left: 30px"> if (canvas==null)</p>
<p style="margin-left: 30px"> return false;</p>
<p style="margin-left: 30px"> var context=canvas.getContext('2d');</p>
<p style="margin-left: 30px"> context.fillStyle="red";</p>
<p style="margin-left: 30px"> context.fillRect(0,0,200,300);</p>
<p style="margin-left: 30px"> context.lineWidth=3;</p>
<p style="margin-left: 30px"> context.translate(40,40);</p>
<p style="margin-left: 30px"> var r=10</p>
<p style="margin-left: 30px"> context.beginPath();</p>
<p style="margin-left: 30px"> context.arc(4*r,6*r,1*r,Math.PI,Math.PI*2,false);</p>
<p style="margin-left: 30px"> context.arc(3*r,6*r,2*r,0,Math.PI/2,false);</p>
<p style="margin-left: 30px"> context.arc(3*r,5*r,3*r,Math.PI/2,Math.PI,false);</p>
<p style="margin-left: 30px"> context.arc(5*r,5*r,5*r,Math.PI,Math.PI*3/2,false);</p>
<p style="margin-left: 30px"> context.arc(5*r,8*r,8*r,Math.PI*3/2,Math.PI*2,false);</p>
<p style="margin-left: 30px"> context.arc(0,8*r,13*r,0,Math.PI/2,false);</p>
<p style="margin-left: 30px"> context.strokeStyle = 'yellow';</p>
<p style="margin-left: 30px"> context.stroke();</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"></script></p>
<p style="margin-left: 30px"></head></p>
<p style="margin-left: 30px"><body onload="draw('myCanvas');"></p>
<p style="margin-left: 30px"><canvas id="myCanvas" width="200" height="300"></canvas></p>
<p style="margin-left: 30px"></body></p>
<p style="margin-left: 30px"></html></p>
<p> 将上述HTML代码保存到一个html文本文件中,再在浏览器中打开包含这段HTML代码的html文件,可以看到在浏览器窗口中绘制出一条黄金螺旋线,如图8所示。</p>
<p align="center"> <img src="https://img2020.cnblogs.com/blog/1485495/202006/1485495-20200628090853987-804556191.png"></p>
<p align="center">图8 人耳黄金螺旋线</p><br><br>
来源:https://www.cnblogs.com/cs-whut/p/13201138.html
頁:
[1]