两个矩形交集 [英] Two Rectangles intersection

查看:215
本文介绍了两个矩形交集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个矩形的特点是4个值每个:

左侧位置 X ,榜首的位置,宽度是W 和高度 ^ h

  X1,Y1,H1,W1
X2,Y2,H 2,W2
 

矩形不旋转,像这样:

  + --------------------> X轴
|
| (X,Y)(X + W,Y)
| + -------------- +
| | |
| | |
| | |
| + -------------- +
V(x,y + H)(X + W,Y + H)

Y轴
 

什么是确定的两个矩形的交集是否为空或不是最好的解决办法?

解决方案

 如果(X1 + W1< X2或X2 + W2< X1或Y1 + H1< Y2或Y2 + H2< Y1):
    路口=空
其他:
    路口=非空
 

如果你有四个坐标 - ((X,Y),(A,B))((X1,Y1),( A1,B1)) - 而不是两个及宽度和高度,它是这样的:

 如果(A< X1或A1中X或B< Y1或B1< Y):
    路口=空
其他:
    路口=非空
 

I have two rectangles characterized by 4 values each :

Left position X, top position Y, width W and height H:

X1, Y1, H1, W1
X2, Y2, H2, W2

Rectangles are not rotated, like so:

+--------------------> X axis
|
|    (X,Y)      (X+W, Y)
|    +--------------+
|    |              |
|    |              |
|    |              |
|    +--------------+
v    (X, Y+H)     (X+W,Y+H)

Y axis

What is the best solution to determine whether the intersection of the two rectangles is empty or not?

解决方案

if (X1+W1<X2 or X2+W2<X1 or Y1+H1<Y2 or Y2+H2<Y1):
    Intersection = Empty
else:
    Intersection = Not Empty

If you have four coordinates – ((X,Y),(A,B)) and ((X1,Y1),(A1,B1)) – rather than two plus width and height, it would look like this:

if (A<X1 or A1<X or B<Y1 or B1<Y):
    Intersection = Empty
else:
    Intersection = Not Empty

这篇关于两个矩形交集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆