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 & HelpPrograms › basic vector calculations
Page Index Toggle Pages: 1
basic vector calculations (Read 4163 times)
basic vector calculations
Dec 21st, 2009, 10:41am
 
I am still working on my particleMesh inspired by dook Mesh by introspector. I used some of his code for vector calculations of the different points. its needed especially for generating the holes.

Now i want to go a bit further and enhance the model
but i am lacking some basic vector math knowledge. I just dont know how to calculate these points.


What i wanna do is adding some tube like structure to the holes. for doing that i need to calculate the red points (only 1/4 is drawn yet) that are standing out of the mesh to connect them. i tried it but you can tell that it is not working. I hope its clear what i want to achieve.

If i need to be clearer on what I need, please let me know.
Would be thankful for any help.

you can take a look at a reduced sketch at
http://www.dec32.de/public/p5/vectors

Re: basic vector calculations
Reply #1 - Dec 21st, 2009, 1:11pm
 
it's easy to work out a vector at right angles to two others - there's a method on PVector that just does that. ( http://processing.org/reference/PVector_cross_.html )

use two sides of your hole and it'll give you a vector sticking out (or in, in which case just reverse the source vectors)

more details from wolfram...
http://mathworld.wolfram.com/CrossProduct.html
Re: basic vector calculations
Reply #2 - Dec 21st, 2009, 1:18pm
 
So you want them to be perpendicular to the plane on which the hole lies  Isn't that akin to calculating a normal  Must admit I'm pretty clueless with this 3d vector stuff too, but I suspect normal calculation is pretty well documented.

In fact it looks like you might need the cross product.  Not sure my brain can cope with all that maths right now though :S
Re: basic vector calculations
Reply #3 - Dec 21st, 2009, 2:58pm
 
Thanks guys, i had the feeling it has something to do with the crossproduct and i already tried my best but couldnt figure out how to use it. Like I said, i havent worked alot with 3d vector math.

So maybe you can be a bit more specific on what i have to do.
As these PVectors I am using are actually coordinates . I guess i have to substract/normalize whatever before I can use them. And then get the crossproduct and add/multiply it ?!  So yeah like i said, i am a bit lost here.

I made a quick sketch to show you the naming of the variables.
So what would be the next step to get these perpendiculat points?
Thx!
...
Re: basic vector calculations
Reply #4 - Dec 21st, 2009, 11:41pm
 
diagram a bit confusing (to me) are the perpendicular points emerging from the Po points or the P points?

anyway, to find a point out from P1 then you want the cross product of P4-P1 and P1-P2. the vector from P4 to P1 is simply P1 - P4.

so your cross product is PVector.cross(P1 - P4, P2 - P1); (NB not sure of the syntax here)

then just add P1 to that so that it emerges from P1.

(i haven't checked the signs of the above so you might end up with a vector going IN in which case just swap the two arguments to the cross() (i think, failing that use P1-P4 and P2-P1 as arguments)

and you don't need to normalise before finding cross product - it'll just mean your result isn't normalised but will still point in the same direction.

(and there's not really a lot of maths involved in cross product, just mults and adds - you write the two 3d vectors down as columns, put your finger over the row you're calculating, find out the determinant of the 2x2 matrix that's left (which is just AD - BC) and write that down (multiplying by -1 for the middle row). used to have to do it manually at A-level)
Re: basic vector calculations
Reply #5 - Dec 22nd, 2009, 12:59am
 
Hey koogy, thanks for your help.

Sorry for the bad drawing. There are no perpendicular lines drawn yet. thats just the way it is right now. i thought i make a sketch so we know what points we are talking about.
Anyway i guess it doesnt matter what point we take. Im sure I can adapt it as long as i get the concept.

I tried to do what you told me. But i get some really strange results.
so what i did was. getting the cross product of these two vectors.

PVector V1 = PVector.sub(P1, P4).cross(PVector.sub(P2, P1));

and then added P1 to it.
V1.add(P1);

so shouldnt " point(V1.x , V1.y , V1.z);" be what I wanted?

thx again!
Re: basic vector calculations
Reply #6 - Dec 22nd, 2009, 3:13am
 
apologies for the delay - breakfast!

couldn't get your example working due to lack of libraries. rattled up a shorter one, hope it's clear.

that PVector class is a bit of a mess, or the documentation for it is. it always ends up looking like functional programming! it's not obvious what gets created, what gets modified etc.

Code:

// acd 20091222
// for Cedric

PVector[] p  = new PVector[8];  // cube
PVector[] pi = new PVector[4];  // inner square
PVector[] n = new PVector[4];   // normals
float xangle, yangle, zangle;
float xdelta, ydelta, zdelta;

void setup() {
 size(640, 640, P3D);
 println("Setup");
 
 // big cube
 p[0] = new PVector(1, 1, 1);
 p[1] = new PVector(1, 1, -1);
 p[2] = new PVector(1, -1, -1);
 p[3] = new PVector(1, -1, 1);
 p[4] = new PVector(-1, 1, 1);
 p[5] = new PVector(-1, 1, -1);
 p[6] = new PVector(-1, -1, -1);
 p[7] = new PVector(-1, -1, 1);
 
 // inner square
 pi[0] = new PVector(1, .5, .5);
 pi[1] = new PVector(1, .5, -.5);
 pi[2] = new PVector(1, -.5, -.5);
 pi[3] = new PVector(1, -.5, .5);
 
 xangle = random(TWO_PI);
 xdelta = random(-.02, .02);
 yangle = random(TWO_PI);
 ydelta = random(-.02, .02);
 zangle = random(TWO_PI);
 zdelta = random(-.02, .02);
 println("Setup Done");
}

void draw() {
 background(0);
 camera(500, 500, 500, 0, 0, 0, 0, 1, 0);
 rotateX(xangle);
 xangle += xdelta;
 rotateY(yangle);
 yangle += ydelta;
 rotateZ(zangle);
 zangle += zdelta;
 scale(100);
 
 // big cube (white)
 stroke(255);

 line(p[0], p[1]);
 line(p[1], p[2]);
 line(p[2], p[3]);
 line(p[3], p[0]);
 
 line(p[4], p[5]);
 line(p[5], p[6]);
 line(p[6], p[7]);
 line(p[7], p[4]);

 line(p[0], p[4]);
 line(p[1], p[5]);
 line(p[2], p[6]);
 line(p[3], p[7]);

 // join outer to inner (blue)
 stroke(0, 0, 255);
 line(p[0], pi[0]);
 line(p[1], pi[1]);
 line(p[2], pi[2]);
 line(p[3], pi[3]);
 
 // inner square (green)
 stroke(0, 255, 0);
 line(pi[0], pi[1]);
 line(pi[1], pi[2]);
 line(pi[2], pi[3]);
 line(pi[3], pi[0]);
 
 // normals off inner square (red)
 stroke(255, 0, 0);
 // vectors joining adjacent points
 PVector pi01 = PVector.sub(pi[1], pi[0]);
 PVector pi12 = PVector.sub(pi[2], pi[1]);
 PVector pi23 = PVector.sub(pi[3], pi[2]);
 PVector pi30 = PVector.sub(pi[0], pi[3]);
 // find normals and add to points
 n[0] = PVector.add(pi01.cross(pi30), pi[0]);
 n[1] = PVector.add(pi12.cross(pi01), pi[1]);
 n[2] = PVector.add(pi23.cross(pi12), pi[2]);
 n[3] = PVector.add(pi30.cross(pi23), pi[3]);
 // draw them
 line(pi[0], n[0]);
 line(pi[1], n[1]);
 line(pi[2], n[2]);
 line(pi[3], n[3]);
}

// line routine which takes vectors (to save typing)
void line(PVector a, PVector b) {
 line(a.x, a.y, a.z, b.x, b.y, b.z);
}


NB those red lines are calculated using ONLY the green lines, that's all you need.
Re: basic vector calculations
Reply #7 - Dec 22nd, 2009, 3:16am
 
No problem, I am thankful for any help i can get Smiley

I am about to leave but i will take a closer look at it later.
But looks promising.

Thanks again! i will tell you if i could make it work.
Ttyl
Re: basic vector calculations
Reply #8 - Dec 22nd, 2009, 1:13pm
 
Alright, I spend some time adding your code to the sketch. Now i had to normalize and multiplay them to get some distance points. But works great. Thanks a lot for helping with these vector calculuations. Learned a lot this way.

http://dl.dropbox.com/u/1152794/Screen_3876.jpg


Page Index Toggle Pages: 1