|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * @brief check whether two line segments intersect each other |
| 4 | + * or not. |
| 5 | + */ |
| 6 | +#include <iostream> |
| 7 | + |
| 8 | +/** |
| 9 | + * Define a Point. |
| 10 | + */ |
| 11 | +struct Point { |
| 12 | + int x; /// Point respect to x coordinate |
| 13 | + int y; /// Point respect to y coordinate |
| 14 | +}; |
| 15 | + |
| 16 | +/** |
| 17 | + * intersect returns true if segments of two line intersects and |
| 18 | + * false if they do not. It calls the subroutines direction |
| 19 | + * which computes the orientation. |
| 20 | + */ |
| 21 | +struct SegmentIntersection { |
| 22 | + inline bool intersect(Point first_point, Point second_point, |
| 23 | + Point third_point, Point forth_point) { |
| 24 | + int direction1 = direction(third_point, forth_point, first_point); |
| 25 | + int direction2 = direction(third_point, forth_point, second_point); |
| 26 | + int direction3 = direction(first_point, second_point, third_point); |
| 27 | + int direction4 = direction(first_point, second_point, forth_point); |
| 28 | + |
| 29 | + if ((direction1 < 0 || direction2 > 0) && (direction3 < 0 || |
| 30 | + direction4 > 0)) |
| 31 | + return true; |
| 32 | + |
| 33 | + else if (direction1 == 0 && on_segment(third_point, forth_point, |
| 34 | + first_point)) |
| 35 | + return true; |
| 36 | + |
| 37 | + else if (direction2 == 0 && on_segment(third_point, forth_point, |
| 38 | + second_point)) |
| 39 | + return true; |
| 40 | + |
| 41 | + else if (direction3 == 0 && on_segment(first_point, second_point, |
| 42 | + third_point)) |
| 43 | + return true; |
| 44 | + |
| 45 | + else if (direction3 == 0 && on_segment(first_point, second_point, |
| 46 | + forth_point)) |
| 47 | + return true; |
| 48 | + |
| 49 | + else |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * We will find direction of line here respect to @first_point. |
| 55 | + * Here @second_point and @third_point is first and second points |
| 56 | + * of the line respectively. we want a method to determine which way a |
| 57 | + * given angle these three points turns. If returned number is negative, |
| 58 | + * then the angle is counter-clockwise. That means the line is going to |
| 59 | + * right to left. We will fount angle as clockwise if the method returns |
| 60 | + * positive number. |
| 61 | + */ |
| 62 | + inline int direction(Point first_point, Point second_point, |
| 63 | + Point third_point) { |
| 64 | + return ((third_point.x-first_point.x)*(second_point.y-first_point.y))- |
| 65 | + ((second_point.x-first_point.x) * (third_point.y-first_point.y)); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * This method determines whether a point known to be colinear |
| 70 | + * with a segment lies on that segment. |
| 71 | + */ |
| 72 | + inline bool on_segment(Point first_point, Point second_point, |
| 73 | + Point third_point) { |
| 74 | + if (std::min(first_point.x, second_point.x) <= third_point.x && |
| 75 | + third_point.x <= std::max(first_point.x, second_point.x) && |
| 76 | + std::min(first_point.y, second_point.y) <= third_point.y && |
| 77 | + third_point.y <= std::max(first_point.y, second_point.y)) |
| 78 | + return true; |
| 79 | + |
| 80 | + else |
| 81 | + return false; |
| 82 | + } |
| 83 | +}; |
| 84 | + |
| 85 | +/** |
| 86 | + * This is the main function to test whether the algorithm is |
| 87 | + * working well. |
| 88 | + */ |
| 89 | +int main() { |
| 90 | + SegmentIntersection segment; |
| 91 | + Point first_point, second_point, third_point, forth_point; |
| 92 | + |
| 93 | + std::cin >> first_point.x >> first_point.y; |
| 94 | + std::cin >> second_point.x >> second_point.y; |
| 95 | + std::cin >> third_point.x >> third_point.y; |
| 96 | + std::cin >> forth_point.x >> forth_point.y; |
| 97 | + |
| 98 | + printf("%d", segment.intersect(first_point, second_point, third_point, |
| 99 | + forth_point)); |
| 100 | + std::cout << std::endl; |
| 101 | +} |
0 commit comments