您的位置:首页 > 编程语言 > C语言/C++

c语言与java语言判断一个点在一个多边形里面

2014-04-25 19:01 309 查看
c代码:

bool pointInPolygon(int polySides,float polyX[],float polyY[],float x,float y) {

int i,j=polySides-1 ;

bool oddNodes=false ;

for (i=0;i<polySides; i++) {

if(polyY[i]<y && polyY[j]>=y

|| polyY[j]<y&& polyY[i]>=y) {

if(polyX[i]+(y-polyY[i])/(polyY[j]-polyY[i])*(polyX[j]-polyX[i])<x) {

oddNodes=!oddNodes;}}

j=i;}

return oddNodes; }

java代码: 引自游戏引擎

/** Checks whether the given point is in the polygon.

* @param polygon The polygon vertices passed as an array

* @param point The point

* @return true if the point is in the polygon */

public static boolean isPointInPolygon (Array<Vector2> polygon, Vector2 point) {

Vector2 lastVertice = polygon.peek();

boolean oddNodes = false;

for (int i = 0; i < polygon.size; i++) {

Vector2 vertice = polygon.get(i);

if ((vertice.y < point.y && lastVertice.y >= point.y) || (lastVertice.y < point.y && vertice.y >= point.y)) {

if (vertice.x + (point.y - vertice.y) / (lastVertice.y - vertice.y) * (lastVertice.x - vertice.x) < point.x) {

oddNodes = !oddNodes;

}

}

lastVertice = vertice;

}

return oddNodes;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐