boolean isCollisionPolygon(int[] xPoints, int[] yPoints, int posx, int posy) { int i, j; int y; int miny, maxy; int x1, y1; int x2, y2; int ind1, ind2; int ints; int[] polyInts; int nPoints = xPoints.length; if (nPoints < 3) { return false; } polyInts = new int[nPoints]; miny = yPoints[0]; maxy = yPoints[0]; for (i = 1; i < nPoints; i++) { if (yPoints[i] < miny) { miny = yPoints[i]; } else if (yPoints[i] > maxy) { maxy = yPoints[i]; } } for (y = miny; y <= maxy; y++) { ints = 0; for (i = 0; i < nPoints; i++) { if (i == 0) { ind1 = nPoints - 1; ind2 = 0; } else { ind1 = i - 1; ind2 = i; } y1 = yPoints[ind1]; y2 = yPoints[ind2]; if (y1 < y2) { x1 = xPoints[ind1]; x2 = xPoints[ind2]; } else if (y1 > y2) { y2 = yPoints[ind1]; y1 = yPoints[ind2]; x2 = xPoints[ind1]; x1 = xPoints[ind2]; } else { continue; } if ( (y >= y1) && (y < y2)) { polyInts[ints++] = (y - y1) * (x2 - x1) / (y2 - y1) + x1; } else if ( (y == maxy) && (y > y1) && (y <= y2)) { polyInts[ints++] = (y - y1) * (x2 - x1) / (y2 - y1) + x1; } } for (i = polyInts.length; --i >= 0; ) { for (j = 0; j < i; j++) { if (polyInts[j] > polyInts[j + 1]) { int T = polyInts[j]; polyInts[j] = polyInts[j + 1]; polyInts[j + 1] = T; } } } for (i = 0; i < ints; i += 2) { if (posy == y) { if (posx >= polyInts[i] && posx <= polyInts[i + 1]) { return true; } } } } return false; }
boolean isCollisionPolygon(int[] xPoints, int[] yPoints, int posx, int posy)使用方法.xPoints,是多边形的X坐标,yPoints是Y坐标posx,posy是那个要检测点的坐标