Skip to content

Commit

Permalink
Add self-intersection check
Browse files Browse the repository at this point in the history
  • Loading branch information
Vort committed Jan 10, 2017
1 parent c7b6b5b commit 9c1ad7e
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
47 changes: 47 additions & 0 deletions Intersection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System.Collections.Generic;

namespace RiverTrace
{
class Intersection
{
private static bool LinesIntersect(Vector a, Vector b, Vector c, Vector d)
{
// Source:
// http://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect#comment19165734_565282

Vector CmP = new Vector(c.X - a.X, c.Y - a.Y);
Vector r = new Vector(b.X - a.X, b.Y - a.Y);
Vector s = new Vector(d.X - c.X, d.Y - c.Y);

double CmPxr = CmP.X * r.Y - CmP.Y * r.X;
double CmPxs = CmP.X * s.Y - CmP.Y * s.X;
double rxs = r.X * s.Y - r.Y * s.X;

if (CmPxr == 0.0)
{
return ((c.X - a.X < 0.0) != (c.X - b.X < 0.0)) ||
((c.Y - a.Y < 0.0) != (c.Y - b.Y < 0.0));
}

if (rxs == 0.0)
return false;

double rxsr = 1.0 / rxs;
double t = CmPxs * rxsr;
double u = CmPxr * rxsr;

return (t >= 0.0) && (t <= 1.0) && (u >= 0.0) && (u <= 1.0);
}

public static bool Check(List<Vector> way, Vector point)
{
if (way.Count < 3)
return true;
Vector lastPoint = way[way.Count - 1];
for (int i = 0; i < way.Count - 2; i++)
if (LinesIntersect(way[i], way[i + 1], lastPoint, point))
return false;
return true;
}
}
}
1 change: 1 addition & 0 deletions RiverTrace.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Compile Include="Bing.cs" />
<Compile Include="Color.cs" />
<Compile Include="Config.cs" />
<Compile Include="Intersection.cs" />
<Compile Include="Program.cs" />
<Compile Include="Projection.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
16 changes: 10 additions & 6 deletions Tracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,21 @@ public Tracer()

tileMap = new TileMap(Config.Data.zoom);

var way = new List<Vector>();

Vector p1 = new Vector();
Vector p2 = new Vector();
Projection.DegToPix(Config.Data.lat1, Config.Data.lon1, Config.Data.zoom, out p1.X, out p1.Y);
Projection.DegToPix(Config.Data.lat2, Config.Data.lon2, Config.Data.zoom, out p2.X, out p2.Y);

way.Add(p1);
Vector lastDirection = p2 - p1;
lastDirection.Normalize();

CalcSampleDimensions(p1, lastDirection);

int pixelRange = (int)(scanRadius * 2) + 1;

var way = new List<Vector>();
way.Add(p1);

List<SimpleBitmap> samples = new List<SimpleBitmap>();

Vector lastPoint = p1;
Expand Down Expand Up @@ -182,16 +182,20 @@ public Tracer()
Config.Data.angleStep - Config.Data.angleRange);
lastDirection = lastDirection.Rotated(bestAngle);
lastPoint += lastDirection * scanRadius * Config.Data.advanceRate;

if (!Intersection.Check(way, lastPoint))
break;

way.Add(lastPoint);
}
sw.Stop();

way.Reverse();
if (Config.Data.simplificationStrength > 0.0)
{
way = Simplify.DouglasPeuckerReduction(way,
riverWidthPx * Config.Data.simplificationStrength);
}
way.Reverse();
sw.Stop();

WriteOsm(way);

for (int i = 0; i < 25; i++)
Expand Down

0 comments on commit 9c1ad7e

Please sign in to comment.