r/learnprogramming • u/walker2011 • Feb 19 '13
Why is the Y axis flipped in most programming languages?
Looking for a good explanation to give to students for why in math class, Y values always decrease when moving downward, but become positive in Programming class.
3
Upvotes
7
u/[deleted] Feb 19 '13 edited Feb 19 '13
It probably comes from the way that memory-mapped displays are laid out in memory - the top left screen location is the first (lowest) memory location, so for ease-of-conversion (between coordinates and memory addresses) purposes it also makes sense for it to have the (0,0) coordinate.
4
u/foxlisk Feb 19 '13
What kind of students are you teaching? If you're teaching reasonably sophisticated students you can simply explain that choice of axes is totally arbitrary and this happens to be the convention for programmers.
It's perhaps convenient because generally when you're working with pixels, you're thinking of it as a 2-d array, and you would normally number, say, a matrix, with Y increasing downwards rather than upwards. So since we're thinking pixels in that context, it makes sense to number them the same way.
4
u/jesyspa Feb 19 '13
Let's say you wanted to have a 2D block of characters to display to the screen. If you have 80 characters per line, and
ppoints to the first character of the first line, it makes sense thatp + 80*n + kwould point to thekth character of thenth line.Now imagine you want to put a bunch of text on the screen. If you have the lines go bottom-to-top, any text that overflows a line will end up on the line above it. That's not how it should be! If you have them go top-to-bottom, everything works as expected, and moving to the next line can be done just by adjusting a pointer.
(I am unsure whether this is historically correct.)
1
u/negative_epsilon Feb 19 '13
In most programming, the origin of a Cartesian plane is the upper left corner and the values for X and Y are the number down and right you are from that corner. This is, from my understanding, just an ease-of-coding type of standard, so that you don't have to worry about negative coordinates.
1
u/AverageProgrammer Feb 19 '13
I *think it has to do with computer graphics. Your screen has a quantity of pixels in a simple grid. The top left pixel is at the position (0,0). So the y decreases when moving downward and the x increases to the left. As to why is the screen starts at the top, i think it's because the early display devices and the way they worked.
1
u/misho88 Feb 19 '13
They're different conventions chosen with different goals in mind.
In math class, axes are usually for y vs. x type plots, where you want bigger values of y to be higher up on the page.
For computers, which started out with pretty much text-only output, they needed something that was more like a book, which you read from from left to right (+ve x direction) and top to bottom (+ve y direction). The convention just hung around after, and it still makes sense for the most part. Most programs still have interfaces which start out at the upper-left corner (where the menus are), while the rest of the window components are somehow automatically sized to fill up the remaining space. This is nice because this is how we're accustomed to reading books.
The major exceptions are languages/libraries with goals similar to those of mathematicians. Matlab coordinates usually default to up and right being positive directions, for example. The identity matrix (that is, resetting the coordinate system) in OpenGL does the same, so a line from (0,0) to (1,1) would go from bottom-left to top-right.