【发布时间】:2021-11-20 04:39:04
【问题描述】:
我正在尝试使用 HTML5 中的 canvas 元素创建斐波那契螺旋线。
这是我的绘图代码,斐波那契数列中的每个数字都会调用此函数。 Side 是正方形的边长,或当前的斐波那契数。 start 是每个正方形的右下角。
function drawSpiral(side, start) {
ctx.beginPath();
ctx.moveTo(start.x, start.y);
ctx.lineTo(start.x, start.y - side);
ctx.lineTo(start.x - side, start.y - side);
ctx.lineTo(start.x - side, start.y);
ctx.lineTo(start.x, start.y);
ctx.stroke();
ctx.closePath();
ctx.beginPath();
switch (direction) {
case 0:
ctx.arc(start.x, start.y - side, side, Math.PI, 1.5 * Math.PI, true);
break;
case 1:
ctx.arc(
start.x - side,
start.y - side,
side,
1.5 * Math.PI,
2 * Math.PI,
true
);
break;
case 2:
ctx.arc(start.x - side, start.y, side, 2 * Math.PI, Math.PI / 2, true);
break;
case 3:
ctx.arc(start.x, start.y, side, Math.PI / 2, Math.PI, true);
break;
}
ctx.stroke();
ctx.closePath();
}
方向是一个数字,0 到 4,表示下一个正方形的方向,逆时针 - 0 = 右,1 = 上,...
它可以很好地绘制正方形和圆弧,但它也到处都是半圆形,我不知道为什么。
【问题讨论】:
-
对我来说看起来像 3/4 圈,可能是顺时针与逆时针的问题。
标签: javascript html html5-canvas