I'm writing a small collision detection library, but the math is taking longer than I planned. I'm currently doing circle with line, triangle and rectangle collision, but the Circle-Line page on MathWorld doesn't make much sense to me (plus, it's for infinite lines which is more work for me). I wonder how to implement a function that allows for the center of the circle to be other than (0, 0) - I can't think of anything with the MW explanation.
Code prefered, but theory will do also.

**0**

# (Finite) Line-Circle Intersection

Started by Pipo DeClown, Mar 03 2005 07:32 AM

7 replies to this topic

Sponsor:

###
#2
Members - Reputation: **494**

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;

}

[Edited by - mike25025 on March 3, 2005 5:49:28 PM]

###
#4
Members - Reputation: **1977**

Posted 03 March 2005 - 11:38 AM

mike25025's code may be correct, but I'm a little confused as to what's happening in the last few lines. Anyway, if you're having trouble with it still, here's an alternate implementation to try:

Theory: Find the parametric projection of the circle center onto the line segment. Use this to find the point on the segment closest to the circle center. Find the squared distance between the closest point and the circle center. Compare to radius squared.

If you want a point of intersection rather than a simple boolean result, different (but equally straightforward) code will be required.

Note: the above code is untested and may not be error-free.

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;

}

Theory: Find the parametric projection of the circle center onto the line segment. Use this to find the point on the segment closest to the circle center. Find the squared distance between the closest point and the circle center. Compare to radius squared.

If you want a point of intersection rather than a simple boolean result, different (but equally straightforward) code will be required.

Note: the above code is untested and may not be error-free.

###
#6
Members - Reputation: **1098**

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)

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.

###
#7
Members - Reputation: **804**

Posted 03 March 2005 - 08:49 PM

Quote:

Original post by Anon MikeQuote:

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.

Yes, that's what I tried to do. But I don't know where to substitude the 0's since they are left out (?) on the MathWorld page.

@Jyk: Thanks, I'm going to look at the theory!

@mike25025: I'm going to replace that code immediately, you'll hear from me!

I'm not back. I'm just visiting.

###
#8
Members - Reputation: **804**

Posted 03 March 2005 - 09:02 PM

MMkay.. The collision works partly now. When the circle is on the line, all is okay, but when the circle is around the starting- and endingpoints, it gets stuck sometimes, while not even touching the line.

I'm using mike25025's code.

[update edit whatever]

YAEAAAAAAH!! I implemented jyk's code and it works like a super charm! I'll work out the theory later when I have time, currently I'm just so happy that the code works!

Thanks for everyone's attempt on helping, all++!

[Edited by - Pipo DeClown on March 4, 2005 3:02:58 AM]

I'm using mike25025's code.

[update edit whatever]

YAEAAAAAAH!! I implemented jyk's code and it works like a super charm! I'll work out the theory later when I have time, currently I'm just so happy that the code works!

Thanks for everyone's attempt on helping, all++!

[Edited by - Pipo DeClown on March 4, 2005 3:02:58 AM]

I'm not back. I'm just visiting.