-
Notifications
You must be signed in to change notification settings - Fork 1
/
Intersection.cs
47 lines (39 loc) · 1.51 KB
/
Intersection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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;
}
}
}