Skip to content
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

Feature point in polygon #57

Merged
merged 3 commits into from
May 9, 2019
Merged

Feature point in polygon #57

merged 3 commits into from
May 9, 2019

Conversation

LukasWikander
Copy link
Collaborator

Implemented ray casting algorithm for checking if a point is in a polygon

@LukasWikander LukasWikander requested a review from a team May 8, 2019 10:26
Copy link
Contributor

@viktorjo viktorjo left a comment

Choose a reason for hiding this comment

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

Looks good! I have some comments in the good which could be good to look at.

Did you use any particular reference when you made this implementation? Is it possible to add this to the comments in the code if this is the case?

}

/*!
* \brief rayFromPointIntersectsLine checks whether ray in X axis direction from point intersects
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mean a ray that starts in an (x,y) coordinate and travels in positive X-direction, parallell to the x-axis?


// Compare the slopes of the lines AB and AP - if the slope of AB is larger than that of AP
// then a ray in X axis direction will not intersect AB
if (linePointAX != linePointBX)
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be a bit nitpicky but if the line is vertical, but wouldn't the previous two if-statements have returned a value already? To check this, assume that linePointAX = linePointBX, then it follows that minVal = maxVal which both of the two previous if-statements cover. pointX will always be less than, equal or bigger than minVal.

Copy link
Collaborator Author

@LukasWikander LukasWikander May 8, 2019

Choose a reason for hiding this comment

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

I agree, but I would rather have divide-by-zero be explicitly handled so that the code is crystal clear to someone reading it. The machine code will be the same as if I had removed it since the compiler optimises away this check.

}

// If the first and last points are different, the polygon segment between them must also be included
if (verticesX[0] != verticesX[nPtsInPolygon-1] || verticesY[0] != verticesY[nPtsInPolygon-1])
Copy link
Contributor

Choose a reason for hiding this comment

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

It is in general hard to compare doubles, especially if you want to check that they are equal to each other. Usually you would check that the difference is small enough to count as zero. This could look something like

abs(x-y) < epsilon

where epsilon is some really small number.

Addressed comments
@LukasWikander LukasWikander requested a review from viktorjo May 8, 2019 13:55
@LukasWikander LukasWikander merged commit c093012 into dev May 9, 2019
@LukasWikander LukasWikander deleted the feature_pointInPolygon branch May 9, 2019 07:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants