[英]How to draw circle at random position in between of two circles?
I have two circles. 我有两个圈子。 one is with 150 radius and other one is 350 radius.
一种半径为150,另一种半径为350。 I want to place number of circles in between space of 150 - 350 radius.
我想在150-350半径的空间之间放置圆数。 It shouldn't be over-placed(overwrite) on other circle which is already placed.
它不应该在已经放置的其他圆上过度放置(覆盖)。 It should be placed randomly on every refresh.
应该在每次刷新时随机放置它。 Can some one please give me idea/logic of how to do this in canvas.
可以请一个人给我关于如何在画布上执行此操作的想法/逻辑。 Javascript.
的JavaScript。
[EDIT] [编辑]
As for drawing the actual images, that's up to you, but here's how you place the circles: 至于绘制实际图像,这取决于您,但是这是您放置圆圈的方式:
First, pick a Θ, at random, between 0° and 360°. 首先,随机选择一个介于0°和360°之间的Θ。
Then pick a radius r1
for your little circle, between 0 and 50 (half of the 100-pixel width of the "track"). 然后为您的小圆圈选择半径
r1
,介于0和50之间(“轨迹”的100像素宽度的一半)。
Then pick a center location r2
, between 150 + r1
and 350 - r1
. 然后在150 +
r1
和350- r1
之间选择一个中心位置r2
。
Now draw a circle radius r1
at the location given in polar coordinates by (Θ, r2
). 现在在由(Θ,
r2
)在极坐标中给定的位置绘制一个圆半径r1
。
A Demo: http://jsfiddle.net/m1erickson/aGq8p/ 演示: http : //jsfiddle.net/m1erickson/aGq8p/
The trick here is to create random circles but also to be sure they don't overlap previously created circles. 这里的技巧是创建随机圆,但也要确保它们不与先前创建的圆重叠。
Here's annotated code: 这是带注释的代码:
function randomCircle(){
// define random radius
var radius=minCircle+Math.random()*(maxCircle-minCircle);
// define random distance between inner/outer rings
var distFromCenter=innerRadius+radius+Math.random()*(outerRadius-innerRadius-radius*2);
// define random angle
var angle=Math.random()*PI2;
// calculate the x,y of the defined circle
var x=cx+distFromCenter*Math.cos(angle);
var y=cy+distFromCenter*Math.sin(angle);
// check this new circle versus all previous random circles for a collision
var hit=false;
for(var i=0;i<randomCircles.length;i++){
var circle=randomCircles[i];
var dx=circle.cx-x;
var dy=circle.cy-y;
var r=circle.radius+radius;
if(dx*dx+dy*dy<=r*r){
hit=true;
}
}
// if no collision occurred, add this new circle to the existing circles array
if(!hit){
var color=randomColor();
randomCircles.push({cx:x,cy:y,radius:radius,color:color});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.