local fn DoLineSegmentsIntersect( p1 as CGPoint, p2 as CGPoint, p3 as CGPoint, p4 as CGPoint ) as BOOL CGFloat den = (p4.y - p3.y) * (p2.x - p1.x) - (p4.x - p3.x) * (p2.y - p1.y) if ( den == 0 ) then return NO CGFloat ua = ((p4.x - p3.x) * (p1.y - p3.y) - (p4.y - p3.y) * (p1.x - p3.x)) / den CGFloat ub = ((p2.x - p1.x) * (p1.y - p3.y) - (p2.y - p1.y) * (p1.x - p3.x)) / den end fn = (ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1) local fn IsPointInTriangle( p as CGPoint, a as CGPoint, b as CGPoint, c as CGPoint ) as BOOL CGFloat den = ((b.y - c.y) * (a.x - c.x) + (c.x - b.x) * (a.y - c.y)) CGFloat alpha = ((b.y - c.y) * (p.x - c.x) + (c.x - b.x) * (p.y - c.y)) / den CGFloat beta = ((c.y - a.y) * (p.x - c.x) + (a.x - c.x) * (p.y - c.y)) / den CGFloat gamma = 1.0 - alpha - beta end fn = (alpha >= 0 && beta >= 0 && gamma >= 0) local fn DoTrianglesOverlap( t1 as CFArrayRef, t2 as CFArrayRef ) as BOOL long i, j for i = 0 to 2 CGPoint p1 = fn ValuePoint( t1[i] ) CGPoint p2 = fn ValuePoint( t1[(i+1) % 3] ) for j = 0 to 2 CGPoint p3 = fn ValuePoint( t2[j] ) CGPoint p4 = fn ValuePoint( t2[(j+1) % 3] ) if ( fn DoLineSegmentsIntersect( p1, p2, p3, p4 ) ) return YES end if next next for i = 0 to 2 if ( fn IsPointInTriangle( fn ValuePoint( t1[i] ), fn ValuePoint( t2[0] ), fn ValuePoint( t2[1] ), fn ValuePoint( t2[2] ) ) ) return YES end if next for i = 0 to 2 if ( fn IsPointInTriangle( fn ValuePoint( t2[i] ), fn ValuePoint( t1[0] ), fn ValuePoint( t1[1] ), fn ValuePoint( t1[2] ) ) ) return YES end if next end fn = NO void local fn DoIt CFArrayRef t1, t2 t1 = @[@(fn CGPointMake(0,0)),@(fn CGPointMake(5,0)),@(fn CGPointMake(0,5))] t2 = @[@(fn CGPointMake(0,0)),@(fn CGPointMake(5,0)),@(fn CGPointMake(0,6))] print fn DoTrianglesOverlap( t1, t2 ) t1 = @[@(fn CGPointMake(0,0)),@(fn CGPointMake(0,5)),@(fn CGPointMake(5,0))] t2 = @[@(fn CGPointMake(0,0)),@(fn CGPointMake(0,5)),@(fn CGPointMake(5,0))] print fn DoTrianglesOverlap( t1, t2 ) t1 = @[@(fn CGPointMake(0,0)),@(fn CGPointMake(5,0)),@(fn CGPointMake(0,5))] t2 = @[@(fn CGPointMake(-10,0)),@(fn CGPointMake(-5,0)),@(fn CGPointMake(-1,6))] print fn DoTrianglesOverlap( t1, t2 ) t1 = @[@(fn CGPointMake(0,0)),@(fn CGPointMake(5,0)),@(fn CGPointMake(2.5,5))] t2 = @[@(fn CGPointMake(0,4)),@(fn CGPointMake(2.5,-1)),@(fn CGPointMake(5,4))] print fn DoTrianglesOverlap( t1, t2 ) t1 = @[@(fn CGPointMake(0,0)),@(fn CGPointMake(1,1)),@(fn CGPointMake(0,2))] t2 = @[@(fn CGPointMake(2,1)),@(fn CGPointMake(3,0)),@(fn CGPointMake(3,2))] print fn DoTrianglesOverlap( t1, t2 ) t1 = @[@(fn CGPointMake(0,0)),@(fn CGPointMake(1,1)),@(fn CGPointMake(0,2))] t2 = @[@(fn CGPointMake(2,1)),@(fn CGPointMake(3,-2)),@(fn CGPointMake(3,4))] print fn DoTrianglesOverlap( t1, t2 ) t1 = @[@(fn CGPointMake(0,0)),@(fn CGPointMake(1,0)),@(fn CGPointMake(0,1))] t2 = @[@(fn CGPointMake(1,0)),@(fn CGPointMake(2,0)),@(fn CGPointMake(1,1))] print fn DoTrianglesOverlap( t1, t2 ) end fn fn DoIt HandleEvents