distance from point within a polygon to polygon edge

若如初见. 提交于 2019-12-01 21:39:54
Paul Sonier

Well, this really does depend on a couple of things; specifically, which edge do you want? Do you want to find the nearest edge, or do you have some other criteria that you want to select an edge by (for example, cardinal direction)?

If you want to find the nearest edge, you basically want to iterate across all of the line segments the polygon defines, doing a line-segment-to-point distance calculation; this will find your distance. There's a good implementation of the algorithm in Python on this question, and there's some good description of the algorithms there.

Here is some code that output the distance from a point to an edge, wether the polygon is convex or not, CCW or not. You'll have to test for all your polygons' edges. It might be a little slow for a large set of edges.

- (double) distanceFromPoint:(yourPoint)testPoint
{

double pointX = edgePointB.x - edgePointA.x;
double pointY = edgePointB.y - edgePointA.y;

double k = pointX * pointX + pointY * pointY;
double u = ((testPoint.x - edgePointA.x) * pointX + (edgePointA.y - edgePointA.y) * pointY) / k;

if (u > 1)
    u = 1;
else if (u < 0)
    u = 0;

double x = edgePointA.x + (u * pointX);
double y = edgePointA.y + (u * pointY);

double dx = x - testPoint.x;
double dy = y - testPoint.y;

return sqrt((dx * dx) + (dy * dy));

}

if you aren't sure that the point is within the outer polygon, test that first. Then, to test for the distance to closest forest edge, you could try something like this:

http://www.bdcc.co.uk/Gmaps/BdccGeo.js

Google has a wealth of results for 'distance from point to polygon edge'

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!