判断区域相交的方法

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package KconniePong;
 
// 两个矩形都是平行于X,Y轴,判断是否相交。两种方法,都需要检查特殊情况。
public class RectangleIntersect {
    // 方法一:矩阵在X,Y轴上的投影都在另一矩形投影的一侧,则矩阵必定无交集;否则,有交集。
    boolean isRectIntersect(Rectangle a, Rectangle b) {
        // 需要排除特殊情况:一个矩形在另一个矩形内
        if ((a.top < b.top && a.bottom > b.bottom && a.right < b.right && a.left > b.left) || (b.top < a.top && b.bottom > a.bottom && b.right < a.right && b.left > a.left)) {
            return false;
        }
        if ((a.top < b.bottom && a.right < b.left) || (a.bottom > b.top && a.left > b.right)) {
            return false;
        }
        return true;
    }
 
    // 方法二:若两矩形的中心满足一定的条件,则必定相交
    boolean isRectIntersect2(Rectangle a, Rectangle b) {
        float aCenterX = (a.left + a.right) / 2;
        float aCenterY = (a.bottom + a.top) / 2;
        float bCenterX = (b.left + b.right) / 2;
        float bCenterY = (b.bottom + b.top) / 2;
        float radiusX = (a.right - a.left) / 2 + (b.right - b.left) / 2;
        float radiusY = (a.top - a.bottom) / 2 + (b.top - b.bottom) / 2;
        // 需要排除特殊情况:一个矩形在另一个矩形内
        if ((a.top < b.top && a.bottom > b.bottom && a.right < b.right && a.left > b.left) || (b.top < a.top && b.bottom > a.bottom && b.right < a.right && b.left > a.left)) {
            return false;
        }
        if ((Math.abs(aCenterX - bCenterX) < radiusX) || (Math.abs(aCenterY - bCenterY) < radiusY)) {
            return true;
        }
        return false;
    }
 
    // 若两矩形相交,相交区域必定有:top = min(top),bottom = max(bottom),right =
    // min(right),left = max(left)
    float intersectArea(Rectangle a, Rectangle b) {
        if (isRectIntersect(a, b)) {
            return (Math.min(a.top, b.top) - Math.max(a.bottom, b.bottom)) * (Math.min(a.right, b.right) - Math.max(a.left, b.left));
        }
        return 0;
    }
 
    public static void main(String[] args) {
        Rectangle ra = new Rectangle(0, 3, 0, 6);
        Rectangle rb = new Rectangle(2, 4, 2, 4);
        RectangleIntersect r = new RectangleIntersect();
        if (r.isRectIntersect(ra, rb)) {
            System.out.println("两矩阵相交");
        } else {
            System.out.println("两矩形不相交");
        }
        if (r.isRectIntersect2(ra, rb)) {
            System.out.println("两矩阵相交");
        } else {
            System.out.println("两矩形不相交");
        }
 
        System.out.println("相交区域面积: " + r.intersectArea(ra, rb));
    }
}
 
class Rectangle {
    // 分别表示最上下左右上的坐标
    float left;
    float right;
    float bottom;
    float top;
 
    Rectangle(float left, float right, float bottom, float top) {
        if (left >= right || top <= bottom) {
            System.out.println("矩形坐标初始化错误!");
            return;
        }
        this.left = left;
        this.right = right;
        this.top = top;
        this.bottom = bottom;
    }
}
posted @   学习札记  阅读(828)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 上周热点回顾(1.20-1.26)
· 【译】.NET 升级助手现在支持升级到集中式包管理
点击右上角即可分享
微信分享提示