JavaScript图形实例:平面镶嵌图案
<p><span style="font-size: 14px"> 用形状、大小完全相同的一种或几种平面图形进行拼接,彼此之间不留空隙、不重叠地铺成一片,就叫做这几种图形的平面镶嵌。</span></p><h3>1.用一种多边形实现的平面镶嵌图案</h3>
<p> 我们可以采用正三角形、正方形或正六边形实现平面镶嵌。</p>
<p style="margin-left: 30px">(1)用正方形平铺。</p>
<p style="margin-left: 30px">用正方形进行平面镶嵌比较简单,编写如下的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"></head></p>
<p style="margin-left: 30px"><body></p>
<p style="margin-left: 30px"><canvas id="myCanvas" width="500" height="500" style="border:3px double #996633;"></p>
<p style="margin-left: 30px"></canvas></p>
<p style="margin-left: 30px"><script type="text/javascript"></p>
<p style="margin-left: 30px"> var canvas = document.getElementById('myCanvas');</p>
<p style="margin-left: 30px"> var ctx = canvas.getContext('2d');</p>
<p style="margin-left: 30px"> var color=['#00FFFF','#00FF00'];</p>
<p style="margin-left: 30px"> var L=50;</p>
<p style="margin-left: 30px"> for (k=0;k<10;k++)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> y=k*L;</p>
<p style="margin-left: 30px"> x0=0;</p>
<p style="margin-left: 30px"> for (i=0;i<10;i++)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> x=x0+i*L;</p>
<p style="margin-left: 30px"> ctx.strokeStyle="black";</p>
<p style="margin-left: 30px"> ctx.strokeRect(x,y,L,L);</p>
<p style="margin-left: 30px"> ctx.fillStyle = color[(k+i)%2];</p>
<p style="margin-left: 30px"> ctx.fillRect(x,y,L,L);</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"></script></p>
<p style="margin-left: 30px"></body></p>
<p style="margin-left: 30px"></html></p>
<p> 在浏览器中打开包含这段HTML代码的html文件,可以看到在浏览器窗口中绘制出如图1所示的正方形平面镶嵌图案。</p>
<p align="center"><img src="https://img2020.cnblogs.com/blog/1485495/202007/1485495-20200714080532719-36343636.png"></p>
<p align="center">图1 正方形平面镶嵌图案(一)</p>
<p> 将上述程序中的语句: x0=0; 改写为:</p>
<p style="margin-left: 30px"> if (k%2==0) x0=0;</p>
<p style="margin-left: 30px"> else x0=-L/2;</p>
<p> 并将填充颜色改为单色填充,例如,ctx.fillStyle = "green";,则绘制出如图2所示的正方形平面镶嵌图案。</p>
<p align="center"><img src="https://img2020.cnblogs.com/blog/1485495/202007/1485495-20200714080557960-530950267.png"></p>
<p align="center">图2 正方形平面镶嵌图案(二)</p>
<p style="margin-left: 30px">(2)用正三角形平铺。</p>
<p style="margin-left: 30px">用正三角形进行平面镶嵌,编写如下的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"></head></p>
<p style="margin-left: 30px"><body></p>
<p style="margin-left: 30px"><canvas id="myCanvas" width="500" height="500" style="border:3px double #996633;"></p>
<p style="margin-left: 30px"></canvas></p>
<p style="margin-left: 30px"><script type="text/javascript"></p>
<p style="margin-left: 30px"> var canvas = document.getElementById('myCanvas');</p>
<p style="margin-left: 30px"> var ctx = canvas.getContext('2d');</p>
<p style="margin-left: 30px"> var sqrt3=Math.sqrt(3);</p>
<p style="margin-left: 30px"> var color=['#00FFFF','#00FF00'];</p>
<p style="margin-left: 30px"> var L=50;</p>
<p style="margin-left: 30px"> for (k=0;k<13;k++)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> if (k%2==0)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> x0=-L;</p>
<p style="margin-left: 30px"> } </p>
<p style="margin-left: 30px"> else</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> x0=-L/2;</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> y=k*sqrt3*L/2;</p>
<p style="margin-left: 30px"> for (i=0;i<15;i++)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> x=x0+i*L;</p>
<p style="margin-left: 30px"> ctx.strokeStyle="black";</p>
<p style="margin-left: 30px"> ctx.beginPath();</p>
<p style="margin-left: 30px"> ctx.moveTo(x,y);</p>
<p style="margin-left: 30px"> ctx.lineTo(x+L/2,y-sqrt3/2*L);</p>
<p style="margin-left: 30px"> ctx.lineTo(x+L,y);</p>
<p style="margin-left: 30px"> ctx.closePath();</p>
<p style="margin-left: 30px"> ctx.stroke();</p>
<p style="margin-left: 30px"> ctx.fillStyle=color;</p>
<p style="margin-left: 30px"> ctx.fill();</p>
<p style="margin-left: 30px"> ctx.beginPath();</p>
<p style="margin-left: 30px"> ctx.moveTo(x+L,y);</p>
<p style="margin-left: 30px"> ctx.lineTo(x+L/2,y-sqrt3/2*L);</p>
<p style="margin-left: 30px"> ctx.lineTo(x+3*L/2,y-sqrt3/2*L);</p>
<p style="margin-left: 30px"> ctx.closePath();</p>
<p style="margin-left: 30px"> ctx.fillStyle = color;</p>
<p style="margin-left: 30px"> ctx.stroke();</p>
<p style="margin-left: 30px"> ctx.fill();</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"></script></p>
<p style="margin-left: 30px"></body></p>
<p style="margin-left: 30px"></html></p>
<p> 在浏览器中打开包含这段HTML代码的html文件,可以看到在浏览器窗口中绘制出如图3所示的正三角形平面镶嵌图案。</p>
<p align="center"> <img src="https://img2020.cnblogs.com/blog/1485495/202007/1485495-20200714080626207-955038393.png"></p>
<p align="center">图3 正三角形平面镶嵌图案</p>
<p style="margin-left: 30px">(3)用正六边形平铺。</p>
<p style="margin-left: 30px">用正六边形进行平面镶嵌,编写如下的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"></head></p>
<p style="margin-left: 30px"><body></p>
<p style="margin-left: 30px"><canvas id="myCanvas" width="500" height="500" style="border:3px double #996633;"></p>
<p style="margin-left: 30px"></canvas></p>
<p style="margin-left: 30px"><script type="text/javascript"></p>
<p style="margin-left: 30px"> var canvas = document.getElementById('myCanvas');</p>
<p style="margin-left: 30px"> var ctx = canvas.getContext('2d');</p>
<p style="margin-left: 30px"> var sqrt3=Math.sqrt(3);</p>
<p style="margin-left: 30px"> var color=['#00FFFF','#00FF00','#FFFF00'];</p>
<p style="margin-left: 30px"> function drawHexagon(x,y,L,c)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> ctx.beginPath();</p>
<p style="margin-left: 30px"> ctx.moveTo(x-L/2,y-sqrt3/2*L);</p>
<p style="margin-left: 30px"> ctx.lineTo(x+L/2,y-sqrt3/2*L);</p>
<p style="margin-left: 30px"> ctx.lineTo(x+L,y);</p>
<p style="margin-left: 30px"> ctx.lineTo(x+L/2,y+sqrt3/2*L);</p>
<p style="margin-left: 30px"> ctx.lineTo(x-L/2,y+sqrt3/2*L);</p>
<p style="margin-left: 30px"> ctx.lineTo(x-L,y);</p>
<p style="margin-left: 30px"> ctx.closePath();</p>
<p style="margin-left: 30px"> ctx.fillStyle = c;</p>
<p style="margin-left: 30px"> ctx.fill();</p>
<p style="margin-left: 30px"> ctx.strokeStyle="black";</p>
<p style="margin-left: 30px"> ctx.stroke();</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> var L=45;</p>
<p style="margin-left: 30px"> for (k=0;k<14;k++)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> if (k%2==0)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> x0=L;</p>
<p style="margin-left: 30px"> } </p>
<p style="margin-left: 30px"> else</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> x0=-L/2;</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> y=k*sqrt3*L/2;</p>
<p style="margin-left: 30px"> for (i=0;i<5;i++)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> x=x0+i*3*L;</p>
<p style="margin-left: 30px"> drawHexagon(x,y,L,color);</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"></script></p>
<p style="margin-left: 30px"></body></p>
<p style="margin-left: 30px"></html></p>
<p> 在浏览器中打开包含这段HTML代码的html文件,可以看到在浏览器窗口中绘制出如图4所示的正六边形平面镶嵌图案。</p>
<p align="center"><img src="https://img2020.cnblogs.com/blog/1485495/202007/1485495-20200714080648383-517656085.png"></p>
<p align="center">图4 正六边形平面镶嵌图案</p>
<h3>2.用几种多边形实现的平面镶嵌图案</h3>
<p> 还可以用一种以上的多边形来实现的平面镶嵌。</p>
<p> (1)正三角形和正方形组合平面镶嵌。</p>
<p> 可以使用正三角形与正方形,通过组合后重复排列的方式完成镶嵌。编写如下的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"></head></p>
<p style="margin-left: 30px"><body></p>
<p style="margin-left: 30px"><canvas id="myCanvas" width="500" height="500" style="border:3px double #996633;"></p>
<p style="margin-left: 30px"></canvas></p>
<p style="margin-left: 30px"><script type="text/javascript"></p>
<p style="margin-left: 30px"> var canvas = document.getElementById('myCanvas');</p>
<p style="margin-left: 30px"> var ctx = canvas.getContext('2d');</p>
<p style="margin-left: 30px"> var sqrt3=Math.sqrt(3);</p>
<p style="margin-left: 30px"> var color=['#00FFFF','#00FF00'];</p>
<p style="margin-left: 30px"> var L=50;</p>
<p style="margin-left: 30px"> var y=0;</p>
<p style="margin-left: 30px"> for (k=0;k<13;k++)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> if (k%2==0)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> x0=-L;</p>
<p style="margin-left: 30px"> y=y+sqrt3*L/2;</p>
<p style="margin-left: 30px"> for (i=0;i<12;i++)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> x=x0+i*L;</p>
<p style="margin-left: 30px"> ctx.strokeStyle="black";</p>
<p style="margin-left: 30px"> ctx.beginPath();</p>
<p style="margin-left: 30px"> ctx.moveTo(x,y);</p>
<p style="margin-left: 30px"> ctx.lineTo(x+L/2,y-sqrt3/2*L);</p>
<p style="margin-left: 30px"> ctx.lineTo(x+L,y);</p>
<p style="margin-left: 30px"> ctx.closePath();</p>
<p style="margin-left: 30px"> ctx.stroke();</p>
<p style="margin-left: 30px"> ctx.fillStyle=color;</p>
<p style="margin-left: 30px"> ctx.fill();</p>
<p style="margin-left: 30px"> ctx.beginPath();</p>
<p style="margin-left: 30px"> ctx.moveTo(x+L,y);</p>
<p style="margin-left: 30px"> ctx.lineTo(x+L/2,y-sqrt3/2*L);</p>
<p style="margin-left: 30px"> ctx.lineTo(x+3*L/2,y-sqrt3/2*L);</p>
<p style="margin-left: 30px"> ctx.closePath();</p>
<p style="margin-left: 30px"> ctx.fillStyle = color;</p>
<p style="margin-left: 30px"> ctx.stroke();</p>
<p style="margin-left: 30px"> ctx.fill();</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> else</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> x0=0;</p>
<p style="margin-left: 30px"> y=y+L;</p>
<p style="margin-left: 30px"> for (i=0;i<6;i++)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> x=x0+2*i*L;</p>
<p style="margin-left: 30px"> ctx.strokeStyle="black";</p>
<p style="margin-left: 30px"> ctx.strokeRect(x,y-L,L,L);</p>
<p style="margin-left: 30px"> ctx.fillStyle=color;</p>
<p style="margin-left: 30px"> ctx.fillRect(x,y-L,L,L);</p>
<p style="margin-left: 30px"> ctx.strokeRect(x+L,y-L,L,L);</p>
<p style="margin-left: 30px"> ctx.fillRect(x+L,y-L,L,L);</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"></script></p>
<p style="margin-left: 30px"></body></p>
<p style="margin-left: 30px"></html></p>
<p> 在浏览器中打开包含这段HTML代码的html文件,可以看到在浏览器窗口中绘制出如图5所示的正三角形和正方形组合平面镶嵌图案。</p>
<p align="center"> <img src="https://img2020.cnblogs.com/blog/1485495/202007/1485495-20200714080730193-547872586.png"></p>
<p align="center">图5 正三角形和正方形组合平面镶嵌图案</p>
<p> (2)正六边形与正三角形组合平面镶嵌。</p>
<p> 可以使用正六边形与正三角形,通过组合后重复排列的方式完成镶嵌。编写如下的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"></head></p>
<p style="margin-left: 30px"><body></p>
<p style="margin-left: 30px"><canvas id="myCanvas" width="500" height="500" style="border:3px double #996633;"></p>
<p style="margin-left: 30px"></canvas></p>
<p style="margin-left: 30px"><script type="text/javascript"></p>
<p style="margin-left: 30px"> var canvas = document.getElementById('myCanvas');</p>
<p style="margin-left: 30px"> var ctx = canvas.getContext('2d');</p>
<p style="margin-left: 30px"> var sqrt3=Math.sqrt(3);</p>
<p style="margin-left: 30px"> var color=['#00FFFF','#00FF00'];</p>
<p style="margin-left: 30px"> function drawHexagon(x,y,L,c)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> ctx.beginPath();</p>
<p style="margin-left: 30px"> ctx.moveTo(x-L/2,y-sqrt3/2*L);</p>
<p style="margin-left: 30px"> ctx.lineTo(x+L/2,y-sqrt3/2*L);</p>
<p style="margin-left: 30px"> ctx.lineTo(x+L,y);</p>
<p style="margin-left: 30px"> ctx.lineTo(x+L/2,y+sqrt3/2*L);</p>
<p style="margin-left: 30px"> ctx.lineTo(x-L/2,y+sqrt3/2*L);</p>
<p style="margin-left: 30px"> ctx.lineTo(x-L,y);</p>
<p style="margin-left: 30px"> ctx.closePath();</p>
<p style="margin-left: 30px"> ctx.fillStyle = c;</p>
<p style="margin-left: 30px"> ctx.fill();</p>
<p style="margin-left: 30px"> ctx.strokeStyle="black";</p>
<p style="margin-left: 30px"> ctx.stroke();</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> ctx.fillStyle="#FFFF00";</p>
<p style="margin-left: 30px"> ctx.fillRect(0,0,canvas.width,canvas.height);</p>
<p style="margin-left: 30px"> var L=45;</p>
<p style="margin-left: 30px"> for (k=0;k<7;k++)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> if (k%2==0)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> x0=L;</p>
<p style="margin-left: 30px"> } </p>
<p style="margin-left: 30px"> else</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> x0=0;</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> y=k*sqrt3*L;</p>
<p style="margin-left: 30px"> for (i=0;i<7;i++)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> x=x0+i*2*L;</p>
<p style="margin-left: 30px"> drawHexagon(x,y,L,color);</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"></script></p>
<p style="margin-left: 30px"></body></p>
<p style="margin-left: 30px"></html></p>
<p> 在浏览器中打开包含这段HTML代码的html文件,可以看到在浏览器窗口中绘制出如图6所示的正六边形与正三角形组合平面镶嵌图案。</p>
<p align="center"><img src="https://img2020.cnblogs.com/blog/1485495/202007/1485495-20200714080807569-1489807003.png"></p>
<p align="center">图6 正六边形与正三角形组合平面镶嵌图案</p>
<p> (3)正八边形组合正方形平面镶嵌。</p>
<p> 可以使用正八边形与正方形,通过组合后重复排列的方式完成镶嵌。编写如下的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"></head></p>
<p style="margin-left: 30px"><body></p>
<p style="margin-left: 30px"><canvas id="myCanvas" width="500" height="500" style="border:3px double #996633;"></p>
<p style="margin-left: 30px"></canvas></p>
<p style="margin-left: 30px"><script type="text/javascript"></p>
<p style="margin-left: 30px"> var canvas = document.getElementById('myCanvas');</p>
<p style="margin-left: 30px"> var ctx = canvas.getContext('2d');</p>
<p style="margin-left: 30px"> var sqrt2=Math.sqrt(2);</p>
<p style="margin-left: 30px"> var color=['#00FFFF','#00FF00'];</p>
<p style="margin-left: 30px"> function drawOctagon(x,y,L,c)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> ctx.beginPath();</p>
<p style="margin-left: 30px"> ctx.moveTo(x-L/2-sqrt2*L/2,y-L/2);</p>
<p style="margin-left: 30px"> ctx.lineTo(x-L/2-sqrt2*L/2,y+L/2);</p>
<p style="margin-left: 30px"> ctx.lineTo(x-L/2,y+L/2+sqrt2*L/2);</p>
<p style="margin-left: 30px"> ctx.lineTo(x+L/2,y+L/2+sqrt2*L/2);</p>
<p style="margin-left: 30px"> ctx.lineTo(x+L/2+sqrt2*L/2,y+L/2);</p>
<p style="margin-left: 30px"> ctx.lineTo(x+L/2+sqrt2*L/2,y-L/2);</p>
<p style="margin-left: 30px"> ctx.lineTo(x+L/2,y-L/2-sqrt2*L/2);</p>
<p style="margin-left: 30px"> ctx.lineTo(x-L/2,y-L/2-sqrt2*L/2);</p>
<p style="margin-left: 30px"> ctx.closePath();</p>
<p style="margin-left: 30px"> ctx.fillStyle = c;</p>
<p style="margin-left: 30px"> ctx.fill();</p>
<p style="margin-left: 30px"> ctx.strokeStyle="black";</p>
<p style="margin-left: 30px"> ctx.stroke();</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> ctx.fillStyle="#FFFF00";</p>
<p style="margin-left: 30px"> ctx.fillRect(0,0,canvas.width,canvas.height);</p>
<p style="margin-left: 30px"> var L=30;</p>
<p style="margin-left: 30px"> var y0=(sqrt2+1)*L/2;</p>
<p style="margin-left: 30px"> for (k=0;k<11;k++)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> if (k%2==0)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> x0=(sqrt2+1)*L/2;</p>
<p style="margin-left: 30px"> } </p>
<p style="margin-left: 30px"> else</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> x0=-L/2;</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> y=y0+(k-1)*(sqrt2+2)*L/2;</p>
<p style="margin-left: 30px"> for (i=0;i<7;i++)</p>
<p style="margin-left: 30px"> {</p>
<p style="margin-left: 30px"> x=x0+i*(2+sqrt2)*L;</p>
<p style="margin-left: 30px"> drawOctagon(x,y,L,color);</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"> }</p>
<p style="margin-left: 30px"></script></p>
<p style="margin-left: 30px"></body></p>
<p style="margin-left: 30px"></html></p>
<p> 在浏览器中打开包含这段HTML代码的html文件,可以看到在浏览器窗口中绘制出如图7所示的正八边形组合正方形平面镶嵌图案。</p>
<p align="center"> <img src="https://img2020.cnblogs.com/blog/1485495/202007/1485495-20200714080842545-628732306.png"></p>
<p align="center">图7 正八边形组合正方形平面镶嵌图案</p><br><br>
来源:https://www.cnblogs.com/cs-whut/p/13297080.html
頁:
[1]