Skip to content

Commit 2771e3e

Browse files
committed
Clean up lineIntersection
1 parent 201c51c commit 2771e3e

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

content/geometry/lineIntersection.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
* Source: https://vlecomte.github.io/cp-geo.pdf
66
* Description:\\
77
\begin{minipage}{75mm}
8-
If a unique intersection point of the lines going through s1,e1 and s2,e2 exists {1, point} is returned. If no
9-
intersection point exists {0, (0,0)} is returned and if infinitely many exists {-1, (0,0)} is returned. The wrong
10-
position will be returned if P is Point<ll> and the intersection point does not have integer coordinates. Products of
11-
three coordinates are used in intermediate steps so watch out for overflow if using int or long long. \end{minipage}
8+
If a unique intersection point of the lines going through s1,e1 and s2,e2 exists \{1, point\} is returned.
9+
If no intersection point exists \{0, (0,0)\} is returned and if infinitely many exists \{-1, (0,0)\} is returned.
10+
The wrong position will be returned if P is Point<ll> and the intersection point does not have integer coordinates.
11+
Products of three coordinates are used in intermediate steps so watch out for overflow if using int or ll.
12+
\end{minipage}
1213
\begin{minipage}{15mm}
1314
\includegraphics[width=\textwidth]{content/geometry/lineIntersection}
1415
\end{minipage}
@@ -22,12 +23,11 @@ three coordinates are used in intermediate steps so watch out for overflow if us
2223

2324
#include "Point.h"
2425

25-
template <class P> pair<int, P> lineInter(P s1, P e1, P s2, P e2) {
26-
auto d = (e1 - s1).cross(e2 - s2);
27-
if (d == 0) // if parallel
28-
return {-(s1.cross(e1, s2) == 0 || s2 == e2), P(0, 0)};
29-
else {
30-
double a1 = s2.cross(e2, s1), a2 = -s2.cross(e2, e1);
31-
return {1, (s1 * a2 + e1 * a1) / (a1 + a2)};
32-
}
26+
template<class P>
27+
pair<int, P> lineInter(P s1, P e1, P s2, P e2) {
28+
auto d = (e1 - s1).cross(e2 - s2);
29+
if (d == 0) // if parallel
30+
return {-(s1.cross(e1, s2) == 0), P(0, 0)};
31+
auto p = s2.cross(e1, e2), q = s2.cross(e2, s1);
32+
return {1, (s1 * p + e1 * q) / d};
3333
}

0 commit comments

Comments
 (0)