android 线段与矩形碰撞检测

来源:互联网 发布:js event.target 编辑:程序博客网 时间:2022/01/24 05:57
/** * 线段与矩形碰撞检测*  * @param x1 线段一端点x坐标* @param y1 线段一端点y坐标* @param x2 线段二端点x坐标* @param y2 线段二端点y坐标* @param x 矩形左上角x坐标* @param y 矩形左上角y坐标* @param width 矩形宽度* @param height 矩形高度* @return */1.2.public static boolean lineWidthRect(int x1, int y1, int x2, int y2, int x, int y, int width, int height) {3.double a =(double) ((y1 - y2)) / (double) ((x1 - x2));4.double b =y1 - a * x1;5.if (x1 < x2) {6.for (int i =x1; i <= x2; i++) {7.int tempY =(int) (a * i + b);8.if (intersectRect(i, tempY, 1, 1, x, y, width, height)) {9.return true;10.}11.}12.} else if (x1 > x2) {13.for (int i =x1; i >= x2; i--) {14.int tempY =(int) (a * i + b);15.if (intersectRect(i, tempY, 1, 1, x, y, width, height)) {16.return true;17.}18.}19.} else {20.if (y1 <= y2) {21.for (int i =y1; i < y2; i++) {22.if (intersectRect(x1, i, 1, 1, x, y, width, height)) {23.return true;24.}25.}26.} else {27.for (int i =y2; i > y2; i--) {28.if (intersectRect(x1, i, 1, 1, x, y, width, height)) {29.return true;30.}31.}32.}33.}34.return false;35.}

0 0