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 › Best Practice with 2D Vectors
Page Index Toggle Pages: 1
Best Practice with 2D Vectors (Read 1133 times)
Best Practice with 2D Vectors
Aug 5th, 2006, 7:28pm
 
Hello,

I've got an array of 2D points and I need a function to take one of those points, change it and then return.

There are two differet ways I can see going about it:

1) Using a 2d array of [point index][xy].
2) Using a 1d array of objects [object .x.y]

I'm interested in hearing how other people are doing this... Eventually I would like to have a proper vector class that can interact with a math library (transforms + rotations).  Is there a standard library people use for that in processing?

In the meantime I was keeping it simple and using approach 1, but I am confused on syntax.

I would like to make a 2d array and then fill the first slot with a point array [.1,.1].  In Jscript I would have done something like this:

var PointArray = new Array();
PointArray[0] = [.1, .1];

I can see Java is more strict but I'm hoping there's a way to do this... so far I tried:

float[][] PointArray;
PointArray = new float[3][2];
PointArray[0] = {.1 , .1};

But that doesn't seem to work at all.

Anyone have ideas about syntax or how I should be working towards using a proper math library???

Thanks alot,
RoboHaus
Re: Best Practice with 2D Vectors
Reply #1 - Aug 5th, 2006, 8:38pm
 
To use the 3D array, the syntax is:
Code:
float[][] PointArray;
PointArray = new float[3][2];
PointArray[0][0] = .1;
PointArray[0][1] = .1;


Or if you want to set the values at compile time, instead of whilst the code is running you can shorten it to:

Code:
 float[][] PointArray;
PointArray = new float[3][2];
PointArray[0] = new float[]{.1 , .1};


Howeve this isn't so good if you get to doing larger numbers of things, since it means you keep using new bits of memory instead of everything being nicely sorted and easy to accessfor the program.
Re: Best Practice with 2D Vectors
Reply #2 - Aug 5th, 2006, 10:41pm
 
Ok awesome.  Thanks for all the help on this...

It's taking me a minute to dig into all the little differences all over the place.

Another thing I'm stuck on is I get my methods working no problem in classes but parameters are giving me errors.  

I'm pretty sure I have to make my parameters public somehow does anyone know the syntax?

Thanks again for all the support,
RoboHaus

------ code that needs public access to params -----


wVec point = new wVec();
point.x = 7;

void setup()
{
 size(200, 200);
 background(176);
}

void draw()
{
}

class wVec
{
 float x;
 float y;
 float w;
 
 wVec(){
   this.x=0;
   this.y=0;
   this.w=0;
 }
 wVec(float x,float y){
   this.x=x;
   this.y=y;
   this.w=0;
 }
}



Re: Best Practice with 2D Vectors
Reply #3 - Aug 5th, 2006, 11:26pm
 
if you'r eusing classes, it's best to initialise them and change parts from within functions, not outside.

E.g:

Code:

wVec point;

void setup()
{
size(200, 200);
background(176);
point = new wVec();
wVec.x = 7;
}


and also you don't need the "this." stuff all the time, it's only useful if you've got a parameter with the same name, so you can get rid of them completely if you change it to this:

Code:
 class wVec
{
float x;
float y;
float w;

wVec(){
x=0;
y=0;
w=0;
}
wVec(float _x,float _y){
x=_x;
y=_y;
w=0;
}
}
Re: Best Practice with 2D Vectors
Reply #4 - Aug 6th, 2006, 2:24am
 
Ok cool.

You are saving me alot of time here... I much prefer to have code that is clean like that.

Thanks again,
RoboHaus
Re: Best Practice with 2D Vectors
Reply #5 - Aug 7th, 2006, 12:02am
 
Here's mine with a bit bloated set of tools

I've written this a while back and have been adding/changing it every time I learn something new about coding. This is only a snippet of my usual vector lib.

In general I dislike using n-dimensional arrays to do lists of vectors, but it's a bit faster if you do. Also, when making libraries, it's good to use array vectors since you don't want many versions of your Vector class.

I sometimes make lists of these Vec2 objects in a java Vector() list (not to be confused with mathematical vectors like below). This is again slower, but easier to add things into the array, delete things, insert things, etc. The only drawback to that method is that you must type-cast it back into a Vec2 every time you use it.

Note that some of these methods abuse Code:
 new Object()  

and can cause memory thrashing if you have say a hundred thousand objects.

Code:

class Vec2
{
float x;
float y;
Vec2()
{//constructor with no arguments, gives you origin
x=0;
y=0;
}
Vec2(float x,float y)
{//constructor with 2 arguments x and y
this.x=x;
this.y=y;
}
Vec2(Vec2 s)
{//constructor with Vec as input. basically an assignment operator
if(s==null)
return;
this.x=s.x;
this.y=s.y;
}
Vec2(Vec2 s,Vec2 t)
{//constructs a Vector from the difference of two Vectors
float dx=t.x-s.x;
float dy=t.y-s.y;
this.x=dx;
this.y=dy;
}
Vec2(Vec2 s,Vec2 t,float k)
{//constructs a Vector from the difference of two Vectors, then modifies it by k
float dx=t.x-s.x;
float dy=t.y-s.y;
this.x=dx*k;
this.y=dy*k;
}

//--------------BEGINNING OF VECTOR METHODS--------------
void disp(float ang,float magnitude)
{//displacement. will offset this Vector by an angle and distance
ang=radians(ang);
x+=cos(ang)*magnitude;
y-=sin(ang)*magnitude;
}
void rotate(float ang)
{
Vec2 temp=new Vec2();
temp.disp(ang()+ang,mag());
x=temp.x;
y=temp.y;
}
void rotate(Vec2 v,float ang)
{
Vec2 cen=new Vec2(v);
Vec2 ori=new Vec2(v,this);
ori.rotate(180+ang);
cen.add(ori);
this.x=cen.x;
this.y=cen.y;
return;
}
float ang()
{//returns the angle between this Vector and origin
return getAng(this,origin2);
}
float mag()
{//returns the distance between this Vector and origin
return dist(origin2,this);
}
//scalar operations
void add(float s)
{//addition operator
x+=s;
y+=s;
}
void sub(float s)
{//subtraction operator
x-=s;
y-=s;
}
void mul(float s)
{//multiplication operator
x*=s;
y*=s;
}
void div(float s)
{//division operator. returns 0 when division by zero
if(s==0)
return;
x/=s;
y/=s;
}
void add(float x,float y)
{//addition operator
this.x+=x;
this.y+=y;
}
void sub(float x,float y)
{//subtraction operator
this.x-=x;
this.y-=y;
}
void mul(float x,float y)
{//multiplication operator
this.x*=x;
this.y*=y;
}
void div(float x,float y)
{//division operator. returns 0 when division by zero
if(x==0||y==0)
return;
this.x/=x;
this.y/=y;
}
//Vector operators
void add(Vec2 s)
{//addition operator
x+=s.x;
y+=s.y;
}
void sub(Vec2 s)
{//subtraction operator
x-=s.x;
y-=s.y;
}
void mul(Vec2 s)
{//multiplication operator
x*=s.x;
y*=s.y;
}
void div(Vec2 s)
{//division operator. returns 0 when division by zero
if(s.x==0||s.y==0)
return;
x/=s.x;
y/=s.y;
}
}
Re: Best Practice with 2D Vectors
Reply #6 - Aug 10th, 2006, 10:08am
 
This is good information.  Thank you...

RoboHaus
Re: Best Practice with 2D Vectors
Reply #7 - Aug 15th, 2006, 8:24am
 
mflux wrote on Aug 7th, 2006, 12:02am:
Here's mine with a bit bloated set of tools

I've written this a while back and have been adding/changing it every time I learn something new about coding. This is only a snippet of my usual vector lib.


Mightn't it be easier to use this
http://java.sun.com/products/java-media/3D/forDevelopers/J3D_1_3_API/j3dapi/javax/vecmath/Vector2d.html

Well, not necessarily easier to use, but at least less coding for you Unless there's some other reason, I thought this was the standard way to make 2D Vectors in Java.
Re: Best Practice with 2D Vectors
Reply #8 - Aug 20th, 2006, 8:28am
 
Well, I didn't know that existed Smiley
Nowadays I'm well aware of plenty of these existing, but I never bother using them because I find my own custom stuff easier to personalize and change.

It's also a great learning experience for me knowing how to write stuff like this.
Re: Best Practice with 2D Vectors
Reply #9 - Aug 26th, 2006, 10:53pm
 
A little tutorial on Vectors, though my class is not as supercool as mflux's!

http://www.shiffman.net/teaching/the-nature-of-code/vectors/
Page Index Toggle Pages: 1