10

我不知道从哪里开始。显然CGRectIntersectsRect在这种情况下不起作用,你会明白为什么。

我有一个 UIView 的子类,其中有一个 UIImageView,它位于 UIView 的确切中心:

旋转前的 UIView

然后我旋转自定义 UIView 以保持内部 UIImageView 的框架,同时仍然能够执行 CGAffineRotation。生成的框架如下所示:

旋转后的 UIView

我需要防止用户使这些 UIImageViews 相交,但我不知道如何检查两个 UIImageViews 之间的交集,因为它们的框架不仅不适用于父 UIView,而且它们在不影响它们的框架的情况下旋转。

我尝试的唯一结果是不成功。

有任何想法吗?

4

1 回答 1

9

以下算法可用于检查两个(旋转或其他变换的)视图是否重叠:

  • 用于[view convertPoint:point toView:nil]将两个视图的 4 个边界点转换为公共坐标系(窗口坐标)。
  • 转换后的点形成两个凸四边形。
  • 使用SAT(分离轴定理)检查四边形是否相交。

这:http ://www.geometrictools.com/Documentation/MethodOfSeparatingAxes.pdf是包含伪代码的算法的另一种描述,更多可以通过谷歌搜索“分离轴定理”找到。


更新:我试图为“分离轴定理”创建一个 Objective-C 方法,这就是我得到的。到目前为止,我只做了几个测试,所以我希望没有太多错误。

- (BOOL)convexPolygon:(CGPoint *)poly1 count:(int)count1 intersectsWith:(CGPoint *)poly2 count:(int)count2;

测试 2 个凸多边形是否相交。两个多边形都作为CGPoint顶点数组给出。

- (BOOL)view:(UIView *)view1 intersectsWith:(UIView *)view2

测试(如上所述)两个任意视图是否相交。

执行:

- (void)projectionOfPolygon:(CGPoint *)poly count:(int)count onto:(CGPoint)perp min:(CGFloat *)minp max:(CGFloat *)maxp
{
    CGFloat minproj = MAXFLOAT;
    CGFloat maxproj = -MAXFLOAT;
    for (int j = 0; j < count; j++) {
        CGFloat proj = poly[j].x * perp.x + poly[j].y * perp.y;
        if (proj > maxproj)
            maxproj = proj;
        if (proj < minproj)
            minproj = proj;
    }
    *minp = minproj;
    *maxp = maxproj;
}

-(BOOL)convexPolygon:(CGPoint *)poly1 count:(int)count1 intersectsWith:(CGPoint *)poly2 count:(int)count2
{
    for (int i = 0; i < count1; i++) {
        // Perpendicular vector for one edge of poly1:
        CGPoint p1 = poly1[i];
        CGPoint p2 = poly1[(i+1) % count1];
        CGPoint perp = CGPointMake(- (p2.y - p1.y), p2.x - p1.x);

        // Projection intervals of poly1, poly2 onto perpendicular vector:
        CGFloat minp1, maxp1, minp2, maxp2;
        [self projectionOfPolygon:poly1 count:count1 onto:perp min:&minp1 max:&maxp1];
        [self projectionOfPolygon:poly2 count:count1 onto:perp min:&minp2 max:&maxp2];

        // If projections do not overlap then we have a "separating axis"
        // which means that the polygons do not intersect:
        if (maxp1 < minp2 || maxp2 < minp1)
            return NO;
    }

    // And now the other way around with edges from poly2:
    for (int i = 0; i < count2; i++) {
        CGPoint p1 = poly2[i];
        CGPoint p2 = poly2[(i+1) % count2];
        CGPoint perp = CGPointMake(- (p2.y - p1.y), p2.x - p1.x);

        CGFloat minp1, maxp1, minp2, maxp2;
        [self projectionOfPolygon:poly1 count:count1 onto:perp min:&minp1 max:&maxp1];
        [self projectionOfPolygon:poly2 count:count1 onto:perp min:&minp2 max:&maxp2];

        if (maxp1 < minp2 || maxp2 < minp1)
            return NO;
    }

    // No separating axis found, then the polygons must intersect:
    return YES;
}

- (BOOL)view:(UIView *)view1 intersectsWith:(UIView *)view2
{
    CGPoint poly1[4];
    CGRect bounds1 = view1.bounds;
    poly1[0] = [view1 convertPoint:bounds1.origin toView:nil];
    poly1[1] = [view1 convertPoint:CGPointMake(bounds1.origin.x + bounds1.size.width, bounds1.origin.y) toView:nil];
    poly1[2] = [view1 convertPoint:CGPointMake(bounds1.origin.x + bounds1.size.width, bounds1.origin.y + bounds1.size.height) toView:nil];
    poly1[3] = [view1 convertPoint:CGPointMake(bounds1.origin.x, bounds1.origin.y + bounds1.size.height) toView:nil];

    CGPoint poly2[4];
    CGRect bounds2 = view2.bounds;
    poly2[0] = [view2 convertPoint:bounds2.origin toView:nil];
    poly2[1] = [view2 convertPoint:CGPointMake(bounds2.origin.x + bounds2.size.width, bounds2.origin.y) toView:nil];
    poly2[2] = [view2 convertPoint:CGPointMake(bounds2.origin.x + bounds2.size.width, bounds2.origin.y + bounds2.size.height) toView:nil];
    poly2[3] = [view2 convertPoint:CGPointMake(bounds2.origin.x, bounds2.origin.y + bounds2.size.height) toView:nil];

    return [self convexPolygon:poly1 count:4 intersectsWith:poly2 count:4];
}

斯威夫特版本。(通过扩展将此行为添加到 UIView)

extension UIView {

func projection(of polygon: [CGPoint], perpendicularVector: CGPoint) -> (CGFloat, CGFloat) {
    var minproj = CGFloat.greatestFiniteMagnitude
    var maxproj = -CGFloat.greatestFiniteMagnitude
    
    for j in 0..<polygon.count {
        let proj = polygon[j].x * perpendicularVector.x + polygon[j].y * perpendicularVector.y
        if proj > maxproj {
            maxproj = proj
        }
        
        if proj < minproj {
            minproj = proj
        }
    }
    
    return (minproj, maxproj)
}

func convex(polygon: [CGPoint], intersectsWith polygon2: [CGPoint]) -> Bool {
    //
    let count1 = polygon.count
    for i in 0..<count1 {
        let p1 = polygon[i]
        let p2 = polygon[(i+1) % count1]
        let perpendicularVector = CGPoint(x: -(p2.y - p1.y), y: p2.x - p1.x)
        
        let m1 = projection(of: polygon, perpendicularVector: perpendicularVector)
        let minp1 = m1.0
        let maxp1 = m1.1

        let m2 = projection(of: polygon2, perpendicularVector: perpendicularVector)
        let minp2 = m2.0
        let maxp2 = m2.1
        
        if maxp1 < minp2 || maxp2 < minp1 {
            return false
        }
    }
    //
    let count2 = polygon2.count
    for i in 0..<count2 {
        let p1 = polygon2[i]
        let p2 = polygon2[(i+1) % count2]
        let perpendicularVector = CGPoint(x: -(p2.y - p1.y), y: p2.x - p1.x)
        
        let m1 = projection(of: polygon, perpendicularVector: perpendicularVector)
        let minp1 = m1.0
        let maxp1 = m1.1

        let m2 = projection(of: polygon2, perpendicularVector: perpendicularVector)
        let minp2 = m2.0
        let maxp2 = m1.0
        
        if maxp1 < minp2 || maxp2 < minp1 {
            return false
        }
    }
    //
    return true
}

func intersects(with someView: UIView) -> Bool {
    //
    var points1 = [CGPoint]()
    let bounds1 = bounds
    let p11 = convert(bounds1.origin, to: nil)
    let p21 = convert(CGPoint(x: bounds1.origin.x + bounds1.size.width, y: bounds1.origin.y), to: nil)
    let p31 = convert(CGPoint(x: bounds1.origin.x + bounds1.size.width, y: bounds1.origin.y + bounds1.size.height) , to: nil)
    let p41 = convert(CGPoint(x: bounds1.origin.x, y: bounds1.origin.y + bounds1.size.height), to: nil)
    points1.append(p11)
    points1.append(p21)
    points1.append(p31)
    points1.append(p41)
    //
    var points2 = [CGPoint]()
    let bounds2 = someView.bounds
    let p12 = someView.convert(bounds2.origin, to: nil)
    let p22 = someView.convert(CGPoint(x: bounds2.origin.x + bounds2.size.width, y: bounds2.origin.y), to: nil)
    let p32 = someView.convert(CGPoint(x: bounds2.origin.x + bounds2.size.width, y: bounds2.origin.y + bounds2.size.height) , to: nil)
    let p42 = someView.convert(CGPoint(x: bounds2.origin.x, y: bounds2.origin.y + bounds2.size.height), to: nil)
    points2.append(p12)
    points2.append(p22)
    points2.append(p32)
    points2.append(p42)
    //
    return convex(polygon: points1, intersectsWith: points2)
}
于 2013-03-29T21:02:09.583 回答