private void CircleLineIntersection(
double x1, double y1,
double x2, double y2,
double x3, double y3, double r, ref double[] p)
{
// x1,y1 P1 coordinates [point of line]
// x2,y2 P2 coordinates [point of line]
// x3,y3, r P3 coordinates(circle center) and radius [circle]
// p[1],p[2]; p[3],p[4] intersection coordinates
//
// This function returns a pointer array which first index indicates
// the number of intersection points, followed by coordinate pairs.
//double x , y ;
double a, b, c, mu, i;
a = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
b = 2 * ((x2 - x1) * (x1 - x3) + (y2 - y1) * (y1 - y3));
c = x3 * x3 + y3 * y3 + x1 * x1 + y1 * y1 - 2 * (x3 * x1 + y3 * y1) - r * r;
i = b * b - 4 * a * c;
if (i < 0.0)
{
// no intersection
p[0] = 0.0;
}
else if (Math.Abs(i - 0.0) < EPS)
{
// one intersection
p[0] = 1.0;
mu = -b / (2 * a);
p[1] = x1 + mu * (x2 - x1);
p[2] = y1 + mu * (y2 - y1);
}
else if (i > 0.0 && !(Math.Abs(a - 0.0) < EPS))
{
// two intersections
p[0] = 2.0;
// first intersection
mu = (-b + Math.Sqrt(i)) / (2 * a);
p[1] = x1 + mu * (x2 - x1);
p[2] = y1 + mu * (y2 - y1);
// second intersection
mu = (-b - Math.Sqrt(i)) / (2 * a);
p[3] = x1 + mu * (x2 - x1);
p[4] = y1 + mu * (y2 - y1);
}
else
{
p[0] = 0.0;
}
}