initial implementation sweeplineff
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
using System.Collections;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Medallion.Collections;
|
||||
|
||||
namespace TMI_practicum
|
||||
{
|
||||
@@ -7,7 +9,193 @@ namespace TMI_practicum
|
||||
{
|
||||
public static IEnumerable<Intersection> Solve(IEnumerable<Circle> circles)
|
||||
{
|
||||
return null;
|
||||
var intersections = new Stack<Intersection>();
|
||||
var events = new PriorityQueue<Event>();
|
||||
|
||||
var upperActive = new CircleTree<SegmentKey, Segment>();
|
||||
var bottomActive = new CircleTree<SegmentKey, Segment>();
|
||||
|
||||
|
||||
foreach (var circle in circles)
|
||||
{
|
||||
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();
|
||||
int hash = e.GetHashCode();
|
||||
switch (e.Type)
|
||||
{
|
||||
case Event.EventType.Start:
|
||||
upperActive.Add(new SegmentKey(e.X, e.Circle.Y, Segment.SegmentType.Upper, hash),
|
||||
new Segment(Segment.SegmentType.Upper, e.Circle.Y, e.Circle));
|
||||
bottomActive.Add(new SegmentKey(e.X, e.Circle.Y, Segment.SegmentType.Bottom, hash),
|
||||
new Segment(Segment.SegmentType.Bottom, e.Circle.Y, e.Circle));
|
||||
|
||||
CheckIntersections(upperActive, e, intersections, true);
|
||||
CheckIntersections(bottomActive, e, intersections, false);
|
||||
break;
|
||||
case Event.EventType.End:
|
||||
upperActive.Remove(new SegmentKey(e.X, e.Circle.Y, Segment.SegmentType.Upper, hash));
|
||||
bottomActive.Remove(new SegmentKey(e.X, e.Circle.Y, Segment.SegmentType.Bottom, hash));
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return intersections;
|
||||
}
|
||||
|
||||
private static void CheckIntersections(CircleTree<SegmentKey, Segment> active, Event e, Stack<Intersection> intersections, bool upper)
|
||||
{
|
||||
Segment other;
|
||||
try
|
||||
{
|
||||
other = upper
|
||||
? active.FindSuccessor(new SegmentKey(e.X, e.Circle.Y, Segment.SegmentType.Upper, e.GetHashCode()))
|
||||
: active.FindPredecessor(new SegmentKey(e.X, e.Circle.Y, Segment.SegmentType.Bottom, e.GetHashCode()));
|
||||
}
|
||||
catch (KeyNotFoundException)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var intersects = e.Circle.FindIntersections(other.Circle);
|
||||
if (intersects == null) return;
|
||||
|
||||
foreach (var intersection in intersects)
|
||||
{
|
||||
if (upper)
|
||||
{
|
||||
if (intersection.Y < e.Circle.Y) continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (intersection.Y > e.Circle.Y) continue;
|
||||
}
|
||||
intersections.Push(intersection);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private class CircleTree<TKey, TValue> : SortedDictionary<TKey, TValue>
|
||||
{
|
||||
public TValue FindSuccessor(TKey key)
|
||||
{
|
||||
var other = this.Keys.Where(s => this.Comparer.Compare(s, key) > 0).MinOrDefault();
|
||||
return this[other];
|
||||
}
|
||||
|
||||
public TValue FindPredecessor(TKey key)
|
||||
{
|
||||
var other = this.Keys.Where(s => this.Comparer.Compare(s, key) < 0).MaxOrDefault();
|
||||
return this[other];
|
||||
}
|
||||
}
|
||||
|
||||
private struct Event : IComparable<Event>
|
||||
{
|
||||
public EventType Type { get; }
|
||||
public Circle Circle { get; }
|
||||
public double X { get; }
|
||||
|
||||
public enum EventType
|
||||
{
|
||||
Start,
|
||||
Intersect,
|
||||
End
|
||||
}
|
||||
|
||||
public Event(EventType type, double x, Circle circle)
|
||||
{
|
||||
Type = type;
|
||||
X = x;
|
||||
Circle = circle;
|
||||
}
|
||||
|
||||
public int CompareTo(Event other)
|
||||
{
|
||||
return X.CompareTo(other.X);
|
||||
}
|
||||
}
|
||||
|
||||
private class Segment : IComparable<Segment>
|
||||
{
|
||||
public Segment(SegmentType type, double y, Circle circle)
|
||||
{
|
||||
_type = type;
|
||||
_y = y;
|
||||
Circle = circle;
|
||||
}
|
||||
|
||||
private readonly SegmentType _type;
|
||||
private readonly double _y;
|
||||
|
||||
public Circle Circle { get; }
|
||||
|
||||
public int CompareTo(Segment other)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public enum SegmentType
|
||||
{
|
||||
Bottom,
|
||||
Upper,
|
||||
Null
|
||||
}
|
||||
}
|
||||
|
||||
private struct SegmentKey : IComparable<SegmentKey>
|
||||
{
|
||||
private readonly double _x;
|
||||
private readonly double _y;
|
||||
private readonly Segment.SegmentType _type;
|
||||
private readonly int _identifier;
|
||||
|
||||
public SegmentKey(double x, double y, Segment.SegmentType type, int identifier)
|
||||
{
|
||||
_y = y;
|
||||
_type = type;
|
||||
_identifier = identifier;
|
||||
_x = x;
|
||||
}
|
||||
|
||||
public int CompareTo(SegmentKey other)
|
||||
{
|
||||
if (_identifier.Equals(other._identifier)) return _type.CompareTo(other._type);
|
||||
if (!_y.Equals(other._y)) return _y.CompareTo(other._y);
|
||||
|
||||
return _type.Equals(other._type) ? _x.CompareTo(other._x) : _type.CompareTo(other._type);
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == null || GetType() != obj.GetType()) return false;
|
||||
SegmentKey r = (SegmentKey) obj;
|
||||
|
||||
return _identifier.Equals(r._identifier) && _type.Equals(r._type);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return _identifier.GetHashCode() ^ _type.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
public static class EnumerableExtensions
|
||||
{
|
||||
public static T MinOrDefault<T>(this IEnumerable<T> sequence)
|
||||
{
|
||||
return sequence.Any() ? sequence.Min() : default(T);
|
||||
}
|
||||
public static T MaxOrDefault<T>(this IEnumerable<T> sequence)
|
||||
{
|
||||
return sequence.Any() ? sequence.Max() : default(T);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user