9

According to the Apple documentation,

CGRectMinY(CGRect) rect returns the y coordinate of the top right corner of the specified rectangle

Shouldn't it be the bottom left corner of the rectangle? Because I think the Y-axis is downwards.

random
  • 9,774
  • 10
  • 66
  • 83
tranvutuan
  • 6,089
  • 8
  • 47
  • 83

2 Answers2

16

If this is for iOS I think your function is wrong.

http://developer.apple.com/library/ios/#documentation/GraphicsImaging/Reference/CGGeometry/Reference/reference.html

CGRectGetMinY

Returns the y-coordinate that establishes the bottom edge of a rectangle.

CGFloat CGRectGetMinY ( CGRect rect );

Parameters

rect

The rectangle to examine. 

Return Value

The y-coordinate of the bottom-left corner of the specified rectangle. Availability

Available in iOS 2.0 and later.

Related Sample Code

HeadsUpUI
oalTouch
PhotoScroller
QuartzDemo
SpeakHere

Declared In CGGeometry.h

Edit: To clear the confussion. The "CGRectGetMinY" is a function to be used on CGRect, this means that it will return the result as if it was only considering a rectangle. for example:

// With a self created element

CGRect rect = CGRectMake(0, 429, 100, 44);

NSLog(@"rect.origin.y: %f",rect.origin.y);
NSLog(@"rect.size.height: %f",rect.size.height);
NSLog(@"CGRectGetMinY(rect): %f",CGRectGetMinY(rect));
NSLog(@"CGRectGetMaxY(rect): %f",CGRectGetMaxY(rect));

returns

2012-06-04 10:55:49.737 test[7613:707] rect.origin.y: 429.000000
2012-06-04 10:55:49.739 test[7613:707] rect.size.height: 44.000000
2012-06-04 10:55:49.741 test[7613:707] CGRectGetMinY(rect): 429.000000
2012-06-04 10:55:49.748 test[7613:707] CGRectGetMaxY(rect): 473.000000

The key is to JUST consider this, if you ask for the min you will get a lower value than if you ask for the max. Even if you think about this without using the ios coordinate system.

NOW the ios IS inverted, so you have to consider this, the previous function will work as well, but visually speaking the result is inverted because the system is inverted.

// With an element on the screen

NSLog(@"gpsButton.frame.origin.y: %f",gpsButton.frame.origin.y);
NSLog(@"gpsButton.frame.size.height: %f",gpsButton.frame.size.height);
NSLog(@"CGRectGetMinY(gpsButton.frame): %f",CGRectGetMinY(gpsButton.frame));
NSLog(@"CGRectGetMaxY(gpsButton.frame): %f",CGRectGetMaxY(gpsButton.frame));

returns

2012-06-04 10:55:49.725 test[7613:707] gpsButton.frame.origin.y: 429.000000
2012-06-04 10:55:49.727 test[7613:707] gpsButton.frame.size.height: 44.000000
2012-06-04 10:55:49.732 test[7613:707] CGRectGetMinY(gpsButton.frame): 429.000000
2012-06-04 10:55:49.735 test[7613:707] CGRectGetMaxY(gpsButton.frame): 473.000000

For a person who sees this, there is also nothing wrong, min is less than max.

So the confusion is in the ios inverted system, since you want to retrieve a visually less value from an inverted system.

This is why it seems weird, the description in CGGeometry is made for the "human world". Not for ios inverted system.

Pochi
  • 13,391
  • 3
  • 64
  • 104
  • 1
    wow, now i am confused. it looks like the questioner misquoted the docs, but the misquote made sense to me. your quote appears accurate, but now i have the same question as the questioner! why would minY refer to the bottom when the CGRect coordinate system (like view.bounds) has an origin at the upper left. min is closer to the origin, right? and the origin is at the top right? shouldn't min be closer to the top? – danh Jun 04 '12 at 01:15
  • 2
    Because this is a convenience method for humans, so even when the ios coordinate system is backwards, a human would usually want the point closer to the ground. And also due to the fact that if you want the top one you just have to frame/bounds.origin.y contrary to getting the lower one in where you would have to frame/bounds.origin.y + frame/bounds.size.height – Pochi Jun 04 '12 at 01:20
  • @LuisOscar : oops i quoted the wrong one. It is my bad. If the y is downward ( equivalent to the origin at upper left ), should `CGRectGetMinY` is at top right and `CGRectGetMax` is at bottom left – tranvutuan Jun 04 '12 at 01:25
  • 1
    I'm still confused (along with @ttran). If the humans were talking about are developers, then this doesn't seem convenient at all. As a human programmer, asked to decide if a point is above a rectangle, I would expect "point.y < CGRectGetMinY(rect)" to work. I think you are saying (well supported by the docs) that that's wrong? – danh Jun 04 '12 at 01:29
  • hey after starring at the picture drawn by my self.... I configured it out..It is kinda tricky....this [link](http://soulwithmobiletechnology.blogspot.ca/2011/08/iphone-find-cgrect-coordinates.html) should be useful after all... – tranvutuan Jun 04 '12 at 01:58
  • @danh read the edit, and ttran sorry for the delay in the edit. – Pochi Jun 04 '12 at 02:10
  • @ttran yes its just a matter of the description and considering the ios inverted system, alot of the descriptions are not ment for an inverted system. – Pochi Jun 04 '12 at 02:12
0

Y axis does point downward, starting from the top left point in a view. So smaller values of y are closer to the top of the view.

danh
  • 62,181
  • 10
  • 95
  • 136