This post is the third installment from my post (How to detect a collision between a circle and a line). In this post I will show how to find the closest point to a point or circle. It is pretty straight forward after you know how to determine the distance between 2 points.

If you want to know the closest point to a circle then just use the center of the circle for the next part. So to measure the distance between 2 points use this code:

float d(pt P, pt Q) { return sqrt(d2(P, Q)); }; // ||AB|| (Distance) float d2(pt P, pt Q) { return sq(Q.x-P.x)+sq(Q.y-P.y); }; // AB*AB (Distance squared)

You would use the ‘d’ method to cycle through your point array to find the closest point. Here is my code that I used to find the closest point to a circle:

int findClosePt(pt[] A, pt B){ // takes in a point array and point to compare int nearest ; float temp; // for (int i=0; i

I hope this helps out anyone out there.