Working on a game, I have to make a circumference and line collision detection.
This is what I get. Hope it help some one :)
First we have the circumference equation:
center is (h,k) and radius r.
pow(x) + pow(y) +Dx + Ey + F = 0
we have:
D=-2h, E=-2k, F=pow(h) + pow(k) + pow(r)
Then we have the line equation:
y = mx + n
where m is the slope and n the ordinate( where the line cut the x ordinate, or in other words, where y = 0).
m = tan(alpha) where alpha is the angle that makes the line with the x ordinate.
if we have one point of the line, we can calculate
= y - (m * x)
.
Supplanting y in the circumference equation:
pow(x) + pow(mx+n) + Dx + E(mx + n) + F = 0
if we do some math here we end having this:
( 1+pow(m) )*pow(x) + (2mn+D+Em)*x + pow(n) + E*n + F = 0
Then we have to remember something about quadratic equations:
a*pow(x) + b*x + c = 0
so from the resulting equation we have:
a = 1+pow(m), b = 2*m*n + D + E*m, c = pow(n) + E*n + F
and to resolve it:
x = ( -b +- sqrt(pow(b) - 4*a*c) ) / (2*a)
and from here we know that:
pow(b) - 4*a*c
must be positive to be real, so:
resuming all here is the function:
private function circumferenceRectInterception(x:Number, y:Number, radius:Number, slope:Number, intercept:Number = 0):Boolean {
var intercepted:Boolean;
var result:Number;
var D:Number, E:Number, F:Number, a:Number, b:Number, c:Number;
D = round(-2 * x,5);
E = round(-2 * y,5);
F = round((x*x) + (y*y) - (radius*radius),5);
a = round(1 + (slope*slope),5);
b = round((2*slope*intercept) + D + (E*slope),5);
c = round((intercept*intercept) + (E*intercept) + F,5);
result = round((b*b) - (4*a*c),5);
intercepted = false;
if (result >= 0) {
intercepted = true;
}
return intercepted;
}
And that is all. I hope it understandable.
And I'm sorry about my bad English.
Any doubt or criticism are welcome.