问题描述
我如何从另一个减去一个 CGRect
?我希望结果 R1 - R2
成为R1中与R2不相交的最大子矩形。
示例1 :
+ ------------------- --------------- +
| + -------- + |
| | R2 | |
| | | |
| + -------- + R1 |
| |
| |
| |
+ ---------------------------------- +
R3 = CGRectSubstract(R2,R1);
+ ------ ---------------- +
| |
| |
| |
| R3 |
| |
| |
| |
+ ---------------------- +
示例2 :
+ ----------------- ------ + ---------- +
| | |
| | R2 |
| | |
| R1 + ---------- +
| |
| |
| |
+ ---------------------------------- +
R3 = CGRectSubstract(R2,R1);
+ ------ ----------------- +
| |
| |
| |
| R3 |
| |
| |
| |
+ ----------------------- +
示例3 :
+ ---------------- ------------------ +
| |
| |
| |
| R1 |
| + --------- + |
| | | |
| | R2 | |
+ --------- + --------- + -------------- +
R3 = CGRectSubstract(R2,R1);
+ ------ ---------------------------- +
| |
| |
| R3 |
| |
+ ---------------------------------- +
您的定义相当含糊,什么说减法是水平的还是垂直的?我推荐使用CGRectIntersection和CGRectDivide的组合,并指定消除歧义的方向。
(未测试,甚至编译)
CGRect rectSubtract(CGRect r1,CGRect r2,CGRectEdge edge){
//查找多少r1重叠r2
CGRect intersection = CGRectIntersection( r1,r2);
//如果它们不相交,则返回r1。如果(CGRectIsNull(intersection)){
return r1;
}
//找出我们砍掉了多少r1
float chopAmount =(edge == CGRectMinXEdge || edge == CGRectMaxXEdge)
? intersection.size.width
:intersection.size.height;
CGRect r3,一次性使用;
//印章
CGRectDivide(r1,& throwaway,& r3,chopAmount,edge);
返回r3;
}
How I can substract one CGRect
from another? I want the result R1 - R2
to be the largest subrectangle of R1 that does not intersect R2.
Example 1:
+----------------------------------+ | +--------+ | | | R2 | | | | | | | +--------+ R1 | | | | | | | +----------------------------------+
R3 = CGRectSubstract(R2,R1);
+----------------------+ | | | | | | | R3 | | | | | | | +----------------------+
Example 2:
+-----------------------+----------+ | | | | | R2 | | | | | R1 +----------+ | | | | | | +----------------------------------+
R3 = CGRectSubstract(R2,R1);
+-----------------------+ | | | | | | | R3 | | | | | | | +-----------------------+
Example 3:
+----------------------------------+ | | | | | | | R1 | | +---------+ | | | | | | | R2 | | +---------+---------+--------------+
R3 = CGRectSubstract(R2,R1);
+----------------------------------+ | | | | | R3 | | | +----------------------------------+
Your definition is fairly ambiguous, what says whether the subtraction is horizontal or vertical? I recommend using a combination of CGRectIntersection and CGRectDivide, along with specifying a direction to remove ambiguity.
(not tested, or even compiled)
CGRect rectSubtract(CGRect r1, CGRect r2, CGRectEdge edge) {
// Find how much r1 overlaps r2
CGRect intersection = CGRectIntersection(r1, r2);
// If they don't intersect, just return r1. No subtraction to be done
if (CGRectIsNull(intersection)) {
return r1;
}
// Figure out how much we chop off r1
float chopAmount = (edge == CGRectMinXEdge || edge == CGRectMaxXEdge)
? intersection.size.width
: intersection.size.height;
CGRect r3, throwaway;
// Chop
CGRectDivide(r1, &throwaway, &r3, chopAmount, edge);
return r3;
}
这篇关于从CGRect减去CGRect - 最大的一个不包含另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!