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.
Page Index Toggle Pages: 1
ortho() for 2D? (Read 554 times)
ortho() for 2D?
May 29th, 2006, 6:48pm
 
Is there a way to set the coordinate system in a 2D sketch by specifying the X and Y ranges? I have a collection of nodes with varying positions in my sketch and I want to be able to shift and zoom the window to show them all. I know I can do that with translate() and scale(), but it seems useful enough to have a dedicated function for. ortho() does exactly this in 3D, but is there a purely 2D version? And if not, shouldn't there be?
Re: ortho() for 2D?
Reply #1 - Jun 8th, 2006, 8:45pm
 
Sure.   The window pixels can relate to your 'world coordinates' any way you want by using 2 variables, lets call them rangex and rangey.

Lets say you want the center of the window to be 0,0 and that the window can cover any sized rectangle of your world coordinates. So window's x could cover [-1,1] (rangex=2) or [-3000,3000] (rangex=6000), etc.

drawnode(float nodex, float nodey){  
int screenx,screeny;

screenx=(nodex/rangex) * width +  (width/2);
screenx=(nodey/rangey) * height +  (height/2);

ellipse(screenx, screeny,10,10);  //draw node
 
}

This is coordinate mapping.  To add shifting is not much harder.
Page Index Toggle Pages: 1