1
0

finished sweepline

This commit is contained in:
2019-05-15 16:28:26 +02:00
parent 57314a0a94
commit 6e1f8bbf63
7 changed files with 91 additions and 71 deletions

View File

@@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;
using Priority_Queue;
using Medallion.Collections;
namespace TMI_practicum
{
@@ -8,58 +8,73 @@ namespace TMI_practicum
{
public static IEnumerable<Intersection> Solve(IList<Circle> circles)
{
// fuck deze priority queue piece of shit.... >:(
SimplePriorityQueue<Event, double> events = new SimplePriorityQueue<Event, double>();
Stack<Intersection> intersections = new Stack<Intersection>();
var segments = new List<Segment>();
var events = new PriorityQueue<Event>();
var intersections = new List<Intersection>();
var active = new List<Circle>();
foreach (var circle in circles)
{
events.Enqueue(new Event(Event.EventType.Start, circle.X - circle.R), circle.X - circle.R);
events.Enqueue(new Event(Event.EventType.End, circle.X + circle.R), circle.X + circle.R);
events.Enqueue(new Event(Event.EventType.Start, circle.X - circle.R, circle));
events.Enqueue(new Event(Event.EventType.End, circle.X + circle.R, circle));
}
while (events.Count != 0)
{
Event e = events.Dequeue();
switch (e.Type)
{
case Event.EventType.Start:
active.Add(e.Circle);
CheckIntersections(active, e.Circle, intersections);
break;
case Event.EventType.End:
active.Remove(e.Circle);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
return null;
return intersections;
}
private static void CheckIntersections(IEnumerable<Circle> active, Circle circle, List<Intersection> intersections)
{
foreach (var otherCircle in active)
{
if (circle.Equals(otherCircle)) continue;
var intersects = circle.FindIntersections(otherCircle);
if (intersects == null) continue;
intersections.AddRange(intersects);
}
}
private struct Event
private struct Event : IComparable<Event>
{
public EventType Type { get; }
public Circle Circle { get; }
public double X { get; }
public enum EventType
{
Start,
End,
Intersect
End
}
public Event(EventType type, double xCoordinate)
public Event(EventType type, double x, Circle circle)
{
Type = type;
X = xCoordinate;
}
}
private struct Segment
{
public SegmentType Type { get; }
public Circle Circle { get; }
public enum SegmentType
{
Top,
Bottom
}
public Segment(Circle circle, SegmentType type)
{
X = x;
Circle = circle;
Type = type;
}
public int CompareTo(Event other)
{
return X.CompareTo(other.X);
}
}
}