游戏中判断一群点可否构成凸多边形

下面的代码需要box2d!!!!!!!!!!!

import org.jbox2d.common.Vec2;

public class PolyonUtil {
 private static PolyonUtil instance;
 private float a;
 private float b;
 private boolean x_axis;
 private boolean y_axis;
 private float axis;
 float i;

 public static PolyonUtil getInstance() {
  if (instance == null) {
   instance = new PolyonUtil();
  }
  return instance;
 }

 public void setAB(Vec2 point1, Vec2 point2) {
  if (point1.x == point2.x) {
   if (point1.y != point2.y) {
    y_axis = true;
    axis = point1.x;
   }
  } else if (point1.y == point2.y) {
   if (point1.x != point2.x) {
    x_axis = true;
    axis = point1.y;
   }
  } else {
   y_axis = false;
   x_axis = false;
   a = (point1.y - point2.y) / (point1.x - point2.x);
   b = point1.y - point1.x * a;
  }
 }

 public byte getFlag(Vec2 point) {
  if (y_axis) {
   if (point.x > axis) {
    return 1;
   } else if (point.x < axis) {
    return -1;
   } else {
    return 0;
   }

  } else if (x_axis) {
   if (point.y > axis) {
    return 1;
   } else if (point.y < axis) {
    return -1;
   } else {
    return 0;
   }
  } else {
    i = point.y - a * point.x - b;
   if ((point.y - a * point.x - b) < 0) {
    return -1;
   } else if ((point.y - a * point.x - b) > 0) {
    return 1;
   } else {
    return 0;
   }
  }
 }
}

//下面的函数判断一群点是否构成凸多边形

public boolean isHull(Vec2[] mouseStroke){

int mouseStrokeLength=mouseStroke.length;

for (int i = 0; i < mouseStrokeLength; i+=2) {
   PolyonUtil.getInstance().setAB(mouseStroke[(i) % mouseStrokeLength],
     mouseStroke[(i + 1) % mouseStrokeLength]);
   for (int j = 0, k = 0; j < mouseStrokeLength; j++) {
    if (j == i || j == (i + 1)) {
     continue;
    } else {
     flag[k] = PolyonUtil.getInstance().getFlag(mouseStroke[j
       % mouseStrokeLength]);
     k++;
    }
   }
   for (int l = 0; l < flag.length - 1; l++) {
    if (flag[l] * flag[l + 1] < 0) {
     // ao 当是凹多边形时候
          return false;
    }
   }
  }

return true;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值