1
0

fuck alles

This commit is contained in:
2019-05-21 17:50:08 +02:00
parent a7b988a977
commit 168262ea7b
2 changed files with 84 additions and 52 deletions

View File

@@ -18,7 +18,7 @@ namespace TMI_practicum
public override bool Equals(object obj)
{
return obj is Intersection intersection && this == intersection;
return obj is Intersection && this == (Intersection) obj;
}
public static bool operator ==(Intersection x, Intersection y)
{

View File

@@ -15,7 +15,6 @@ namespace TMI_practicum
var events = new PriorityQueue<Event>();
var active = new C5.TreeDictionary<SegmentKey, Circle>();
foreach (var circle in circles)
{
events.Enqueue(new Event(Event.EventType.Start, circle.X - circle.R, circle, circle.Y));
@@ -25,49 +24,82 @@ namespace TMI_practicum
while (events.Count != 0)
{
Event e = events.Dequeue();
int hash = e.GetHashCode();
int hash = e.GetIdentifier();
_sweepline = e.X;
switch (e.Type)
{
case Event.EventType.Start:
active.Add(
new SegmentKey(SegmentKey.SegmentType.Upper, hash, e.Circle),
e.Circle);
active.Add(
new SegmentKey(SegmentKey.SegmentType.Bottom, hash, e.Circle),
e.Circle);
CheckIntersections(active, e, intersections, true, events);
CheckIntersections(active, e, intersections, false, events);
var upper = new SegmentKey(SegmentKey.SegmentType.Upper, hash, e.Circle);
var lower = new SegmentKey(SegmentKey.SegmentType.Bottom, hash, e.Circle);
active.Add(upper, e.Circle);
active.Add(lower, e.Circle);
if (active.TrySuccessor(upper, out var other))
CheckIntersections(upper, other, e.Circle, intersections, true, events);
if (active.TryPredecessor(lower, out other))
CheckIntersections(lower, other, e.Circle, intersections, false, events);
break;
case Event.EventType.End:
CheckIntersections(active, e, intersections, true, events);
CheckIntersections(active, e, intersections, false, events);
active.Remove(new SegmentKey(SegmentKey.SegmentType.Upper,
hash, e.Circle));
active.Remove(new SegmentKey(SegmentKey.SegmentType.Bottom,
hash, e.Circle));
upper = new SegmentKey(SegmentKey.SegmentType.Upper, hash, e.Circle);
lower = new SegmentKey(SegmentKey.SegmentType.Bottom, hash, e.Circle);
if (active.TrySuccessor(upper, out other))
CheckIntersections(upper, other, e.Circle, intersections, true, events);
if (active.TryPredecessor(lower, out other))
CheckIntersections(lower, other, e.Circle, intersections, false, events);
active.Remove(upper);
active.Remove(lower);
break;
case Event.EventType.Intersection:
var s1 = e.Segments[0].Key;
var s2 = e.Segments[1].Key;
var s1 = e.Segments[0];
var s2 = e.Segments[1];
_sweepline -= 10e-12;
active.Remove(s1.Key);
active.Remove(s2.Key);
_sweepline += 20e-12;
active.Remove(s1);
active.Remove(s2);
try
{
active.Add(s2, e.Segments[1].Value);
active.Add(s1, e.Segments[0].Value);
active.Add(s2.Key, e.Segments[1].Value);
active.Add(s1.Key, e.Segments[0].Value);
}
catch (DuplicateNotAllowedException)
{
Console.WriteLine("error");
}
CheckIntersections(active, e, intersections, true, events);
CheckIntersections(active, e, intersections, false, events);
if (s1.Key.Type == SegmentKey.SegmentType.Upper)
{
if (active.TrySuccessor(s1.Key, out other))
CheckIntersections(s1.Key, other, s1.Value, intersections, true, events);
}
else
{
if (active.TryPredecessor(s1.Key, out other))
CheckIntersections(s1.Key, other, s1.Value, intersections, true, events);
}
if (s2.Key.Type == SegmentKey.SegmentType.Upper)
{
if (active.TrySuccessor(s2.Key, out other))
CheckIntersections(s2.Key, other, s2.Value, intersections, true, events);
}
else
{
if (active.TryPredecessor(s2.Key, out other))
CheckIntersections(s2.Key, other, s2.Value, intersections, true, events);
}
break;
default:
throw new ArgumentOutOfRangeException();
@@ -77,34 +109,23 @@ namespace TMI_practicum
return intersections;
}
private static void CheckIntersections(C5.TreeDictionary<SegmentKey, Circle> active, Event e, System.Collections.Generic.HashSet<Intersection> intersections, bool upper, PriorityQueue<Event> events)
private static void CheckIntersections(SegmentKey segment, C5.KeyValuePair<SegmentKey, Circle> neighbour, Circle circle,
ISet<Intersection> intersections, bool upper, PriorityQueue<Event> events)
{
C5.KeyValuePair<SegmentKey, Circle> other;
SegmentKey s;
if (upper)
{
s = new SegmentKey(SegmentKey.SegmentType.Upper,
e.GetHashCode(), e.Circle);
if (!active.TrySuccessor(s, out other))
return;
}
else
{
s = new SegmentKey(SegmentKey.SegmentType.Bottom, e.GetHashCode(), e.Circle);
if (!active.TryPredecessor(
s, out other))
{
return;
}
}
var intersects = e.Circle.FindIntersections(other.Value);
var intersects = circle.FindIntersections(neighbour.Value);
if (intersects == null) return;
foreach (var intersection in intersects)
{
// if (upper)
// {
// if (intersection.Y < circle.Y) continue;
// }
// else if (intersection.Y > circle.Y) continue;
if (intersections.Contains(intersection)) continue;
events.Add(new Event(Event.EventType.Intersection, intersection.X, e.Circle, intersection.Y, new []{new C5.KeyValuePair<SegmentKey, Circle>(s, e.Circle), other}));
events.Add(new Event(Event.EventType.Intersection, intersection.X, circle, intersection.Y,
new[] {new C5.KeyValuePair<SegmentKey, Circle>(segment, circle), neighbour}));
intersections.Add(intersection);
}
}
@@ -135,7 +156,6 @@ namespace TMI_practicum
Segments = segments;
}
public int CompareTo(Event other)
{
return X.CompareTo(other.X);
@@ -147,8 +167,20 @@ namespace TMI_practicum
{
int hash = 17;
hash = hash * 23 + Type.GetHashCode();
hash = hash * 23 + X.GetHashCode();
hash = hash * 23 + Y.GetHashCode();
hash = hash * 23 + Circle.X.GetHashCode();
hash = hash * 23 + Circle.Y.GetHashCode();
return hash;
}
}
public int GetIdentifier()
{
unchecked
{
int hash = 17;
hash = hash * 23 + EventType.Start.GetHashCode();
hash = hash * 23 + Circle.X.GetHashCode();
hash = hash * 23 + Circle.Y.GetHashCode();
return hash;
}
}
@@ -172,7 +204,7 @@ namespace TMI_practicum
}
}
private SegmentType Type { get; }
public SegmentType Type { get; }
private readonly int _identifier;
public SegmentKey(SegmentType type, int identifier, Circle circle)