separating axis test——分离轴测试算法的实现

iOS开发判断两个旋转后的矩形是否相交

开发过程中需要解决任意操作的两个任意旋转的矩形相交的问题,面向google编程后发现新的理论知识:separating axis test。
简单了解后实现了该功能。感谢强大的数学知识。

理论知识:

separating axis test——分离轴测试算法的实现_第1张图片

OC实现

// both rects must be specified as all four points, clockwise from top-left point
// their points must already be rotated and specified in global space before passing to this function
- (BOOL)isRectIntersectWithRectA:(NSArray <NSValue *>*)r1 rectB:(NSArray <NSValue*>*)r2 {
    if (!r1 || !r2) {
        NSLog(@"Rects are not accessible");
        return NO;
    }

    NSInteger pn, px;
    for (NSInteger pi = 0, pl = r1.count; pi < pl; pi += 1) {
        pn = (pi == (pl - 1)) ? 0 : pi + 1; // next point
        px = (pn == (pl - 1)) ? 0 : pn + 1;
        
        CGPoint pointPi = [r1[pi] CGPointValue];
        CGPoint pointPn = [r1[pn] CGPointValue];
        CGPoint pointPx = [r1[px] CGPointValue];
        
        BOOL result = [self edgeTestWithPoint1:pointPi point2:pointPn point3:pointPx rect2:r2];
        if (result) {
            return NO;
        }
    }
        
    for (NSInteger pi = 0, pl = r2.count; pi < pl; pi += 1) {
        pn = (pi == (pl - 1)) ? 0 : pi + 1; // next point
        px = (pn == (pl - 1)) ? 0 : pn + 1;
        
        CGPoint pointPi = [r2[pi] CGPointValue];
        CGPoint pointPn = [r2[pn] CGPointValue];
        CGPoint pointPx = [r2[px] CGPointValue];
        
        BOOL result = [self edgeTestWithPoint1:pointPi point2:pointPn point3:pointPx rect2:r1];
        if (result) {
            return NO;
        }
    }

    return YES;
}

-(BOOL)edgeTestWithPoint1:(CGPoint)p1 point2:(CGPoint)p2 point3:(CGPoint)p3 rect2:(NSArray <NSValue *>*)r2 {
    CGPoint rot = CGPointMake(-(p2.y - p1.y), p2.x - p1.x);
    
    BOOL ref = (rot.x * (p3.x - p1.x) + rot.y * (p3.y - p1.y)) >= 0;

    for (NSInteger i=0; i<r2.count; i++) {
        CGPoint point = [r2[i] CGPointValue];
        if ((rot.x * (point.x-p1.x) + rot.y * (point.y-p1.y)) >= 0 == ref) {
            return NO;
        }
    }

    return YES;
}

参考资源:

  • js实现版本
  • stackOverflow

你可能感兴趣的:(iOS开发,数据结构与算法,ios)