-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
41df377
feat: added a new algorithm to find whether two line segment intersec…
meSajied f1cd848
fix: cpplint issues
meSajied 56ea92e
added documentation.....
meSajied 56ded89
added documentation....
meSajied 1ecd631
added documentation...
meSajied e66c5a8
added doxygen format....
meSajied 84531fb
fix: comment blocks...
meSajied 6f3923d
fix: cpplint issue
meSajied 3e15e82
fix: cpplint issues...
meSajied 165a30c
fix: comment spaces...
meSajied 8243864
fix: additional spaces...
meSajied 1540a6c
fix: documentation issue
meSajied File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* @file | ||
* @brief check whether two line segments intersect each other | ||
* or not. | ||
*/ | ||
#include <iostream> | ||
|
||
/** | ||
* Define a Point. | ||
*/ | ||
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 | ||
* which computes the orientation. | ||
*/ | ||
struct SegmentIntersection { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this struct is undocumented |
||
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 ((direction1 < 0 || direction2 > 0) && (direction3 < 0 || | ||
direction4 > 0)) | ||
return true; | ||
|
||
else if (direction1 == 0 && on_segment(third_point, forth_point, | ||
first_point)) | ||
return true; | ||
|
||
else if (direction2 == 0 && on_segment(third_point, forth_point, | ||
second_point)) | ||
return true; | ||
|
||
else if (direction3 == 0 && on_segment(first_point, second_point, | ||
third_point)) | ||
return true; | ||
|
||
else if (direction3 == 0 && on_segment(first_point, second_point, | ||
forth_point)) | ||
return true; | ||
|
||
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) { | ||
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) { | ||
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; | ||
} | ||
}; | ||
|
||
/** | ||
* This is the main function to test whether the algorithm is | ||
* working well. | ||
*/ | ||
int main() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. document the main function. a simple one-liner is good |
||
SegmentIntersection segment; | ||
Point first_point, second_point, third_point, forth_point; | ||
|
||
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; | ||
|
||
printf("%d", segment.intersect(first_point, second_point, third_point, | ||
forth_point)); | ||
std::cout << std::endl; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
struct is undocumented.
you can add a brief comment like so:
others/smallest_circle