finished sweepline
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user