52 lines
1.7 KiB
C#
52 lines
1.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace TMI_practicum
|
|
{
|
|
public class Circle
|
|
{
|
|
public readonly double X;
|
|
public readonly double Y;
|
|
public readonly double R;
|
|
|
|
public Circle(double x, double y, double r)
|
|
{
|
|
X = x;
|
|
Y = y;
|
|
R = r;
|
|
}
|
|
|
|
public double Distance(Circle otherCircle)
|
|
{
|
|
return Math.Round(Math.Sqrt(Math.Pow(X - otherCircle.X, 2) + Math.Pow(Y - otherCircle.Y, 2)), 15);
|
|
}
|
|
|
|
public IEnumerable<Intersection> FindIntersections(Circle c1)
|
|
{
|
|
|
|
IList<Intersection> intersections;
|
|
double d = Distance(c1);
|
|
if (d > R + c1.R || d < Math.Abs(R - c1.R) || (d == 0.0 && R - c1.R == 0.0)) return null;
|
|
|
|
double a = (R * R - c1.R * c1.R + d * d) / (2.0*d);
|
|
double px = X + a * (c1.X - X) / d;
|
|
double py = Y + a * (c1.Y - Y) / d;
|
|
|
|
double htemp = R * R - a * a;
|
|
if (Math.Round(htemp, 15) == 0)
|
|
{
|
|
intersections = new Intersection[1];
|
|
intersections[0] = new Intersection(Math.Round(px, 15), Math.Round(py, 15));
|
|
}
|
|
else
|
|
{
|
|
intersections = new Intersection[2];
|
|
double h = Math.Sqrt(htemp);
|
|
intersections[0] = new Intersection(Math.Round(px + h * (c1.Y - Y) / d, 15), Math.Round(py - h * (c1.X - X) / d, 15));
|
|
intersections[1] = new Intersection(Math.Round(px - h * (c1.Y - Y) / d, 15), Math.Round(py + h * (c1.X - X) / d, 15));
|
|
}
|
|
|
|
return intersections;
|
|
}
|
|
}
|
|
} |