5
5
* Source: https://vlecomte.github.io/cp-geo.pdf
6
6
* Description:\\
7
7
\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}
12
13
\begin{minipage}{15mm}
13
14
\includegraphics[width=\textwidth]{content/geometry/lineIntersection}
14
15
\end{minipage}
@@ -22,12 +23,11 @@ three coordinates are used in intermediate steps so watch out for overflow if us
22
23
23
24
#include " Point.h"
24
25
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};
33
33
}
0 commit comments