Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
Posted 03 March 2005 - 07:32 AM
Posted 03 March 2005 - 07:49 AM
bool clIntersect(Circle c,Line l)
{
float x0 = c.x;
float y0 = c.y;
float x1 = l.s.x;
float y1 = l.s.y;
float x2 = l.e.x;
float y2 = l.e.y;
float n = fabs((x2-x1)*(y1-y0)-(x1-x0)*(y2-y1));
float d = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
float dist = n/d;
if(dist > c.r)return false;
float d1 = sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1));
if((d1-c.r) > d)return false;
float d2 = sqrt((x0-x2)*(x0-x2)+(y0-y2)*(y0-y2));
if((d2-c.r) > d)return false;
return true;
}
Posted 03 March 2005 - 08:17 AM
Posted 03 March 2005 - 11:38 AM
bool IntersectCircleSegment(
const Vector2& c, // center
float r, // radius
const Vector2& p1, // segment start
const Vector2& p2) // segment end
{
Vector2 dir = p2 - p1;
Vector2 diff = c - p1;
float t = diff.Dot(dir) / dir.Dot(dir);
if (t < 0.0f)
t = 0.0f;
if (t > 1.0f)
t = 1.0f;
Vector2 closest = p1 + t * dir;
Vector2 d = c - closest;
float distsqr = d.Dot(d);
return distsqr <= r * r;
}
Posted 03 March 2005 - 11:50 AM
Quote:
I wonder how to implement a function that allows for the center of the circle to be other than (0, 0)
Posted 03 March 2005 - 08:49 PM
Quote:
Original post by Anon Mike Quote:
I wonder how to implement a function that allows for the center of the circle to be other than (0, 0)
If that's the only holdup then you can just translate all your points before you jump into the algorithm. e.g. if a point on the line is at (Px, Py) and the circle is at (Cx, Cy) then use (Px - Cx, Py - Cy) and (0, 0) instead.
Posted 03 March 2005 - 09:02 PM
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.
GameDev.net™, the GameDev.net logo, and GDNet™ are trademarks of GameDev.net, LLC.