检查CGRect是否包含在另一个(转换的)rect中 [英] Check if CGRect is contained within another (transformed) rect

查看:175
本文介绍了检查CGRect是否包含在另一个(转换的)rect中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现裁剪功能,并且试图弄清楚如何测试裁剪后的矩形视图中是否完全包含了裁剪矩形。也就是说,图像的裁剪部分应该没有空格。

I'm implementing a cropping feature and I'm trying to figure out how to test whether the crop rectangle is fully contained within the transformed image view. i.e. there should be no whitespace in the cropped portion of the image.

我试图复制此组件中实现的行为: https://github.com/heitorfr/ios-image-editor ,它实现了类似的检查(请参见下文),

I've tried to copy the behavior as implemented in this component: https://github.com/heitorfr/ios-image-editor, which implements a similar check (see below), but I can't get it to work for my situation.

- (void)checkBoundsWithTransform:(CGAffineTransform)transform
{
    CGRect r1 = [self boundingBoxForRect:self.preview.cropRect 
                        rotatedByRadians:[self imageRotation]];
    Rectangle r2 = [self applyTransform:transform 
                                 toRect:self.preview.initialImageFrame];

    CGAffineTransform t = 
     CGAffineTransformMakeTranslation(CGRectGetMidX(self.preview.cropRect), 
                                      CGRectGetMidY(self.preview.cropRect));
    t = CGAffineTransformRotate(t, -[self imageRotation]);
    t = CGAffineTransformTranslate(t, 
                                   -CGRectGetMidX(self.preview.cropRect), -
                                   CGRectGetMidY(self.preview.cropRect));

    Rectangle r3 = [self applyTransform:t toRectangle:r2];

    if(CGRectContainsRect([self CGRectFromRectangle:r3],r1)) {
        self.validTransform = transform;
    }
}


推荐答案

NSBezierPath *path = [NSBezierPath bezierPathWithRect:r2];
[path transformUsingAffineTransform:t];
if([path containsPoint:NSMinX(r1)] 
    && [path containsPoint:NSMinY(r1)] 
    && [path containsPoint:NSMaxX(r1)] 
    && [path containsPoint:(NSMaxY(r1)] ){
    self.validTransform = transform;
}

这篇关于检查CGRect是否包含在另一个(转换的)rect中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆