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
xy-tuples (Read 658 times)
xy-tuples
Oct 19th, 2006, 5:28am
 
what would be nice is a built-in datatype that represents a single x and y value.  Then arrays of this xy-tuple could be manipulated as one variable in arrays, rect() corners, line() coordinates, etc.  This would permit the easier manipulation of x and y variables, while maintaining the x and y values.
Re: xy-tuples
Reply #1 - Oct 19th, 2006, 11:19am
 
This is what classes are good for, defining your own complex data types.

Code:
class xy
{
float x,y;
xy(float _x, float _y)
{
x=_x;
y=_y;
}
}


You can then overload rect/line yourself:

Code:
void rect(xy topleft, xy bottomright)
{
rect(topleft.x,topleft.y,bottomright.x,bottomright.y);
}
void line(xy start, xy end)
{
line(start.x,start.y,end.x,end.y);
}
Re: xy-tuples
Reply #2 - Oct 19th, 2006, 4:17pm
 
JohnG, thanks for the great example.  

I know writing this is easy to do; I seem to write this kind of class with every other Processing project I work on.  It would just be nice if this was a "value-added" data structure in processing.  Processing has a color datatype (which is from  Java), so why not an xy-tuple?
Re: xy-tuples
Reply #3 - Oct 19th, 2006, 4:26pm
 
Processing's color datatype isn't actually a datatype. Color is just another name for int.
Re: xy-tuples
Reply #4 - Oct 19th, 2006, 6:47pm
 
you can use Point and Point2D.Float from java to do this.

Code:

Point p = new Point(50, 20);
line(p.x, p.y, width, height);


or if you want floats:

Code:

import java.awt.geom.*;

Point2D.Float p = new Point2D.Float(50, 20);
line(p.x, p.y, width, height);


we don't have a special PPoint class because 1) you can get this straight from java, so there's no need and 2) using objects for very simple things like this creates a lot of overhead. this is what slows down java2d a lot, because it encourages a style of programming where you create a lot of small, temporary objects.
Page Index Toggle Pages: 1