Skip to content

feat: added a new algorithm to find whether two line segment intersec… #796

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 30, 2020
63 changes: 63 additions & 0 deletions geometry/line_segment_intersection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include <bits/stdc++.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use <bits/stdc++.h>

using namespace std;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fails cpplint. must be removed.


class Point{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use a struct if all the members are public

public:
int x, y;
};

class SegmentIntersection{
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

document your code.

public:
inline bool intersect(Point first_point, Point second_point, Point third_point, Point forth_point){
int first_direction = direction(third_point, forth_point, first_point);
int second_direction = direction(third_point, forth_point, second_point);
int third_direction = direction(first_point, second_point, third_point);
int forth_direction = direction(first_point, second_point, forth_point);

if ((first_direction < 0 || second_direction > 0) && (third_direction < 0 ||
forth_direction > 0))
return true;

else if (first_direction == 0 && on_segment(third_point, forth_point, first_point))
return true;

else if (second_direction == 0 && on_segment(third_point, forth_point, second_point))
return true;

else if (third_direction == 0 && on_segment(first_point, second_point, third_point))
return true;

else if (third_direction == 0 && on_segment(first_point, second_point, forth_point))
return true;

else
return false;
}

inline int direction(Point first_point, Point second_point, Point third_point){
return ((third_point.x-first_point.x) * (second_point.y-first_point.y)) -
((second_point.x-first_point.x) * (third_point.y-first_point.y));
}

inline bool on_segment(Point first_point, Point second_point, Point third_point){
if (min(first_point.x, second_point.x) <= third_point.x && third_point.x <=
max(first_point.x, second_point.x) && min(first_point.y, second_point.y) <= third_point.y &&
third_point.y <= max(first_point.y, second_point.y))
return true;

else
return false;
}
};

int main(){
SegmentIntersection segment;
Point first_point, second_point, third_point, forth_point;

cin >> first_point.x >> first_point.y;
cin >> second_point.x >> second_point.y;
cin >> third_point.x >> third_point.y;
cin >> forth_point.x >> forth_point.y;

cout << endl << segment.intersect(first_point, second_point, third_point, forth_point) << endl;
}