arrays - 如何获取在 Canvas 中构成圆圈的坐标数组?

标签 arrays html html5-canvas coordinates geometry

所以,如果这是我用来在 Canvas 上画圆的代码:

ctx.beginPath();
ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
ctx.lineWidth = 3;
ctx.strokeStyle = "black";
ctx.stroke();

...我怎样才能得到组成这个圆的点的坐标数组,这样我就可以将它们保存在数据库中,稍后使用 context.moveTo() 和 context.lineTo() 方法加载到 Canvas 上把点连起来,画出同一个圆圈?

我想我是在问是否有可能不使用 .arc() 方法而是通过用线连接点来绘制这种圆,如果我只知道我的中心坐标和圆半径(当然还有线条和颜色)。这应该使我能够在循环时将每个点坐标保存在一个数组中。

最佳答案

@Octopus 走在正确的轨道上:

var centerX=100;
var centerY=100;
var radius=40;

// an array to save your points
var points=[];

for(var degree=0;degree<360;degree++){
    var radians = degree * Math.PI/180;
    var x = centerX + radius * Math.cos(radians);
    var y = centerY + radius * Math.sin(radians);
    points.push({x:x,y:y});
}

然后您可以使用点数组中的点对象绘制圆:

ctx.beginPath();
ctx.moveTo(points[0].x,points[0].y);
for(var i=1;i<points.length;i++){
    ctx.lineTo(points[i].x,points[i].y);
}
ctx.closePath();
ctx.fillStyle="skyblue";
ctx.fill();
ctx.strokeStyle="lightgray";
ctx.lineWidth=3;
ctx.stroke()

一个建议,但是......

不是将所有点都保存在数据库中,而是将centerX/Y和radius保存在数据库中。

然后您可以使用相同的数学方法来创建点并绘制圆。

关于arrays - 如何获取在 Canvas 中构成圆圈的坐标数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18342216/

相关文章:

html - 忽略父表列限制的表中表

javascript - 从链接打开页面而不是导航到页面时是否可以隐藏 div?

javascript - 重新加载数据使数据堆栈和重复与.append - Jquery

javascript - 如何在 HTML5 Canvas 上绘制背景图像

java - BufferedImage 数组的 NullPointerException

java - 在 3D Young Tableau 上调用 Maxify?

c - 为什么 C 函数不能返回数组?

algorithm - 绘图几何和数学算法

Javascript:将标志设置为 false 时简单动画不会停止

javascript 将数组与对象数组进行比较