From 41df377fbf602b520069bc9476a09ec3801c84dc Mon Sep 17 00:00:00 2001 From: Sajied Shah Yousuf <40203390+meSajied@users.noreply.github.com> Date: Tue, 26 May 2020 19:17:05 +0600 Subject: [PATCH 01/12] feat: added a new algorithm to find whether two line segment intersect or not --- geometry/line_segment_intersection.cpp | 63 ++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 geometry/line_segment_intersection.cpp diff --git a/geometry/line_segment_intersection.cpp b/geometry/line_segment_intersection.cpp new file mode 100644 index 00000000000..3e08ef6659b --- /dev/null +++ b/geometry/line_segment_intersection.cpp @@ -0,0 +1,63 @@ +#include +using namespace std; + +class Point{ +public: + int x, y; +}; + +class SegmentIntersection{ +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; +} From f1cd8486a6050be1aeea92bcdf1f9aceb75cdb14 Mon Sep 17 00:00:00 2001 From: Sajied Shah Yousuf <40203390+meSajied@users.noreply.github.com> Date: Wed, 27 May 2020 15:01:32 +0600 Subject: [PATCH 02/12] fix: cpplint issues --- geometry/line_segment_intersection.cpp | 84 +++++++++++++++----------- 1 file changed, 48 insertions(+), 36 deletions(-) diff --git a/geometry/line_segment_intersection.cpp b/geometry/line_segment_intersection.cpp index 3e08ef6659b..3102b923d59 100644 --- a/geometry/line_segment_intersection.cpp +++ b/geometry/line_segment_intersection.cpp @@ -1,63 +1,75 @@ -#include -using namespace std; +#include -class Point{ -public: +/* +* A algorithm to find whether two line intersect where "on_segment" finds +* the cross multiplication and direction find whether line is in clockwise or not.... +*/ + +struct Point { int x, y; }; -class SegmentIntersection{ -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); +struct SegmentIntersection { + inline bool intersect(Point first_point, Point second_point, + Point third_point, Point forth_point) { + int direction1 = direction(third_point, forth_point, first_point); + int direction2 = direction(third_point, forth_point, second_point); + int direction3 = direction(first_point, second_point, third_point); + int direction4 = direction(first_point, second_point, forth_point); - if ((first_direction < 0 || second_direction > 0) && (third_direction < 0 || - forth_direction > 0)) + if ((direction1 < 0 || direction2 > 0) && (direction3 < 0 || + direction4 > 0)) return true; - - else if (first_direction == 0 && on_segment(third_point, forth_point, first_point)) + + else if (direction1 == 0 && on_segment(third_point, forth_point, + first_point)) return true; - - else if (second_direction == 0 && on_segment(third_point, forth_point, second_point)) + + else if (direction2 == 0 && on_segment(third_point, forth_point, + second_point)) return true; - - else if (third_direction == 0 && on_segment(first_point, second_point, third_point)) + + else if (direction3 == 0 && on_segment(first_point, second_point, + third_point)) return true; - - else if (third_direction == 0 && on_segment(first_point, second_point, forth_point)) + + else if (direction3 == 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 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)) + inline bool on_segment(Point first_point, Point second_point, + Point third_point) { + if (std::min(first_point.x, second_point.x) <= third_point.x && + third_point.x <= std::max(first_point.x, second_point.x) && + std::min(first_point.y, second_point.y) <= third_point.y && + third_point.y <= std::max(first_point.y, second_point.y)) return true; - + else return false; } }; -int main(){ +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; + std::cin >> first_point.x >> first_point.y; + std::cin >> second_point.x >> second_point.y; + std::cin >> third_point.x >> third_point.y; + std::cin >> forth_point.x >> forth_point.y; - cout << endl << segment.intersect(first_point, second_point, third_point, forth_point) << endl; + printf("%d", segment.intersect(first_point, second_point, third_point, + forth_point)); + std::cout << std::endl; } From 56ea92e383d8ecce42100ef7c40dea7e0dd2f2d8 Mon Sep 17 00:00:00 2001 From: Sajied Shah Yousuf <40203390+meSajied@users.noreply.github.com> Date: Thu, 28 May 2020 17:34:32 +0600 Subject: [PATCH 03/12] added documentation..... --- geometry/line_segment_intersection.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/geometry/line_segment_intersection.cpp b/geometry/line_segment_intersection.cpp index 3102b923d59..fb4efe36f32 100644 --- a/geometry/line_segment_intersection.cpp +++ b/geometry/line_segment_intersection.cpp @@ -12,6 +12,7 @@ struct Point { struct SegmentIntersection { inline bool intersect(Point first_point, Point second_point, Point third_point, Point forth_point) { + // check whether two line intersect or not... int direction1 = direction(third_point, forth_point, first_point); int direction2 = direction(third_point, forth_point, second_point); int direction3 = direction(first_point, second_point, third_point); @@ -43,12 +44,14 @@ struct SegmentIntersection { inline int direction(Point first_point, Point second_point, Point third_point) { + // check whether points are colinear, clockwise or counterclockwise... 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) { + // check whether two line overlap or not... if (std::min(first_point.x, second_point.x) <= third_point.x && third_point.x <= std::max(first_point.x, second_point.x) && std::min(first_point.y, second_point.y) <= third_point.y && From 56ded8979f6ea2dababafc1cbec693b91ac6fb05 Mon Sep 17 00:00:00 2001 From: Sajied Shah Yousuf <40203390+meSajied@users.noreply.github.com> Date: Thu, 28 May 2020 17:35:42 +0600 Subject: [PATCH 04/12] added documentation.... --- geometry/line_segment_intersection.cpp | 5 ----- 1 file changed, 5 deletions(-) diff --git a/geometry/line_segment_intersection.cpp b/geometry/line_segment_intersection.cpp index fb4efe36f32..a10c3d18f30 100644 --- a/geometry/line_segment_intersection.cpp +++ b/geometry/line_segment_intersection.cpp @@ -1,10 +1,5 @@ #include -/* -* A algorithm to find whether two line intersect where "on_segment" finds -* the cross multiplication and direction find whether line is in clockwise or not.... -*/ - struct Point { int x, y; }; From 1ecd631a0c332d6e63d7c1a154b0faa57de1ee08 Mon Sep 17 00:00:00 2001 From: Sajied Shah Yousuf <40203390+meSajied@users.noreply.github.com> Date: Fri, 29 May 2020 18:14:06 +0600 Subject: [PATCH 05/12] added documentation... --- geometry/line_segment_intersection.cpp | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/geometry/line_segment_intersection.cpp b/geometry/line_segment_intersection.cpp index a10c3d18f30..8844cda3c18 100644 --- a/geometry/line_segment_intersection.cpp +++ b/geometry/line_segment_intersection.cpp @@ -7,7 +7,11 @@ struct Point { struct SegmentIntersection { inline bool intersect(Point first_point, Point second_point, Point third_point, Point forth_point) { - // check whether two line intersect or not... + /* + * intersect returns true if segments of two line intersects and + * false if they do not. It calls the subroutines direction + * which computes the orientation. + */ int direction1 = direction(third_point, forth_point, first_point); int direction2 = direction(third_point, forth_point, second_point); int direction3 = direction(first_point, second_point, third_point); @@ -39,14 +43,25 @@ struct SegmentIntersection { inline int direction(Point first_point, Point second_point, Point third_point) { - // check whether points are colinear, clockwise or counterclockwise... + /* + * We will find direction of line here respect to @first_point. + * Here @second_point and @third_point is first and second points + * of the line respectively. we want a method to determine which way a + * given angle these three points turns. If returned number is negative, + * then the angle is counter-clockwise. That means the line is going to + * right to left. We will fount angle as clockwise if the method returns + * positive number. + */ 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) { - // check whether two line overlap or not... + /* + * This method determines whether a point known to be colinear + * with a segment lies on that segment. + */ if (std::min(first_point.x, second_point.x) <= third_point.x && third_point.x <= std::max(first_point.x, second_point.x) && std::min(first_point.y, second_point.y) <= third_point.y && From e66c5a8823ad23c45f665541e5e5a9afd9b6b44c Mon Sep 17 00:00:00 2001 From: Sajied Shah Yousuf <40203390+meSajied@users.noreply.github.com> Date: Fri, 29 May 2020 19:49:56 +0600 Subject: [PATCH 06/12] added doxygen format.... --- geometry/line_segment_intersection.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/geometry/line_segment_intersection.cpp b/geometry/line_segment_intersection.cpp index 8844cda3c18..9fb9b12a700 100644 --- a/geometry/line_segment_intersection.cpp +++ b/geometry/line_segment_intersection.cpp @@ -7,7 +7,7 @@ struct Point { struct SegmentIntersection { inline bool intersect(Point first_point, Point second_point, Point third_point, Point forth_point) { - /* + /** * intersect returns true if segments of two line intersects and * false if they do not. It calls the subroutines direction * which computes the orientation. @@ -43,7 +43,7 @@ struct SegmentIntersection { inline int direction(Point first_point, Point second_point, Point third_point) { - /* + /** * We will find direction of line here respect to @first_point. * Here @second_point and @third_point is first and second points * of the line respectively. we want a method to determine which way a @@ -58,7 +58,7 @@ struct SegmentIntersection { inline bool on_segment(Point first_point, Point second_point, Point third_point) { - /* + /** * This method determines whether a point known to be colinear * with a segment lies on that segment. */ From 84531fbdd76242eabb431fffa99d08640d49814f Mon Sep 17 00:00:00 2001 From: Sajied Shah Yousuf <40203390+meSajied@users.noreply.github.com> Date: Fri, 29 May 2020 20:23:53 +0600 Subject: [PATCH 07/12] fix: comment blocks... --- geometry/line_segment_intersection.cpp | 56 +++++++++++++++----------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/geometry/line_segment_intersection.cpp b/geometry/line_segment_intersection.cpp index 9fb9b12a700..72fb34bc0ef 100644 --- a/geometry/line_segment_intersection.cpp +++ b/geometry/line_segment_intersection.cpp @@ -1,17 +1,24 @@ +/** + * @file + * @brief check whether two line segments intersect each other + * or not. + */ #include - +/* + * Define a Point. + */ struct Point { - int x, y; + int x; /// Point respect to x coordinate + int y; /// Point respect to y coordinate }; - +/** + * intersect returns true if segments of two line intersects and + * false if they do not. It calls the subroutines direction + * which computes the orientation. + */ struct SegmentIntersection { inline bool intersect(Point first_point, Point second_point, Point third_point, Point forth_point) { - /** - * intersect returns true if segments of two line intersects and - * false if they do not. It calls the subroutines direction - * which computes the orientation. - */ int direction1 = direction(third_point, forth_point, first_point); int direction2 = direction(third_point, forth_point, second_point); int direction3 = direction(first_point, second_point, third_point); @@ -40,28 +47,26 @@ struct SegmentIntersection { else return false; } - + /** + * We will find direction of line here respect to @first_point. + * Here @second_point and @third_point is first and second points + * of the line respectively. we want a method to determine which way a + * given angle these three points turns. If returned number is negative, + * then the angle is counter-clockwise. That means the line is going to + * right to left. We will fount angle as clockwise if the method returns + * positive number. + */ inline int direction(Point first_point, Point second_point, Point third_point) { - /** - * We will find direction of line here respect to @first_point. - * Here @second_point and @third_point is first and second points - * of the line respectively. we want a method to determine which way a - * given angle these three points turns. If returned number is negative, - * then the angle is counter-clockwise. That means the line is going to - * right to left. We will fount angle as clockwise if the method returns - * positive number. - */ 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)); } - + /** + * This method determines whether a point known to be colinear + * with a segment lies on that segment. + */ inline bool on_segment(Point first_point, Point second_point, Point third_point) { - /** - * This method determines whether a point known to be colinear - * with a segment lies on that segment. - */ if (std::min(first_point.x, second_point.x) <= third_point.x && third_point.x <= std::max(first_point.x, second_point.x) && std::min(first_point.y, second_point.y) <= third_point.y && @@ -72,7 +77,10 @@ struct SegmentIntersection { return false; } }; - +/* + * This is the main function to test whether the algorithm is + * working well. + */ int main() { SegmentIntersection segment; Point first_point, second_point, third_point, forth_point; From 6f3923dd865e08f1432b96efe73ff3a3c3fea848 Mon Sep 17 00:00:00 2001 From: Sajied Shah Yousuf <40203390+meSajied@users.noreply.github.com> Date: Fri, 29 May 2020 20:25:55 +0600 Subject: [PATCH 08/12] fix: cpplint issue From 3e15e82497168711fb754f6eb20b9a8b044f81a8 Mon Sep 17 00:00:00 2001 From: Sajied Shah Yousuf <40203390+meSajied@users.noreply.github.com> Date: Fri, 29 May 2020 20:28:57 +0600 Subject: [PATCH 09/12] fix: cpplint issues... --- geometry/line_segment_intersection.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/geometry/line_segment_intersection.cpp b/geometry/line_segment_intersection.cpp index 72fb34bc0ef..4e9cd06bd75 100644 --- a/geometry/line_segment_intersection.cpp +++ b/geometry/line_segment_intersection.cpp @@ -8,8 +8,8 @@ * Define a Point. */ struct Point { - int x; /// Point respect to x coordinate - int y; /// Point respect to y coordinate + int x; /// Point respect to x coordinate + int y; /// Point respect to y coordinate }; /** * intersect returns true if segments of two line intersects and From 165a30ca4470b56f705d714e57b056577dc0e40e Mon Sep 17 00:00:00 2001 From: Sajied Shah Yousuf <40203390+meSajied@users.noreply.github.com> Date: Fri, 29 May 2020 21:35:42 +0600 Subject: [PATCH 10/12] fix: comment spaces... --- geometry/line_segment_intersection.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/geometry/line_segment_intersection.cpp b/geometry/line_segment_intersection.cpp index 4e9cd06bd75..a5aaa5aa267 100644 --- a/geometry/line_segment_intersection.cpp +++ b/geometry/line_segment_intersection.cpp @@ -3,7 +3,9 @@ * @brief check whether two line segments intersect each other * or not. */ + #include + /* * Define a Point. */ @@ -11,6 +13,7 @@ struct Point { int x; /// Point respect to x coordinate int y; /// Point respect to y coordinate }; + /** * intersect returns true if segments of two line intersects and * false if they do not. It calls the subroutines direction @@ -47,6 +50,7 @@ struct SegmentIntersection { else return false; } + /** * We will find direction of line here respect to @first_point. * Here @second_point and @third_point is first and second points @@ -61,6 +65,7 @@ struct SegmentIntersection { 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)); } + /** * This method determines whether a point known to be colinear * with a segment lies on that segment. @@ -77,6 +82,7 @@ struct SegmentIntersection { return false; } }; + /* * This is the main function to test whether the algorithm is * working well. From 8243864e090aff3e19c471d372bfd6b85fbce3db Mon Sep 17 00:00:00 2001 From: Sajied Shah Yousuf <40203390+meSajied@users.noreply.github.com> Date: Fri, 29 May 2020 21:40:06 +0600 Subject: [PATCH 11/12] fix: additional spaces... --- geometry/line_segment_intersection.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/geometry/line_segment_intersection.cpp b/geometry/line_segment_intersection.cpp index a5aaa5aa267..5b515c23d01 100644 --- a/geometry/line_segment_intersection.cpp +++ b/geometry/line_segment_intersection.cpp @@ -3,7 +3,6 @@ * @brief check whether two line segments intersect each other * or not. */ - #include /* @@ -50,7 +49,7 @@ struct SegmentIntersection { else return false; } - + /** * We will find direction of line here respect to @first_point. * Here @second_point and @third_point is first and second points @@ -65,7 +64,7 @@ struct SegmentIntersection { 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)); } - + /** * This method determines whether a point known to be colinear * with a segment lies on that segment. From 1540a6cbb4f9e3509ac264d3e2de1f7680779353 Mon Sep 17 00:00:00 2001 From: Sajied Shah Yousuf <40203390+meSajied@users.noreply.github.com> Date: Sat, 30 May 2020 20:54:38 +0600 Subject: [PATCH 12/12] fix: documentation issue --- geometry/line_segment_intersection.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/geometry/line_segment_intersection.cpp b/geometry/line_segment_intersection.cpp index 5b515c23d01..0b5b858d747 100644 --- a/geometry/line_segment_intersection.cpp +++ b/geometry/line_segment_intersection.cpp @@ -5,7 +5,7 @@ */ #include -/* +/** * Define a Point. */ struct Point { @@ -82,7 +82,7 @@ struct SegmentIntersection { } }; -/* +/** * This is the main function to test whether the algorithm is * working well. */