【发布时间】:2013-09-29 05:14:57
【问题描述】:
在加载视图控制器时,我正在构建 2 个矩形,使用 CGPath。可以使用PanGestureRecognizer 移动矩形。问题是我怎么知道 2 rects 何时相遇?为了不让它们相交?
MainViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
for (int i=0; i< 2; i++) {
//create rects of CGPath
CGRectCustomView* randomColorRect =
[[CGRectCustomView alloc]initWithFrame:
CGRectMake(<random place on screen>)];
//random angle
randomColorRect.transform =
CGAffineTransformMakeRotation
(DegreesToRadians([Shared randomIntBetween:0 and:360]));
[self.view addSubview:randomColorRect];
}
}
- (BOOL)areRectsCollide {
???How to find this???
}
CGRectCustomView.m:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 8.0);
CGContextStrokePath(context); // do actual stroking
CGContextSetRGBFillColor(context, <green color>, 1);
CGContextFillRect(context, CGRectMake(self.frame.origin.x, self.frame.origin.y, self.frame.size.width, self.frame.size.height));
path = CGContextCopyPath(context);
}
在Apple指南here中,有一个函数可以判断路径是否包含点
- (BOOL)containsPoint:(CGPoint)point onPath:(UIBezierPath *)path inFillArea:(BOOL)inFil,
但我有一个无限多点的矩形。所以我该怎么做?打破我的头......
【问题讨论】:
-
一个矩形有“无限的点数”?这些一定是非常奇怪的矩形......
-
@bummzack 任何线/区域都由无数个点组成。至少在我对几何的理解中。但我可能错了:)
-
噢!你当然是对的。
-
Apple 没有提供任何方法来检查旋转的矩形交点。这个answer 应该可以帮助您实现旋转矩形。碰撞检测。
-
@bummzack,我的天才朋友刚刚说 rect 仅在数学中具有无限点数,在计算机中没有。矩形仅限于屏幕上的像素。实际上,我什至不必检查矩形中的所有点,只需检查其周边即可。在 CGPath 上搜索像素。感觉退出了。
标签: ios objective-c cgpath