TTL
TTL

Reputation: 23

How to determine the shortest distance between a point and a circle

So, I am trying to make these two circles kiss, the unfilled circle is the one being drawn at the mouse cursor and the filled one is fixed at the centre of the canvas. Currently, the circles do not kiss and I'm not quite sure how to approach fixing this. Here is the code, quite simple:

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

Note: In these images, I am looking for the two circles to be touching only by changing the radius of the one with no fill, based off of the distance between them.

In the end, I want to be able to have an array of circle objects and get the distance to the closest point in the space. But that is less related to my issue at the current time.

Upvotes: 1

Views: 276

Answers (1)

marcelodmmenezes
marcelodmmenezes

Reputation: 221

The ellipse function expects the width and height of the ellipse (diameters, not radii). Just change:

ellipse(width/2, height/2, r, r);

to

ellipse(width/2, height/2, 2 * r, 2 * r);

and

ellipse(mouseX, mouseY, d, d)

to

ellipse(mouseX, mouseY, 2*d, 2*d)

Upvotes: 0

Related Questions