finished sweepline
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace TMI_practicum
|
||||
@@ -7,7 +6,7 @@ namespace TMI_practicum
|
||||
{
|
||||
public static IEnumerable<Intersection> Solve(IList<Circle> circles)
|
||||
{
|
||||
var intersections = new Stack<Intersection>();
|
||||
var intersections = new List<Intersection>();
|
||||
|
||||
for (int i = 0; i < circles.Count - 1; i++)
|
||||
{
|
||||
@@ -15,35 +14,12 @@ namespace TMI_practicum
|
||||
{
|
||||
var c1 = circles[i];
|
||||
var c2 = circles[j];
|
||||
CalculateIntersection(intersections, c1, c2);
|
||||
var intersects = c1.FindIntersections(c2);
|
||||
if (intersects != null) intersections.AddRange(c1.FindIntersections(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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user