• Android线段与矩形碰撞检测函数


    原理不明白。。网上找的代码。测试通过,记录一下。

        /**
        * <p> 判断线段是否在矩形内
        * <p> 
        * 先看线段所在直线是否与矩形相交,
        * 如果不相交则返回false,
        * 如果相交,
        * 则看线段的两个点是否在矩形的同一边(即两点的x(y)坐标都比矩形的小x(y)坐标小,或者大),
        * 若在同一边则返回false,
        * 否则就是相交的情况。
        * </p>
        * 
        * @param linePointX1 线段起始点x坐标
        * @param linePointY1 线段起始点y坐标
        * @param linePointX2 线段结束点x坐标
        * @param linePointY2 线段结束点y坐标
        * @param rectangleLeftTopX 矩形左上点x坐标
        * @param rectangleLeftTopY 矩形左上点y坐标
        * @param rectangleRightBottomX 矩形右下点x坐标
        * @param rectangleRightBottomY 矩形右下点y坐标
        * @return 是否相交
        */
        private boolean isLineIntersectRectangle(int linePointX1,int linePointY1,int linePointX2,int linePointY2,int rectangleLeftTopX,int rectangleLeftTopY,int rectangleRightBottomX,int rectangleRightBottomY) {
    
        int lineHeight = linePointY1 - linePointY2;
        int lineWidth = linePointX2 - linePointX1;
        // 计算叉乘
        int c = linePointX1 * linePointY2 - linePointX2 * linePointY1;
    
        if ((lineHeight * rectangleLeftTopX + lineWidth * rectangleLeftTopY + c >= 0 && lineHeight * rectangleRightBottomX + lineWidth * rectangleRightBottomY + c <= 0)
        || (lineHeight * rectangleLeftTopX + lineWidth * rectangleLeftTopY + c <= 0 && lineHeight * rectangleRightBottomX + lineWidth * rectangleRightBottomY + c >= 0)
        || (lineHeight * rectangleLeftTopX + lineWidth * rectangleRightBottomY + c >= 0 && lineHeight * rectangleRightBottomX + lineWidth * rectangleLeftTopY + c <= 0)
        || (lineHeight * rectangleLeftTopX + lineWidth * rectangleRightBottomY + c <= 0 && lineHeight * rectangleRightBottomX + lineWidth * rectangleLeftTopY + c >= 0)) {
                if (rectangleLeftTopX > rectangleRightBottomX) {
                    int temp = rectangleLeftTopX;
                    rectangleLeftTopX = rectangleRightBottomX;
                    rectangleRightBottomX = temp;
                }
                if (rectangleLeftTopY < rectangleRightBottomY) {
                    int temp = rectangleLeftTopY;
                    rectangleLeftTopY = rectangleRightBottomY;
                    rectangleRightBottomY = temp;
                }
                if ((linePointX1 < rectangleLeftTopX && linePointX2 < rectangleLeftTopX) 
                || (linePointX1 > rectangleRightBottomX && linePointX2 > rectangleRightBottomX) 
                || (linePointY1 > rectangleLeftTopY && linePointY2 > rectangleLeftTopY) 
                || (linePointY1 < rectangleRightBottomY && linePointY2 < rectangleRightBottomY)) {
                    return false;
                } else {
                    return true;
                }
            } else {
                return false;
            }
        }
  • 相关阅读:
    什么是数据挖掘?
    Oracle 泵导入导出
    如何创建一个 mongo 数据库并为它添加一个认证用户?
    如何提高 windows 的使用效率?--巧用运行命令
    在 vs2017 中使用 C# 7 新特性。
    什么是按引用传递和按值传递?
    Vue、Vuex+Cookie 实现自动登陆 。
    Web.config 灵活配置
    远程终端
    js框架总结
  • 原文地址:https://www.cnblogs.com/qing123/p/2893231.html
Copyright © 2020-2023  润新知