[英]Determine all the points on a circle #Javascript
I want draw something like this in javascript: 我想在javascript中画出这样的内容:
Example: 例:
The bigger circle: r = 5
大圈:
r = 5
What do I want to do? 我要怎么办 to position circles by forming a circle
通过形成圆来定位圆
My questions are (I would like to know): 我的问题是(我想知道):
How much points can I draw on a circle if I know the ray, 如果我知道射线,我可以在圆上画多少点,
the distance between each circle and the radius of each circle? 每个圆之间的距离和每个圆的半径?
How can I find the position of each circle (to draw automatically)? 如何找到每个圆的位置(自动绘制)?
Like the question 1 but the circles do not have the same radius . 像问题1一样,但是圆的半径不同 。
Thank you! 谢谢!
Using HTML Canvas and a bit of trigonometry Create a reusable circles()
function and pass the desired arguments for Number of circles, Size, Radius distance from center, Color: 使用HTML Canvas和一些三角函数创建一个可重用的
circles()
函数,并为圆数,大小,距中心的半径距离,颜色传递所需的参数:
const ctx = document.getElementById('canvas').getContext('2d'); const cvsSize = 400; const deg2rad = d => d * (Math.PI / 180); ctx.canvas.width = cvsSize; ctx.canvas.height = cvsSize; function circles(tot, rad, dist, color) { const deg = 360 / tot; // Subdivision in degrees let d = 0; // Start at degree (0=east) for (let i = 0; i < tot; i++) { const x = dist * Math.cos(deg2rad(d)) + (cvsSize / 2); const y = dist * Math.sin(deg2rad(d)) + (cvsSize / 2); ctx.beginPath(); ctx.arc(x, y, rad, 0, Math.PI * 2, false); ctx.fillStyle = color; ctx.fill(); ctx.closePath(); d += deg; } } // Total circles, Radius size, Distance from center, Color circles(3, 5, 10, '#f0b'); circles(10, 8, 50, '#0bf'); circles(17, 10, 90, '#bf0'); circles(21, 15, 140, '#b0f');
<canvas id="canvas"></canvas>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.