1
0
This commit is contained in:
2019-05-18 23:40:07 +02:00
parent fed17cd200
commit 71e650e373
5 changed files with 129 additions and 71 deletions

View File

@@ -10,5 +10,23 @@ namespace TMI_practicum
X = x;
Y = y;
}
public override int GetHashCode()
{
return X.GetHashCode() ^ Y.GetHashCode();
}
public override bool Equals(object obj)
{
return obj is Intersection && this == (Intersection) obj;
}
public static bool operator ==(Intersection x, Intersection y)
{
return x.X == y.X && x.Y == y.Y;
}
public static bool operator !=(Intersection x, Intersection y)
{
return !(x == y);
}
}
}

View File

@@ -10,8 +10,7 @@ namespace TMI_practicum
{
public static void Main(string[] args)
{
//SweepLineEffAlgorithm.Solve(null);
GetChartData();
if (args.Length < 1)
{
Console.WriteLine("Please enter a file.");
@@ -26,9 +25,11 @@ namespace TMI_practicum
var (algorithm, _, circles) = ParseFile(args[0]);
IEnumerable<Intersection> solved;
Stopwatch stopwatch = new Stopwatch();
IEnumerable<Intersection> solved;
stopwatch.Start();
switch (algorithm)
@@ -51,6 +52,50 @@ namespace TMI_practicum
}
private static List<Circle> CreateRandomCircles(double amount)
{
var circles = new List<Circle>();
Random rnd = new Random();
for (int i = 0; i < amount; i++)
{
circles.Add(new Circle(Math.Round(rnd.NextDouble() * 1.0, 15), Math.Round(rnd.NextDouble() * 1.0, 15), Math.Round(rnd.NextDouble() * 0.01, 15)));
}
return circles;
}
private static void GetChartData()
{
var stopwatch = new Stopwatch();
//warmup
var circles = CreateRandomCircles(20);
IEnumerable<Intersection> solved = SweepLineEffAlgorithm.Solve(circles);
var times = new List<long>();
Console.WriteLine("size, time");
for (int s = 10; s < 100000; s *= 2)
{
for (int i = 0; i < 100; i++)
{
circles = CreateRandomCircles(s);
stopwatch.Restart();
solved = SweepLineEffAlgorithm.Solve(circles);
stopwatch.Stop();
times.Add(stopwatch.ElapsedMilliseconds);
}
Console.WriteLine("{0}, {1}", s, times.Average());
times.Clear();
}
stopwatch.Stop();
}
private static (byte Algorithm, int NbCircles, IList<Circle> Circles) ParseFile(string path)
{

View File

@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Medallion.Collections;
namespace TMI_practicum
@@ -9,17 +8,15 @@ namespace TMI_practicum
{
public static IEnumerable<Intersection> Solve(IEnumerable<Circle> circles)
{
var intersections = new Stack<Intersection>();
var intersections = new HashSet<Intersection>();
var events = new PriorityQueue<Event>();
var upperActive = new CircleTree<SegmentKey, Circle>();
var bottomActive = new CircleTree<SegmentKey, Circle>();
var active = new C5.TreeDictionary<SegmentKey, Circle>();
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));
events.Enqueue(new Event(Event.EventType.Start, circle.X - circle.R, circle, circle.Y));
events.Enqueue(new Event(Event.EventType.End, circle.X + circle.R, circle, circle.Y));
}
while (events.Count != 0)
@@ -29,73 +26,63 @@ namespace TMI_practicum
switch (e.Type)
{
case Event.EventType.Start:
upperActive.Add(
new SegmentKey(e.X, e.Circle.Y + e.Circle.R, SegmentKey.SegmentType.Upper, hash), e.Circle);
bottomActive.Add(
new SegmentKey(e.X, e.Circle.Y - e.Circle.R, SegmentKey.SegmentType.Bottom, hash),
active.Add(
new SegmentKey(e.Circle.X, e.Circle.Y + e.Circle.R, SegmentKey.SegmentType.Upper, hash), e.Circle);
active.Add(
new SegmentKey(e.Circle.X, e.Circle.Y - e.Circle.R, SegmentKey.SegmentType.Bottom, hash),
e.Circle);
CheckIntersections(upperActive, e, intersections, true);
CheckIntersections(bottomActive, e, intersections, false);
CheckIntersections(active, e, intersections, true, events);
CheckIntersections(active, e, intersections, false, events);
break;
case Event.EventType.End:
upperActive.Remove(new SegmentKey(e.X, e.Circle.Y + e.Circle.R, SegmentKey.SegmentType.Upper,
CheckIntersections(active, e, intersections, true, events);
CheckIntersections(active, e, intersections, false, events);
active.Remove(new SegmentKey(e.Circle.X, e.Circle.Y + e.Circle.R, SegmentKey.SegmentType.Upper,
hash));
bottomActive.Remove(new SegmentKey(e.X, e.Circle.Y - e.Circle.R, SegmentKey.SegmentType.Bottom,
active.Remove(new SegmentKey(e.Circle.X, e.Circle.Y - e.Circle.R, SegmentKey.SegmentType.Bottom,
hash));
break;
case Event.EventType.Intersection:
CheckIntersections(active, e, intersections, true, events);
CheckIntersections(active, e, intersections, false, events);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
return intersections;
}
private static void CheckIntersections(CircleTree<SegmentKey, Circle> active, Event e,
Stack<Intersection> intersections, bool upper)
private static void CheckIntersections(C5.TreeDictionary<SegmentKey, Circle> active, Event e,
HashSet<Intersection> intersections, bool upper, PriorityQueue<Event> events)
{
Circle other = null;
try
{
other = upper
? active.FindSuccessor(new SegmentKey(e.X, e.Circle.Y, SegmentKey.SegmentType.Upper,
e.GetHashCode()))
: active.FindPredecessor(new SegmentKey(e.X, e.Circle.Y, SegmentKey.SegmentType.Bottom,
e.GetHashCode()));
}
catch (KeyNotFoundException)
{
}
if (other != null)
C5.KeyValuePair<SegmentKey, Circle> other;
if (upper)
{
var intersects = e.Circle.FindIntersections(other);
if (intersects != null)
if (!active.TrySuccessor(new SegmentKey(e.X, e.Y, SegmentKey.SegmentType.Upper,
e.GetHashCode()), out other))
return;
}
else
{
if (!active.TryPredecessor(
new SegmentKey(e.X, e.Y, SegmentKey.SegmentType.Bottom, e.GetHashCode()), out other))
{
foreach (var intersection in intersects)
{
if (upper && e.Circle.Y == intersection.Y) continue;
intersections.Push(intersection);
}
return;
}
}
}
private class CircleTree<TKey, TValue> : SortedDictionary<TKey, TValue>
{
public TValue FindSuccessor(TKey key)
var intersects = e.Circle.FindIntersections(other.Value);
if (intersects == null) return;
foreach (var intersection in intersects)
{
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];
if (intersections.Contains(intersection)) continue;
events.Add(new Event(Event.EventType.Intersection, intersection.X, e.Circle, intersection.Y));
intersections.Add(intersection);
}
}
@@ -104,24 +91,40 @@ namespace TMI_practicum
public EventType Type { get; }
public Circle Circle { get; }
public double X { get; }
public double Y { get; }
public enum EventType
{
Start,
Intersection,
End
}
public Event(EventType type, double x, Circle circle)
public Event(EventType type, double x, Circle circle, double y)
{
Type = type;
X = x;
Circle = circle;
Y = y;
}
public int CompareTo(Event other)
{
return X.CompareTo(other.X);
}
public override int GetHashCode()
{
unchecked // Overflow is fine, just wrap
{
int hash = 17;
// Suitable nullity checks etc, of course :)
hash = hash * 23 + Type.GetHashCode();
hash = hash * 23 + X.GetHashCode();
hash = hash * 23 + Y.GetHashCode();
return hash;
}
}
}
private struct SegmentKey : IComparable<SegmentKey>
@@ -168,17 +171,4 @@ namespace TMI_practicum
}
}
}
public static class EnumerableExtensions
{
public static T MinOrDefault<T>(this IEnumerable<T> sequence)
{
return sequence.Any() ? sequence.Min() : default;
}
public static T MaxOrDefault<T>(this IEnumerable<T> sequence)
{
return sequence.Any() ? sequence.Max() : default;
}
}
}

View File

@@ -33,6 +33,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="C5, Version=2.5.0.0, Culture=neutral, PublicKeyToken=282361b99ded7e8e">
<HintPath>..\packages\C5.2.5.3\lib\net45\C5.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="MedallionPriorityQueue, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\packages\MedallionPriorityQueue.1.1.0\lib\net45\MedallionPriorityQueue.dll</HintPath>
<Private>True</Private>

View File

@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="C5" version="2.5.3" targetFramework="net461" />
<package id="MedallionPriorityQueue" version="1.1.0" targetFramework="net461" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net461" />
</packages>