zoukankan      html  css  js  c++  java
  • 像素级别的多边形碰撞

    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是那个要检测点的坐标
  • 相关阅读:
    使用Python的requests库作接口测试——对HTTP动词的支持
    用Python的requests库作接口测试——对响应进行迭代
    用Python的requests库作接口测试——上传文件
    使用Python的requests库作接口测试——请求对象与响应对象
    Python程序的执行过程
    python Requests模块
    Linux命令CURL用法
    curl命令
    设计的七大原则
    深入理解多态 (1)
  • 原文地址:https://www.cnblogs.com/GameDeveloper/p/2272278.html
Copyright © 2011-2022 走看看