forked from drewnoakes/quadrilateral-finder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntersection.cs
77 lines (64 loc) · 2.39 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#region License
/*
* This file is part of QuadrilateralFinder.
*
* QuadrilateralFinder is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QuadrilateralFinder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with QuadrilateralFinder. If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
// Copyright Drew Noakes, http://drewnoakes.com
namespace DrewNoakes.QuadrilateralFinder
{
/// <summary>
/// Models the intersection of two <see cref="LineSegment2"/> instances.
/// </summary>
public struct Intersection
{
public LineSegment2 Line1 { get; private set; }
public LineSegment2 Line2 { get; private set; }
/// <summary>
/// The point at which the lines intersect. Will be (0,0) if <see cref="HasIntersection"/> is <c>false</c>.
/// </summary>
public Vector2 Point { get; private set; }
public double Distance1 { get; private set; }
public double Distance2 { get; private set; }
/// <summary>
/// Will be <c>false</c> if the line segments are parallel.
/// </summary>
public bool HasIntersection { get; private set; }
public Intersection(LineSegment2 line1, LineSegment2 line2) : this()
{
Line1 = line1;
Line2 = line2;
var p = line1.End1;
var q = line2.End1;
var r = line1.ToVector2();
var s = line2.ToVector2();
var denom = r.Cross(s);
if (denom == 0)
{
// The lines are parallel and there's no intersection
HasIntersection = false;
Distance1 = double.NaN;
Distance2 = double.NaN;
return;
}
var t = (q - p).Cross(s)/denom;
var u = (q - p).Cross(r)/denom;
Point = p + r*t;
Distance1 = t;
Distance2 = u;
HasIntersection = true;
}
}
}