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

Simplification in _onSegment() function. #85

Open
3bhady opened this issue Jun 16, 2019 · 0 comments
Open

Simplification in _onSegment() function. #85

3bhady opened this issue Jun 16, 2019 · 0 comments

Comments

@3bhady
Copy link

3bhady commented Jun 16, 2019

function _onSegment(A,B,p){
// vertical line
if(_almostEqual(A.x, B.x) && _almostEqual(p.x, A.x)){
if(!_almostEqual(p.y, B.y) && !_almostEqual(p.y, A.y) && p.y < Math.max(B.y, A.y) && p.y > Math.min(B.y, A.y)){
return true;
}
else{
return false;
}
}
// horizontal line
if(_almostEqual(A.y, B.y) && _almostEqual(p.y, A.y)){
if(!_almostEqual(p.x, B.x) && !_almostEqual(p.x, A.x) && p.x < Math.max(B.x, A.x) && p.x > Math.min(B.x, A.x)){
return true;
}
else{
return false;
}
}
//range check
if((p.x < A.x && p.x < B.x) || (p.x > A.x && p.x > B.x) || (p.y < A.y && p.y < B.y) || (p.y > A.y && p.y > B.y)){
return false;
}
// exclude end points
if((_almostEqual(p.x, A.x) && _almostEqual(p.y, A.y)) || (_almostEqual(p.x, B.x) && _almostEqual(p.y, B.y))){
return false;
}
var cross = (p.y - A.y) * (B.x - A.x) - (p.x - A.x) * (B.y - A.y);
if(Math.abs(cross) > TOL){
return false;
}
var dot = (p.x - A.x) * (B.x - A.x) + (p.y - A.y)*(B.y - A.y);
if(dot < 0 || _almostEqual(dot, 0)){
return false;
}
var len2 = (B.x - A.x)*(B.x - A.x) + (B.y - A.y)*(B.y - A.y);
if(dot > len2 || _almostEqual(dot, len2)){
return false;
}
return true;
}

I think this can be replaced by a simple one liner :
return _almostEqual( _distance(A,p) + _distance(B,p) , _distance(A, B));
with _distance(A,B) being a function that returns the distance between two points A,B.
I've tested it and it's working fine.

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

No branches or pull requests

1 participant