相关疑难解决方法(0)

检测两个矩形交叉的算法?

我正在寻找一种算法来检测两个矩形是否相交(一个是任意角度,另一个只有垂直/水平线).

测试一个角落是否在另一个ALMOST中.如果矩形形成十字形状,则失败.

避免使用线条的斜率似乎是一个好主意,这需要垂直线条的特殊情况.

algorithm math graphics geometry separating-axis-theorem

140
推荐指数
2
解决办法
9万
查看次数

确定裁剪矩形是否完全包含在旋转的 UIView 中

前提:我正在构建一个裁剪工具,可以处理图像的两指任意旋转以及任意裁剪。

有时,图像最终会以插入空白空间的方式旋转,以填充旋转图像和裁剪矩形之间的间隙(请参阅下面的示例)。

我需要确保图像视图在旋转时完全适合裁剪矩形。如果没有,我需要重新转换图像(缩放)以使其适合裁剪范围。

使用这个答案,我实现了检查旋转的 UIImageView 是否与裁剪 CGRect 相交的功能,但不幸的是,这并没有告诉我裁剪矩形是否完全包含在旋转的图像视图中。希望我可以对此答案进行一些简单的修改?

OK 的直观示例:

在此输入图像描述

不好,我需要检测和处理:

在此输入图像描述

更新:不起作用的方法

- (BOOL)rotatedView:(UIView*)rotatedView containsViewCompletely:(UIView*)containedView {

    CGRect rotatedBounds = rotatedView.bounds;
    CGPoint polyContainedView[4];

    polyContainedView[0] = [containedView convertPoint:rotatedBounds.origin toView:rotatedView];
    polyContainedView[1] = [containedView convertPoint:CGPointMake(rotatedBounds.origin.x + rotatedBounds.size.width, rotatedBounds.origin.y) toView:rotatedView];
    polyContainedView[2] = [containedView convertPoint:CGPointMake(rotatedBounds.origin.x + rotatedBounds.size.width, rotatedBounds.origin.y + rotatedBounds.size.height) toView:rotatedView];
    polyContainedView[3] = [containedView convertPoint:CGPointMake(rotatedBounds.origin.x, rotatedBounds.origin.y + rotatedBounds.size.height) toView:rotatedView];

    if (CGRectContainsPoint(rotatedView.bounds, polyContainedView[0]) &&
        CGRectContainsPoint(rotatedView.bounds, polyContainedView[1]) &&
        CGRectContainsPoint(rotatedView.bounds, polyContainedView[2]) &&
        CGRectContainsPoint(rotatedView.bounds, polyContainedView[3]))
        return YES;
    else
        return NO;
}
Run Code Online (Sandbox Code Playgroud)

objective-c cgaffinetransform ios separating-axis-theorem

4
推荐指数
1
解决办法
1031
查看次数