-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLineLineAnalyticalIntersection2d.cs
125 lines (103 loc) · 3.49 KB
/
LineLineAnalyticalIntersection2d.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Rhino;
using Rhino.Geometry;
using Rhino.Collections;
namespace QuadHyp
{
public class LineLineAnalyticalIntersection2d
{
public Line line1;
public Line line2;
public double t1;
public double t2;
public bool isIntersecting;
public bool isAtEnds = false;
public Point3d intersectionPoint;
double p0, p1, p2, p3;
double x0, x1, x2, x3;
double y0, y1, y2, y3;
public LineLineAnalyticalIntersection2d(Line _line1, Line _line2)
{
line1 = _line1;
line2 = _line2;
x0 = line1.From.X;
x1 = line1.To.X;
x2 = line2.From.X;
x3 = line2.To.X;
y0 = line1.From.Y;
y1 = line1.To.Y;
y2 = line2.From.Y;
y3 = line2.To.Y;
CalculateIntersection();
}
public LineLineAnalyticalIntersection2d(Line _line1, Line _line2, bool forceCalculation)
{
if (forceCalculation)
{
}
else
{
line1 = _line1;
line2 = _line2;
x0 = line1.From.X;
x1 = line1.To.X;
x2 = line2.From.X;
x3 = line2.To.X;
y0 = line1.From.Y;
y1 = line1.To.Y;
y2 = line2.From.Y;
y3 = line2.To.Y;
CalculateIntersection();
}
}
void CalculateIntersection()
{
p0 = (y3 - y2) * (x3 - x0) - (x3 - x2) * (y3 - y0);
p1 = (y3 - y2) * (x3 - x1) - (x3 - x2) * (y3 - y1);
p2 = (y1 - y0) * (x1 - x2) - (x1 - x0) * (y1 - y2);
p3 = (y1 - y0) * (x1 - x3) - (x1 - x0) * (y1 - y3);
isIntersecting = ((p0 * p1) <= 0) & ((p2 * p3) <= 0);
if (isIntersecting)
{
double det;
det = (x1 - x0) * (y3 - y2) - (y1 - y0) * (x3 - x2);
t1 = 1 / det * (
(y3 - y2) * (x2 - x0) - (x3 - x2) * (y2 - y0)
);
t2 = 1 / det * (
(y1 - y0) * (x2 - x0) - (x1 - x0) * (y2 - y0)
);
intersectionPoint = line1.PointAt(t1);
}
if ((((t1 == 0) ? 1 : 0) + ((t2 == 0) ? 1 : 0) + ((t1 == 1) ? 1 : 0) + ((t2 == 1) ? 1 : 0)) >= 1)
{
isAtEnds = true;
}
}
void ForceCalculateIntersection()
{
p0 = (y3 - y2) * (x3 - x0) - (x3 - x2) * (y3 - y0);
p1 = (y3 - y2) * (x3 - x1) - (x3 - x2) * (y3 - y1);
p2 = (y1 - y0) * (x1 - x2) - (x1 - x0) * (y1 - y2);
p3 = (y1 - y0) * (x1 - x3) - (x1 - x0) * (y1 - y3);
isIntersecting = ((p0 * p1) <= 0) & ((p2 * p3) <= 0);
double det;
det = (x1 - x0) * (y3 - y2) - (y1 - y0) * (x3 - x2);
t1 = 1 / det * (
(y3 - y2) * (x2 - x0) - (x3 - x2) * (y2 - y0)
);
t2 = 1 / det * (
(y1 - y0) * (x2 - x0) - (x1 - x0) * (y2 - y0)
);
intersectionPoint = line1.PointAt(t1);
if ((((t1 == 0) ? 1 : 0) + ((t2 == 0) ? 1 : 0) + ((t1 == 1) ? 1 : 0) + ((t2 == 1) ? 1 : 0)) >= 1)
{
isAtEnds = true;
}
}
}
}