gpt4 book ai didi

javascript - 把小圆圈包成一个大圆圈

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:14:26 24 4
gpt4 key购买 nike

我正在尝试找出一种将较小的圆圈打包成较大的圆圈的方法,但由于某些原因,较小的圆圈没有正确对齐。

我认为位置计算可能遗漏了一些东西。

详情请看代码:

var c_el = document.getElementById("myCanvas");
var ctx = c_el.getContext("2d");

var canvas_width = c_el.clientWidth;
var canvas_height = c_el.clientHeight;
var circle1 = {
r: 50, /// radius
pos: {
x: (canvas_width / 2),
y: (canvas_height / 2)
}
}

var circle2 = {
r: 5,
pos:{}
}

var c2h = circle2.r * 2; /// circle height ////
var c2w = c2h; //// circle width /////

var c1h = circle1.r * 2; /// circle height ////
var c1w = c1h; //// circle width /////

var max_circles2_H = c1h / c2h;

var r = circle1.r;
var d = circle1.r - 2; //// segement distance from center ////

drawCircle( circle1 );
for(var col = 1; col < max_circles2_H; col++){
var d = circle1.r - ( col * c2h); /// distance from center to segment ///
var c = 2 * (Math.sqrt((r*r) - (d * d))); //// circle's chord length
var max_circles2_W = c / c2w;
for(var row = 1; row < max_circles2_W; row++){
circle2.pos.x = (row * c2w) + (circle1.pos.x - circle1.r);
circle2.pos.y = (col * c2h) + (circle1.pos.y - circle1.r);
drawCircle(circle2);
}
}


function drawCircle( circle ){
ctx.beginPath();
ctx.arc(circle.pos.x, circle.pos.y, circle.r, 0, 2 * Math.PI);
ctx.stroke();
}
<canvas id="myCanvas" width="300" height="150">

非常感谢任何帮助。

最佳答案

这很接近,但需要稍微调整一下。您可以看到计算 x_offset 的位置并尝试一些不同的数学来收紧它。

更新将 Math.ceil 添加到 max_circles2_W,它看起来很合适。

var c_el = document.getElementById("myCanvas");
var ctx = c_el.getContext("2d");

var canvas_width = c_el.clientWidth;
var canvas_height = c_el.clientHeight;
var circle1 = {
r: 50, /// radius
pos: {
x: (canvas_width / 2),
y: (canvas_height / 2)
}
}

var circle2 = {
r: 5,
pos:{}
}

var c2h = circle2.r * 2; /// circle height ////
var c2w = c2h; //// circle width /////

var c1h = circle1.r * 2; /// circle height ////
var c1w = c1h; //// circle width /////

var max_circles2_H = c1h / c2h;

var r = circle1.r;
var d = circle1.r - 2; //// segement distance from center ////

drawCircle( circle1 );
for(var col = 1; col < max_circles2_H; col++){
var d = circle1.r - ( col * c2h); /// distance from center to segment ///
var c = 2 * (Math.sqrt((r*r) - (d * d))); //// circle's chord length
var max_circles2_W = Math.ceil(c / c2w);

// CALCULATE OFFSET HERE
var x_offset = Math.floor((c1w - (max_circles2_W * c2w)) / 2);

for(var row = 1; row < max_circles2_W; row++){
circle2.pos.x = (row * c2w) + (circle1.pos.x - circle1.r) + x_offset;
circle2.pos.y = (col * c2h) + (circle1.pos.y - circle1.r);
ctx.fillText(row, circle2.pos.x-3, circle2.pos.y+4);
drawCircle(circle2);
}
}


function drawCircle( circle ){
ctx.beginPath();
ctx.arc(circle.pos.x, circle.pos.y, circle.r, 0, 2 * Math.PI);
ctx.stroke();
}
<canvas id="myCanvas" width="300" height="150">

关于javascript - 把小圆圈包成一个大圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35580816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com