# line circle

This topic is 3976 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

What would a function in C++ to pick the two points on a circle that a line intersects with look like? I've had some trouble converting this from maths speak into C++. I've got something like this: bool line_vs_circle(vec2 & contact_point, circle * c, line * l); The function needs to return true if a collision occured and false if not, and the variable contact_point needs to be the closest point of contact, if there was any collision. Can anybody show me some psudocode to do this? Thanks.

##### Share on other sites
#define FLT_EPSILON ( 0.000001f )//-----------------------------------------------------------------------------// Input : circle origin and radius, line start and end...// Output: circle hit point, parameter t at line segment // Returns : true if collision occured, false if not...//-----------------------------------------------------------------------------static bool SegmentCircleIntersect( Vector &origin, float radius, Vector vSrc, Vector vEnd, Vector &vHit, float &t ){	t = 1.0f;		Vector delta = vEnd - vSrc;	Vector dist = vSrc - origin;        // LenSqr = dist.x^2 * dist.y^2	if ( dist.LenSqr() <= square(radius+1.0f) )	{	     return false; // line seg start point is inside a circle	}	//Compute A, B and C coefficients	float a = Dot(delta, delta); // dotproduct	float b = 2.0f * Dot( delta, dist );	float c = Dot(dist, dist) - square(radius);	//Find discriminant	float disc = b * b - 4.0f * a * c;	// if discriminant is negative there are no real roots, so return 	// false as line seg misses sphere	if ( fabsf(disc) < FLT_EPSILON )		return false;	// compute q as described above	float distSqrt = sqrtf(disc);	float q;	if ( fabsf(b) < FLT_EPSILON )		q = (-b - distSqrt)/2.0f;	else		q = (-b + distSqrt)/2.0f;	// compute t0 and t1	float t0 = q / a;	float t1 = c / q;	// make sure t0 is smaller than t1	if (t0 > t1)	{		// if t0 is bigger than t1 swap them around		swap(t0,t1);	}	// if t1 is less than zero, the object is in the line negative direction	// and consequently the line misses the sphere	if (t1 < 0.0f)		return false;	// if t0 is less than zero, the intersection point is at t1	if (t0 < 0){ t = t1; }	// else the intersection point is at t0	else { t = t0; }		vHit = vSrc + delta * t;	return true;}

##### Share on other sites

So, vHit is the point where the intersection is? What does t represent?

##### Share on other sites
vHit is a intersection point.
t represents a parameter for line segment.

if vSrc is (0,0) and vEnd is (5,10) so then, if you receive t from funtion and then see that t is 0.5, so it mean that intersection point is lying in center of line segment. t computes in range [0,1]

##### Share on other sites
Thanks alot, I'll try this out first thing tomorrow.

1. 1
Rutin
49
2. 2
3. 3
4. 4
5. 5

• 11
• 16
• 9
• 10
• 13
• ### Forum Statistics

• Total Topics
633003
• Total Posts
3009849
• ### Who's Online (See full list)

There are no registered users currently online

×