# Thread: normal vector. how to compute it?

1. ## normal vector. how to compute it?

hi,

I have some difficulties to compute a normal vector between two vectors AB * AC. Is someone could help me please?

thanks!!

2. ## Re: normal vector. how to compute it?

hi,

use the crossproduct!

Code :
```void Vector3f::CrossProd(const Vector3f&amp; vec,Vector3f&amp; normal)
{
normal.x = y*vec.z-z*vec.y;
normal.y = -x*vec.z+z*vec.x;
normal.z = x*vec.y-y*vec.x;
}
Snippet from my library.```
Happy coding,

Martin

3. ## Re: normal vector. how to compute it?

here is my cross product function:

void crossProduct(vertex3D a, vertex3D b, vertex3D c, vertex3D d)
{
d[0]=(b[1]-a[1])*(c[2]-a[2])-(c[1]-a[1])*(b[2]-a[2]);
d[1]=(-(b[0]-a[0])*(c[2]-a[2])-(c[0]-a[0])*(b[2]-a[2]));
d[2]=(b[0]-a[0])*(c[1]-a[1])-(c[0]-a[0])*(b[1]-a[1]);
normal(d);
}

can you see a mistake?
My normal vectors point into the wrong direction.

4. ## Re: normal vector. how to compute it?

Hi, the cross product is like this ->
Code :
```void vec3_t::CrossProduct(const vec3_t &amp;v1,const vec3_t &amp;v2)
{
v[0] = ( v1.v[1] * v2.v[2] - v1.v[2] * v2.v[1] );
v[1] = ( v1.v[2] * v2.v[0] - v1.v[0] * v2.v[2] );
v[2] = ( v1.v[0] * v2.v[1] - v1.v[1] * v2.v[0] );
}```
Old GLman

5. ## Re: normal vector. how to compute it?

Hi, I have this tick that I always exactly want to know what something does and why..

i know what this does, but what is the mathamatical explaination (im just 15 years old so i dont have this on school yet) but can anyone post an URL on the mathamatical explaination of this crossproduct??

thx alot

6. ## Re: normal vector. how to compute it?

The direction of the normal is dependent on the order of the three vertices to use. Switch a and c around and it will point in the correct direction.

7. ## Re: normal vector. how to compute it?

The dot product of two vectors is 0 if they are orthogonal to each other. You want a vector that is orthogonal to two vectors. Thus you have the equation system:

v1 * normal = 0
v2 * normal = 0

v11 * normal1 + v12 * normal2 + v13 * normal3 = 0 (1)
v21 * normal1 + v22 * normal2 + v23 * normal3 = 0 (2)

Now set normal1 = 1, just to eliminate one unknown (this step will be done backwards at the end). You'll get:

v11 + v12 * normal2 + v13 * normal3 = 0 (1)
v21 + v22 * normal2 + v23 * normal3 = 0 (2)

Solve (1) for normal2

normal2 = (-v11 - v13 * normal3) / v12

Insert into (2):

v21 + v22 * (-v11 - v13*normal3)/v12 + v23 *normal3 = 0

Solve for normal3:
normal3 = (v11*v22-v12*v21)/(v12*v23-v13*v22)

Solve (1) for normal3:
normal3 = -(normal2*v12+v11)/v13

Insert into (2):
v21 + v22*normal2 + v23 * -(normal2*v12+v11)/v13 = 0

Solve for normal2:
normal2 = (v13*v21-v11*v23)/(v12*v23-v13*v22)

Thus we have the vector:

normal1 = 1
normal2 = (v21*v13-v11*v23)/(v12*v23-v13*v22)
normal3 = (v11*v12-v21*v12)/(v12*v23-v13*v12)

Now we can scale the vector with (v12*v23-v13*v12), so we finally have:
normal1 = (v12*v23-v13*v22)
normal2 = (v13*v21-v11*v23)
normal3 = (v11*v22-v12*v21)

[This message has been edited by Michael Steinberg (edited 05-05-2002).]

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•