1
0

Added simple algorithm

This commit is contained in:
2019-05-13 16:16:15 +02:00
parent 610a2372eb
commit e7b6480e7c
5 changed files with 117 additions and 29 deletions

View File

@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
namespace TMI_practicum
{
public static class SimpleAlgorithm
{
public static IEnumerable<Intersection> Solve(IList<Circle> circles)
{
var intersections = new Stack<Intersection>();
for (int i = 0; i < circles.Count - 1; i++)
{
for (int j = i + 1; j < circles.Count; j++)
{
var c1 = circles[i];
var c2 = circles[j];
CalculateIntersection(intersections, c1, c2);
}
}
return intersections;
}
private static void CalculateIntersection(Stack<Intersection> intersections, Circle c1, Circle c2)
{
double d = c1.Distance(c2);
if (d > c1.R + c2.R || d < Math.Abs(c1.R - c2.R)) return;
if (d == 0.0 && c1.R - c2.R == 0.0) throw new ArgumentException("Circles may not be coincident!");
double a = (c1.R * c1.R - c2.R * c2.R + d * d) / (2.0*d);
double px = c1.X + a * (c2.X - c1.X) / d;
double py = c1.Y + a * (c2.Y - c1.Y) / d;
double htemp = c1.R * c1.R - a * a;
if (htemp == 0)
{
intersections.Push(new Intersection(px, py));
}
else
{
double h = Math.Sqrt(htemp);
intersections.Push(new Intersection(px + h * (c2.Y - c1.Y) / d, py - h * (c2.X - c1.X) / d));
intersections.Push(new Intersection(px - h * (c2.Y - c1.Y) / d, py + h * (c2.X - c1.X) / d));
}
}
}
}