如何确定点到圆的最短距离
How to determine the shortest distance between a point and a circle
所以,我试图让这两个圆接吻,未填充的圆是在鼠标光标处绘制的圆,填充的圆固定在 canvas 的中心。目前,圆圈不接吻,我不太确定如何解决这个问题。这是代码,非常简单:
void setup() {
size(500, 500);
}
void draw() {
background(255);
float r = 50; //radius of circle at the centre
fill(0);
ellipse(width/2, height/2, r, r); //Target Circle
//what I am assuming should calculate the radius needed to form the
//kissing circle from my mouse cursor:
float d = dist(width/2, height/2, mouseX, mouseY) - r;
noFill();
ellipse(mouseX, mouseY, d, d); //Drawing of circle, desired to kiss the central one.
}
Current Result When Mouse Cursor Is Close To The Centre
Current Result When Mouse Cursor Is Farther From The Centre
注意:在这些图像中,我只是通过根据它们之间的距离改变没有填充的圆的半径来寻找两个接触的圆。
最后,我希望能够拥有一个圆形对象数组,并获得到 space 中最近点的距离。但这与我目前的问题关系不大。
椭圆函数需要椭圆的宽度和高度(直径,而不是半径)。
只需更改:
ellipse(width/2, height/2, r, r);
到
ellipse(width/2, height/2, 2 * r, 2 * r);
和
ellipse(mouseX, mouseY, d, d)
到
ellipse(mouseX, mouseY, 2*d, 2*d)
所以,我试图让这两个圆接吻,未填充的圆是在鼠标光标处绘制的圆,填充的圆固定在 canvas 的中心。目前,圆圈不接吻,我不太确定如何解决这个问题。这是代码,非常简单:
void setup() {
size(500, 500);
}
void draw() {
background(255);
float r = 50; //radius of circle at the centre
fill(0);
ellipse(width/2, height/2, r, r); //Target Circle
//what I am assuming should calculate the radius needed to form the
//kissing circle from my mouse cursor:
float d = dist(width/2, height/2, mouseX, mouseY) - r;
noFill();
ellipse(mouseX, mouseY, d, d); //Drawing of circle, desired to kiss the central one.
}
Current Result When Mouse Cursor Is Close To The Centre
Current Result When Mouse Cursor Is Farther From The Centre
注意:在这些图像中,我只是通过根据它们之间的距离改变没有填充的圆的半径来寻找两个接触的圆。
最后,我希望能够拥有一个圆形对象数组,并获得到 space 中最近点的距离。但这与我目前的问题关系不大。
椭圆函数需要椭圆的宽度和高度(直径,而不是半径)。 只需更改:
ellipse(width/2, height/2, r, r);
到
ellipse(width/2, height/2, 2 * r, 2 * r);
和
ellipse(mouseX, mouseY, d, d)
到
ellipse(mouseX, mouseY, 2*d, 2*d)