We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Can I invert the y axis so it starts from bottom
Page Index Toggle Pages: 1
Can I invert the y axis so it starts from bottom? (Read 1267 times)
Can I invert the y axis so it starts from bottom?
Apr 8th, 2009, 9:32pm
 
How does someone invert the y axis so it starts from bottom left corner and points up instead of starting from top left corner and points down? Right now all I can think of is to add inversion to every single shape I draw and to mouseY. Any easier way out? Thanks
Re: Can I invert the y axis so it starts from bottom?
Reply #1 - Apr 8th, 2009, 11:30pm
 
This thread discusses how/why of doing that.

http://processing.org/discourse/yabb_beta/YaBB.cgi?board=OpenGL;action=display;num=1206476051
Re: Can I invert the y axis so it starts from bottom?
Reply #2 - Apr 9th, 2009, 6:04am
 
For 2D:
size(200, 200);
background(255);
scale(1, -1);
translate(0, -height);

triangle(0, 0, 58, 200, 200, 75);
triangle(30, 75, 58, 20, 86, 75);
Re: Can I invert the y axis so it starts from bottom?
Reply #3 - Apr 9th, 2009, 6:14am
 
Thank you NoahBuddy and PhiLho. I intend to use my programs as class demos so the y-axis needs to be inverted. Otherwise my students will be confused.
Re: Can I invert the y axis so it starts from bottom?
Reply #4 - Apr 9th, 2009, 9:28am
 
Note that when you flip the Y axis like that, EVERYTHING flips.

Including text, which will appear upside down.

When you want to render right-side-up text, the easiest thing to do is to temporarily translate to the location of the text, scale the y-axis upside-down again, draw the text, and then undo the transformation:

pushMatrix(); // save the current coordinate system
translate(textXPos, textYPos); // move to where you want the text to be
scale(1,-1); // flip things over
text("hello world", 0,0); // draw the text
popMatrix(); // restore the state.
Re: Can I invert the y axis so it starts from bottom?
Reply #5 - Apr 9th, 2009, 9:37am
 
Here's some test results I got:
First, everything is appearing at the correct place according to a right-side-up vertical axis.
Then, the texts are drawn up side down. I tried to add scale(1,1,1) in front of the text but it didn't work. I then added pushMatrix() and popmatrix(). That worked.

A short summary:
pushMatrix() before you scale(1,-1,1)
Do stuff
popMatrix() before you display text.


import processing.opengl.*;
size(640,480,OPENGL);
pushMatrix();
scale(1,-1,1);
translate(tX,tY); // translate however you like.
// Now do stuff that you want in the right-side-up x-y system.
popMatrix();
//Now get back to computer graphics convension and display some text.
text("Blah",20,20);

Re: Can I invert the y axis so it starts from bottom?
Reply #6 - Apr 9th, 2009, 1:57pm
 
See my earlier post, but: you had the right idea, except for one thing.

Scale(), translate(), rotate(), and any other coordinate system transformations are _cumulative_.

So "scale(1,-1)" doesn't mean "make the y-axis go up", it means "flip the y-axis over from whatever it is now".  You can think of this particular scale operation as a toggle.

Similarly, "scale(1,1)" doesn't mean "make the y-axis go down", it means "scale the x- and y- axes by 1.0 times whatever their current basis vectors are".  Which, as you might imagine, has no effect whatsoever.
Re: Can I invert the y axis so it starts from bottom?
Reply #7 - Apr 10th, 2009, 2:46pm
 
Thanks cloister. Now I realize the scale(1,-1) is just an improper rotational matrix instead of a command to demand y to be up. Seems like I did scale(1,1) just to do nothing.

Now, question for you: do you know why the computer graphics seem to be in a left-handed coordinate system? Are the cross products defined with left-hand rule? I bet I can check the Pvector.cross() but seriously if the system is LH I don't know how to do physics in it.
Re: Can I invert the y axis so it starts from bottom?
Reply #8 - May 15th, 2009, 12:30pm
 
liudr wrote on Apr 9th, 2009, 6:14am:
Thank you NoahBuddy and PhiLho. I intend to use my programs as class demos so the y-axis needs to be inverted. Otherwise my students will be confused.


Can you create a "virtual coordinate system" and map it onto the real coordinates  That's what the "graphing2dequation" example does, for instance (I can't link to it).  It creates a virtual x,y coordinate system on top of the normal one, and then creates a virtual r,theta polar coordinate system on top of that.  I'm trying to plot math functions, and this seems like a good way to do it.
Page Index Toggle Pages: 1