How to figure out if two CGRect intersect? - iphone

In -drawRect: I want to check if the provided rect intersects with anotherRect.
I'm not good at this geometry math stuff and especially at english so what I want to say is:
If you have two sheets of paper on the desk, and one of them covers the other either completely or just a small portion, it is rectsIntersect = YES.
How to check that out for rect and anotherRect?

Use bool CGRectIntersectsRect (CGRect rect1,CGRect rect2);
Swift 3:
public func intersects(_ rect2: CGRect) -> Bool
Example: rect1.intersects(rect2)

Try to use this..
While animating imageview you can get its actual frame by this:
[[obstacle1.layer presentationLayer] frame];
So, it will easily to make collision of two imageview
if (CGRectIntersectsRect([[obstacle1.layer presentationLayer] frame], [[bgImageView.layer
presentationLayer] frame]))
{
NSLog(#"Collision 1");
}

Related

UIGestureRecogniser on a masked UIView?

Is there a way to know if a 'tap' is inside or outside the masked area of a UIView? I'm using CoreGraphics to mask the UIView.
So far my code goes something like this..
- (void)viewDidLoad {
UIGestureRecogniser *r = [[UIGestureRecogniser alloc] initWithTarget:self action:#selector(gestCall:)];
[self addGestureRecogniser:r];
}
- (void)gestCall:(UIGestureRecogniser *)gestRec {
if ("somthing") {
// outside of mask
} else {
// inside of mask
}
}
Thank you.
I've finally found the solution I was looking for. So for the benefit of any one trying to find is a CGPoint is inside any CGPath.
It's simple.
UIBezierPath *p = [UIBezierPath bezierPathWithCGPath:anyCGPath];
BOOL isInPath = [p containsPoint:anyCGPoint];
Basically you need to check the touch coordinate and decide whether is falls into the mask area or not. Override the hitTest:withEvent: and account for the image mask. You can use [[[self layer] presentationLayer] hitTest:aPoint] or [[[self layer] mask] hitTest:aPoint] in your overridden `-[UIView hitTest:withEvent:].
[EDIT]
Check if a user tapped near a CGPath might help to find answer to your question.
[EDIT]
Do following in your Gesture Handler to figure out to process tap or not.
Specify the center of the circle (This would be UIView.Center as CGPoint)
Specify the radius of pie chart
When user tap on the view, get the location as point - CGPoint and calculate point.x*point.x+point.y*point.y (Circle formulae) and this value must be less than or equal to the square of the radius i.e radius*radius. If this condition satisfied then your tap point is inside the circle otherwise outside.
Hope that makes clear.

custom drawing nsview inside nsscrollview

I'm having a problem with custom drawing NSView using as documentView of a NSScrollView.
Here is my drawRect: code:
- (void)drawRect:(NSRect)dirtyRect
{
[[NSColor lightGrayColor] set];
NSRectFill(self.frame); // Fill entire frame
[[NSColor grayColor] set];
[NSBezierPath setDefaultLineWidth:1];
float y = 0.0f;
while (y <= self.frame.size.height) {
[NSBezierPath strokeLineFromPoint:NSMakePoint(0.0f, y) toPoint:NSMakePoint(self.frame.size.width, y)];
y += 50.0f;
}
float x = 0.0f;
while (x <= self.frame.size.width) {
[NSBezierPath strokeLineFromPoint:NSMakePoint(x, 0.0f) toPoint:NSMakePoint(x, self.frame.size.height)];
x += 50.0f;
}
}
Everything is OK when I scroll the view rightward, but I see strange lines when I scroll the view leftward.
I think this is a cached image buffer or something but I couldn't get why this occurs because I fill a rect which covers entire frame.
What causes this problem? How can I solve it? Thanks.
I know this is an old question but I just encountered the same problem and I managed to fix it.
the drawRect method has one argument, a (CGRect) dirtyRect. My mistake was that I used the dirtyRect to check for the boundaries to draw on, just like on iOS. But I discovered that this CGRect is called 'dirty' for a reason. It specifies the region that the OS wants you to redraw. This sometimes isn't the entire frame of the NSView, but a part of it. This allows you to make really fast optimized code that does't draw more than it has to.
My solution was to ignore the dirtyRect and instead simply use self.bounds for drawing.
Basically, you're drawing on top of the old content.
So, essentially, you need to clear it somehow.
One way is
[[NSColor clearColor] set];
NSRectFill(_bounds);
You may want something more precise or not.

How do i find out if a touch is over the frame of another shape?

I know how to get the position touched, so now i have a CGPoint with the touched position. What's the best way to find out if the touch is over a UIView or not? I know of the method of:
if touchpoint.x > frame.origin.x && touchpoint.x < frame.size.width + frame.origin.x
etc., but is this the best way of going about it?
If you simply want to know if a point is inside the bounds of a view, you can use the pointInside:withEvent: method.
CGPoint touchPoint = [theTouch locationInView:theView];
// If the point was retrieved for a different view, it must be converted to the coordinate space of the destination view using convertPoint:fromView:
if([theView pointInside:touchPoint withEvent:nil]) {
NSLog(#"point inside");
}
Ugh's answer covers views, which I believe is what you want; if you have an arbitrary CGRect, you can use CGRectContainsPoint:
BOOL isInside = CGRectContainsPoint(myRect, touchPoint);
Yes, CGRectContainsPoint will save you from writing so many comparision equations.

sample code for collision detection in iPhone sdk

Can you please suggest sample code for collision detection of two imageviews.
Thanks in advance.
You can Detect Collision between two images by making rect for those image views.
Consider my image-views being named: img_view1 and img_view2.
Image-views creation:
//For img_view1 rect
//parameters are x,y,width,height
CGRect image_rect1 = CGRectMake(img_view1.position.x,img_view1.position.y,100,100);
//For img_view2 rect
//parameters are x,y,width,height
CGRect image_rect2 = CGRectMake(img_view2.position.x,img_view2.position.y,100,100);
Collision detection:
if(CGRectIntersectsRect(image_rect1, image_rect2))
{
NSLog(#"Rect is Intersecting");
}
Nice answere #Anish, however you dont really need to create a new CGRect for the views as you can simply use their respective frame properties.
If you want to put that logic in a method it would look like this:
-(BOOL)viewsDoCollide:(UIView *)view1 :(UIView *)view2{
if(CGRectIntersectsRect(view1.frame, view2.frame))
{
return YES;
}
return NO;
}
Simply pass the two views you want to test into this method, and check the output result.

How to get UIView's Position in DrawRect method

I have 4 views and i am drawing circles inside these views.The user is able to move these views.How can i get the position of each view?
If your views are instance variables in your class you can use view.frame or view.center but your question is quite low on details so I'm just shooting in the dark.
CGPoint origin = view.origin;
CGPoint center = view.center;
CGRect frame = view.frame;
All these things can be used to get what you want.