1
0
This commit is contained in:
2019-05-15 16:46:55 +02:00
parent 6e1f8bbf63
commit de4d08b938
3 changed files with 18 additions and 10 deletions

View File

@@ -23,7 +23,7 @@ namespace TMI_practicum
public IList<Intersection> FindIntersections(Circle c1) public IList<Intersection> FindIntersections(Circle c1)
{ {
var intersections = new List<Intersection>(); IList<Intersection> intersections;
double d = Distance(c1); 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; if (d > R + c1.R || d < Math.Abs(R - c1.R) || d == 0.0 && R - c1.R == 0.0) return null;
@@ -35,13 +35,15 @@ namespace TMI_practicum
double htemp = Math.Round(R * R - a * a, 15); double htemp = Math.Round(R * R - a * a, 15);
if (htemp == 0) if (htemp == 0)
{ {
intersections.Add(new Intersection(px, py)); intersections = new Intersection[1];
intersections[0] = new Intersection(px, py);
} }
else else
{ {
intersections = new Intersection[2];
double h = Math.Round(Math.Sqrt(htemp), 15); double h = Math.Round(Math.Sqrt(htemp), 15);
intersections.Add(new Intersection(px + h * (c1.Y - Y) / d, py - h * (c1.X - X) / d)); intersections[0] = new Intersection(px + h * (c1.Y - Y) / d, py - h * (c1.X - X) / d);
intersections.Add(new Intersection(px - h * (c1.Y - Y) / d, py + h * (c1.X - X) / d)); intersections[1] = new Intersection(px - h * (c1.Y - Y) / d, py + h * (c1.X - X) / d);
} }
return intersections; return intersections;

View File

@@ -6,7 +6,7 @@ namespace TMI_practicum
{ {
public static IEnumerable<Intersection> Solve(IList<Circle> circles) public static IEnumerable<Intersection> Solve(IList<Circle> circles)
{ {
var intersections = new List<Intersection>(); var intersections = new HashSet<Intersection>();
for (int i = 0; i < circles.Count - 1; i++) for (int i = 0; i < circles.Count - 1; i++)
{ {
@@ -15,7 +15,11 @@ namespace TMI_practicum
var c1 = circles[i]; var c1 = circles[i];
var c2 = circles[j]; var c2 = circles[j];
var intersects = c1.FindIntersections(c2); var intersects = c1.FindIntersections(c2);
if (intersects != null) intersections.AddRange(c1.FindIntersections(c2)); if (intersects == null) continue;
foreach (var intersect in intersects)
{
intersections.Add(intersect);
}
} }
} }

View File

@@ -9,7 +9,7 @@ namespace TMI_practicum
public static IEnumerable<Intersection> Solve(IList<Circle> circles) public static IEnumerable<Intersection> Solve(IList<Circle> circles)
{ {
var events = new PriorityQueue<Event>(); var events = new PriorityQueue<Event>();
var intersections = new List<Intersection>(); var intersections = new HashSet<Intersection>();
var active = new List<Circle>(); var active = new List<Circle>();
foreach (var circle in circles) foreach (var circle in circles)
@@ -21,7 +21,6 @@ namespace TMI_practicum
while (events.Count != 0) while (events.Count != 0)
{ {
Event e = events.Dequeue(); Event e = events.Dequeue();
switch (e.Type) switch (e.Type)
{ {
case Event.EventType.Start: case Event.EventType.Start:
@@ -39,7 +38,7 @@ namespace TMI_practicum
return intersections; return intersections;
} }
private static void CheckIntersections(IEnumerable<Circle> active, Circle circle, List<Intersection> intersections) private static void CheckIntersections(IEnumerable<Circle> active, Circle circle, HashSet<Intersection> intersections)
{ {
foreach (var otherCircle in active) foreach (var otherCircle in active)
{ {
@@ -48,7 +47,10 @@ namespace TMI_practicum
var intersects = circle.FindIntersections(otherCircle); var intersects = circle.FindIntersections(otherCircle);
if (intersects == null) continue; if (intersects == null) continue;
intersections.AddRange(intersects); foreach (var intersection in intersects)
{
intersections.Add(intersection);
}
} }
} }