Skip to content

Commit fab7b2e

Browse files
committed
made change
1 parent ba9395d commit fab7b2e

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

content/geometry/lineIntersection.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
* Source:
66
* Description:\\
77
\begin{minipage}{75mm}
8-
If a unique intersetion point of the lines going through s1,e1 and s2,e2 exists r is set to this point and 1 is returned. If no intersection point exists 0 is returned and if infinitely many exists -1 is returned. If s1==e1 or s2==e2 -1 is returned. The wrong position will be returned if P is Point<int> and the intersection point does not have integer coordinates. Products of three coordinates are used in intermediate steps so watch out for overflow if using int or long long.
8+
If a unique intersection point of the lines going through s1,e1 and s2,e2 exists r is set to this point and 1 is returned. If no intersection point exists 0 is returned and if infinitely many exists -1 is returned. If s1==e1 or s2==e2 -1 is returned. The wrong position will be returned if P is Point<int> and the intersection point does not have integer coordinates. Products of three coordinates are used in intermediate steps so watch out for overflow if using int or long long.
99
\end{minipage}
1010
\begin{minipage}{15mm}
1111
\includegraphics[width=\textwidth]{content/geometry/lineIntersection}
1212
\end{minipage}
1313
* Status: tested
14-
* Usage:
14+
* Usage:
1515
* point<double> intersection;
1616
* if (1 == LineIntersection(s1,e1,s2,e2,intersection))
1717
* cout << "intersection point at " << intersection << endl;
@@ -21,11 +21,10 @@ If a unique intersetion point of the lines going through s1,e1 and s2,e2 exists
2121
#include "Point.h"
2222

2323
template<class P>
24-
int lineIntersection(const P& s1, const P& e1, const P& s2,
25-
const P& e2, P& r) {
26-
if ((e1-s1).cross(e2-s2)) { //if not parallell
27-
r = s2-(e2-s2)*(e1-s1).cross(s2-s1)/(e1-s1).cross(e2-s2);
28-
return 1;
29-
} else
30-
return -((e1-s1).cross(s2-s1)==0 || s2==e2);
24+
pair<int, P> lineIntersection(P s1, P e1, P s2, P e2) {
25+
auto d = (e1-s1).cross(e2-s2);
26+
if (d == 0) //if parallel
27+
return {-((e1-s1).cross(s2-s1)==0 || s2==e2), P(0,0)};
28+
else
29+
return {1, s2-(e2-s2)*(e1-s1).cross(s2-s1)/d};
3130
}

0 commit comments

Comments
 (0)