rohit Sidpara rohit Sidpara - 5 months ago 28
Objective-C Question

How CGRectContainsPoint works?

I have Rect and origin. I will check my origin point is in rect or not.

My Rect is:


aRect = (origin = (x = 0, y = 44), size = (width = 768, height = 306))


My Origin is:


ptOrigin = (x = 201, y = 313)


and i will check Like This

if (!CGRectContainsPoint(aRect, ptOrigin) )
{
[self.scrollView scrollRectToVisible:aRectTextView animated:YES];
}


But it gives the result origin is inside, But my Y = 313 is greater than the Height(306) of Rect then why it gives inside? So i want to know how
CGRectContainsPoint works?

Answer

The logic is as follows: The point is inside iff:

ptOrigin.x > aRect.origin.x and
ptOrigin.y > aRect.origin.y and
ptOrigin.x < aRect.origin.x + aRect.size.width and
ptOrigin.y < aRect.origin.y + aRect.size.height.

In your case that results in

201 > 0 and
313 > 44 and
201 < 0 + 768 and
313 < 44 + 306

Since all 4 are true, the and-ing is true as well.

Comments