- Home /
Rect.ymax wrong?
Hello.
I got a simple problem:
I start by creating a new rectangle in (0, 0) with a width and height of 100.
Now, according to this, the y-coordinate is the top of the rectangle.
So if the top is 0 and it's height is 100, the bottom coordinate should be (-100), right?
Well, not according to Rect.yMax, which says the coordinate is (100) (positive).
But according to this, the yMax property should return the bottom coordinate.
So is this a bug or am i misunderstanding something?
Answer by Kleptomaniac · Mar 02, 2012 at 02:47 PM
I'm not an expert on screen coordinates, but I believe in computing systems the screen origin (0,0) is always in the top-left corner of the screen. From that origin point, the coordinates at the very bottom of the screen and the very right of the screen are accounted for as being positive. This means that despite what you might think about normal coordinate geometry, as we go down the screen the y-coordinates become larger as opposed to becoming smaller as you assumed they should have.
Conversely, anything above what the maximum y coordinate of the screen is becomes negative and anything further to the left (x-axis) than the dimensions of the screen allow becomes negative. So, if your rectangle had coordinates of (-100, -100), it would be completely off the screen.
So, in fact, in this case (your rectangle being drawn at the origin point of the screen), the bottom coordinate should be positive 100 as Rect.yMax reveals.
I hope that made some element of sense ... if not, hopefully I'll be able to elaborate a bit better!
Klep
Doh!
I actually thought about that, but im not sure why my head refused to accept that. I think i've had a brain-fart.
Thanks for the answer!
The origin depends on what coordinate system you're using. GUI space has 0,0 at the top-left, screen space has 0,0 at the bottom-left.
Exactly, but the Rect struct always act the same way. The rect actually stores m_X$$anonymous$$in, m_Y$$anonymous$$in, m_Width and m_Height. Nothing more. All other properties are calculated out of these values. x$$anonymous$$in and x just return m_X$$anonymous$$in. x$$anonymous$$ax return m_X$$anonymous$$in + m_Width.
The difference in x and x$$anonymous$$in is when you set them. x just sets directly m_X$$anonymous$$in so m_Width stay the same. If you set x$$anonymous$$in it will make sure that x$$anonymous$$ax will stay the same. Therefore m_Width is changed.
Because Rect is used in may different cases they deprecated left, right, top and bottom because it's misleading.
I really don't think the unity documentation explains this well at all. That rect documentation with the y$$anonymous$$ax at the top is very misleading imho.
Your answer
![](https://koobas.hobune.stream/wayback/20220613064700im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity UI Rect 0 Answers
Problems with Rect 1 Answer
Is there a way to color in a rect? 1 Answer
Why can't GUI.Box have a width of 5 or less? 1 Answer
Bounds vs. Rectangle 1 Answer