如何用JavaScript编写类似雪花般的Hexaflake分形图案的代码?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1044个文字,预计阅读时间需要5分钟。
编写如下函数绘制六边形:
javascriptfunction drawHexagon(x, y, L) { ctx.beginPath(); ctx.moveTo(x - Math.sqrt(3)/2 * L, y - L/2); ctx.lineTo(x - Math.sqrt(3)/2 * L, y + L/2); ctx.lineTo(x, y + L); ctx.lineTo(x + Math.sqrt(3)/2 * L, y + L/2); ctx.lineTo(x + Math.sqrt(3)/2 * L, y - L/2); ctx.lineTo(x, y - L); ctx.closePath();}
编写如下的函数:
function drawHexagon(x,y,L) { ctx.beginPath(); ctx.moveTo(x-sqrt3/2*L,y-L/2); ctx.lineTo(x-sqrt3/2*L,y+L/2); ctx.lineTo(x,y+L); ctx.lineTo(x+sqrt3/2*L,y+L/2); ctx.lineTo(x+sqrt3/2*L,y-L/2); ctx.lineTo(x,y-L); ctx.closePath(); ctx.fillStyle = "#00FFFF"; ctx.fill(); }
函数中sqrt3的值为Math.sqrt(3)。该函数的功能是:以坐标(x,y)为中心点,绘制一个边长为L的正六边形并进行填充,如下图所示。
本文共计1044个文字,预计阅读时间需要5分钟。
编写如下函数绘制六边形:
javascriptfunction drawHexagon(x, y, L) { ctx.beginPath(); ctx.moveTo(x - Math.sqrt(3)/2 * L, y - L/2); ctx.lineTo(x - Math.sqrt(3)/2 * L, y + L/2); ctx.lineTo(x, y + L); ctx.lineTo(x + Math.sqrt(3)/2 * L, y + L/2); ctx.lineTo(x + Math.sqrt(3)/2 * L, y - L/2); ctx.lineTo(x, y - L); ctx.closePath();}
编写如下的函数:
function drawHexagon(x,y,L) { ctx.beginPath(); ctx.moveTo(x-sqrt3/2*L,y-L/2); ctx.lineTo(x-sqrt3/2*L,y+L/2); ctx.lineTo(x,y+L); ctx.lineTo(x+sqrt3/2*L,y+L/2); ctx.lineTo(x+sqrt3/2*L,y-L/2); ctx.lineTo(x,y-L); ctx.closePath(); ctx.fillStyle = "#00FFFF"; ctx.fill(); }
函数中sqrt3的值为Math.sqrt(3)。该函数的功能是:以坐标(x,y)为中心点,绘制一个边长为L的正六边形并进行填充,如下图所示。

