Ken Selvia
asked on
How to find coordinates of intersection of a line and a circle
Hello experts,
I have a circle plotted in x,y coordinates. The center is at (14,5) with a radius of 5.
I have a point (*) at (16,6). I need a formula to determine the x,y coordinates where
a line intersects the circle (at ? below) if drawn from the center of the circle through the point *.
y
|
| .
| . ?.
| *
| . @ .
|
| . .
| .
|
+------------------------- x
The coordinates I gave are just an example, I need a formula to solve any center, radius and * point.
BTW: This is not a homework problem. If I had paid more attention in geometry though I would
proabably know how to do this. This is not really a Java problem but I didn't know the
best forum to post it in.
I have a circle plotted in x,y coordinates. The center is at (14,5) with a radius of 5.
I have a point (*) at (16,6). I need a formula to determine the x,y coordinates where
a line intersects the circle (at ? below) if drawn from the center of the circle through the point *.
y
|
| .
| . ?.
| *
| . @ .
|
| . .
| .
|
+-------------------------
The coordinates I gave are just an example, I need a formula to solve any center, radius and * point.
BTW: This is not a homework problem. If I had paid more attention in geometry though I would
proabably know how to do this. This is not really a Java problem but I didn't know the
best forum to post it in.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
the equation is given on that page, which part is giving you problems?
ASKER
The part where I find the equation on the page.
"gives the points of intersection as ..."
http://mathworld.wolfram.com/c1img3452.gif
http://mathworld.wolfram.com/c1img3452.gif
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes I understand now. Thank you both very much.
Something like this?
class Circle
{
...
/**
* Find the intersection of this circle with the line defined by point p1 & p2
public Point Intersect(Point p1, Point p2)
{
int dx=p2a.x-p1.x;
int dy=p2.y-p1.y;
int dr=Math.sqrt(dx*dx+dy*dy);
int D=p1.x*p2.y-p2.x*p1.y;
int discriminant=this.radius*t his.radius *dr*dr-D*D ;
if (discriminant<0)
return null; //No intersection;
else if (discriminant==0)
;//Tangant line there is only ONE intersection
else if (discriminant>0)
;//The line intersects at TWO points
//Compute intersection for ONE point (to compute intersection at OTHER point change + to a -)
int x=(D*dy+Math.sgn(dy)*dx*Ma th.sqrt(di scriminant ))/(dr*dr) ;
int y=(-D*dx+Math.abs(dy)*Math .sqrt(disc riminant)) /(dr*dr);
return new Point(x,y);
}
class Circle
{
...
/**
* Find the intersection of this circle with the line defined by point p1 & p2
public Point Intersect(Point p1, Point p2)
{
int dx=p2a.x-p1.x;
int dy=p2.y-p1.y;
int dr=Math.sqrt(dx*dx+dy*dy);
int D=p1.x*p2.y-p2.x*p1.y;
int discriminant=this.radius*t
if (discriminant<0)
return null; //No intersection;
else if (discriminant==0)
;//Tangant line there is only ONE intersection
else if (discriminant>0)
;//The line intersects at TWO points
//Compute intersection for ONE point (to compute intersection at OTHER point change + to a -)
int x=(D*dy+Math.sgn(dy)*dx*Ma
int y=(-D*dx+Math.abs(dy)*Math
return new Point(x,y);
}
Thanks for accepting
ASKER
Given the data I have: The center point, another point inside the circle, where will a line drawn from the center throug the point cross the circle.