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 › support for Point class
Page Index Toggle Pages: 1
support for Point class? (Read 769 times)
support for Point class?
Dec 4th, 2008, 5:37am
 
Hi!  I am writing functions which need to return a certain point.  At first I was stupidly writing 2 almost identical functions, one to return the x and one to return the y.  Then I discovered the Point class.  I think this is implementing java.awt.geom.Point2D?  Is that right?

My question is why can't I simply use the point data type in the drawing functions such as vertex(), rect(), point() etc.?  Thanks!
Re: support for Point class?
Reply #1 - Dec 4th, 2008, 8:21am
 
What Point class are you referring to?

The function definitions aren't overloaded for parameters of type 'Point.' However, you can easily overload them yourself for whatever application you're working on.

The following example uses a simple Point2D class and an overloaded rect() function to let you pass two points (rather than four floats or ints) as the parameters. Since you're drawing a rect between two points, it's easy to pin one corner and draw to the mouse position without worrying about how wide your rectangle is.

Code:

Point2D left, right;

void setup(){
size(400,400);
left = new Point2D( 10, 200 );
right = new Point2D( 100, 100 );
}

void draw(){
background(0);
right.x = mouseX;
rect( left, right );
}

class Point2D{
float x, y;
Point2D( float x, float y ){
this.x = x;
this.y = y;
}
}

void rect( Point2D topLeft, Point2D bottomRight ){
rect( topLeft.x, topLeft.y, bottomRight.x-topLeft.x, bottomRight.y-topLeft.y );
}
Re: support for Point class?
Reply #2 - Dec 4th, 2008, 8:38pm
 
Ah, thanks!  I didn't know you could overload functions like that.

Processing seems to recognize a class called "Point", and it seems to work just like Point2D.  Here's your example modified to use only Point:

Code:

Point left, right;

void setup(){
 size(400,400);
 left = new Point( 10, 200 );
 right = new Point( 100, 100 );
}

void draw(){
 background(0);
 right.x = mouseX;
 rect( left, right );
}

void rect( Point topLeft, Point bottomRight ){
 rect( topLeft.x, topLeft.y, bottomRight.x-topLeft.x, bottomRight.y-topLeft.y );
}
Re: support for Point class?
Reply #3 - Dec 5th, 2008, 10:33am
 
That's probably Java's java.awt.Point.
Note that recent versions of Processing have introduced PVector for this kind of usage (storing 2D and 3D coordinates, which can be "used as a position, velocity, and/or acceleration".
It has lot of useful methods...
Page Index Toggle Pages: 1