My current code to detect circle circle intersection looks like this:
private List<Point> intersect(Circle c1, Circle c2)
{
List<Point> rLp = new List<Point>();
double d = new Point(c1.p.X - c2.p.X, c1.p.Y - c2.p.Y).dist();
if (d > (c1.radius + c2.radius))
return rLp;
else if (d == 0 && c1.radius == c2.radius)
return rLp;
else if ((d + Math.Min(c1.radius, c2.radius)) < Math.Max(c1.radius, c2.radius))
return rLp;
else
{
double a = (c1.radius * c1.radius - c2.radius * c2.radius + d * d) / (2 * d);
double h = Math.Sqrt(c1.radius * c1.radius - a * a);
Point p2 = new Point(Convert.ToInt32(c1.p.X + (a * (c2.p.X - c1.p.X)) / d), Convert.ToInt32(c1.p.Y + (a * c2.p.Y - c1.p.Y) / d));
Point i1 = new Point(Convert.ToInt32(p2.X + (h * (c2.p.Y - c1.p.Y)) / d), Convert.ToInt32(p2.Y - (h * (c2.p.X - c1.p.X)) / d));
Point i2 = new Point(Convert.ToInt32(p2.X - (h * (c2.p.Y - c1.p.Y)) / d), Convert.ToInt32(p2.Y + (h * (c2.p.X - c1.p.X)) / d));
if (d == (c1.radius + c2.radius))
rLp.Add(i1);
else
{
rLp.Add(i1);
rLp.Add(i2);
}
return rLp;
}
}
The strange thing is:
the intersection is detected correctly but the y-coordinate is wrong. Any idea why that is?