Solved

# Calculate Normal  Vector for given triangle in Managed DirectX C#

Posted on 2007-08-10
2,892 Views
In Managed DirectX C#, I am creating a custome triangle list.

What I want to know, is how to easily (or do it at all i guess) calculate the normal for a given face (triangle). This is sure easy when you have a fave which all to verticies rest on the same X,Y, or Z plane, but how can you calaculate it if you have a face which could be facing any direction?

I am sure it can be calculated from the 3 verticies, but I am not seeing how. I am hoping there is a simple method which returns a normal vector for a given triangle but that's probably hoping for to much.

Thanks,
Matt
0
Question by:mattososky

LVL 22

Expert Comment

You can use the vector coross procuct.
Assume triangles have vertices at p1 (x1, y1, z1), p2 (x2, y2, z2) p3 (x3, y3, z3).
Build two vectors:
A = (x2 - x1)i + (y2 - y1)j + (z2 - z1)k = A1i + A2j + A3k
B = (x3 - x1)i + (y3 - y1)j + (z3 - z1)k = B1i + B2j + B3k

C = A X B = (A2B3 - A3B2)i + (A3B1 - A1B3)j + (A1B2 - A2B1)k

C is perpendicular (normal)  to the plane of the triangle.
0

Author Comment

As I'm reading your comment I don't understand what the variables i,j,k are suppose to represent.
0

LVL 22

Expert Comment

They identify the components of the vector. i is component parallel to x axis, j is parallel to y, and k is parallel to z. They are not really variables. So if you think of a vector as a 3-tuple, then the "i" value is the first, j the second, and k the third.

0

LVL 22

Expert Comment

Just to clarify, in case you are new to vectors, it's kind of like the general equation of a line:
aX + bY +cZ + D = 0

Where x, y and z just identify the use of the variables.
0

Author Comment

perhaps just the language and syntax are the difference. I'm using c# managed directX 9. you can define a Vector3 like Vector3(float x, float y, float z) and access the members by vector.position.x , y , & z;

So if I'm understanding you right. what your saying is i can do this: Create 2 new vectors. NewVectory1 will have x,y,z componets that are the difference of the x,y,z of Triangle points 2 & 1. NewVector2 will have x,y,z, components that are the difference of the x,y,z of Triangle points 3 & 1. Then, multiply the two new vectors together and i will get a vector perpendicular to the face of the Triangle.

Yes?

0

LVL 22

Accepted Solution

Correct, but the components need the operations shown here for the multiplication;
say your vectors are called A and B, then:
C = A X B = new Vector( (AYBZ - AZBY),  (AZBX - AXBZ),  (AXBY - AYBX));

Make sense now?

0

## Featured Post

### Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…