# Calculate the slope of a 3D triangle?

I have lots of triangles in 3D space. How do I determine the slope/angle of these triangles with respect to a fixed ground plane? I need pseudo code examples at the very least.

• What do you mean by “angle”? Are you asking how to calculate a normal?
– Anko
Aug 18 '15 at 19:29
• Maybe he's looking for tangent and bi-tangent Aug 19 '15 at 2:36
• I'm not looking for a vector. I need the slope of the plane formed by the triangle with respect to the ground plane. The angle between the floating plane and the ground plane will be okay too. Aug 20 '15 at 6:35
• I guess the angle between the normal and the ground would be okay too. From that I can get the information I need. Aug 21 '15 at 0:32

1. get triangle normal vector v1 (normalized)

2. get reference surface normal vector v2 (normalized)

3. get angle between normals : angle = acos(v1•v2) (where • = 'dot' product )

4. get slope = Tan(angle)

if you need a surface normal here come the simple algoritm :

A surface normal for a triangle can be calculated by taking the vector cross product of two edges of that triangle. The order of the vertices used in the calculation will affect the direction of the normal (in or out of the face w.r.t. winding).

So for a triangle p1, p2, p3, if the vector U = p2 - p1 and the vector V = p3 - p1 then the normal N = U X V and can be calculated by:

Nx = UyVz - UzVy

Ny = UzVx - UxVz

Nz = UxVy - UyVx

EDIT: to get the angle between reference plane and triangle plane , you can calculate the angle between reference plane normal vector (call it Nref) and triangle normal (N already calculated). Here the angle between 3d vectors math:

"If v1 and v2 are normalised so that |v1|=|v2|=1, then,

angle = acos(v1•v2)"

Finaly from angle to slope : Tan(angle)

• I need the slope, though. A single scalar value not a vector. Aug 20 '15 at 6:27
• @posfan12 Angle between the ground plane and a triangle? Aug 20 '15 at 6:30
• I think you can't define a single slope. You can get x-slope and y-slope , and can calculate them from normal vector x and y components Aug 20 '15 at 6:37
• Why no slope? Two planes intersect at only one angle. Aug 21 '15 at 0:04
• oops , you're absolutely right Aug 21 '15 at 6:21

Based on dnk drone.vs.drones' instructions I created the following JavaScript:

function Get_Angle(vertex_1, vertex_2, vertex_3)
{
// get two vectors in the triangle
var vector_u =
[
vertex_2 - vertex_1,
vertex_2 - vertex_1,
vertex_2 - vertex_1
]
var vector_v =
[
vertex_3 - vertex_1,
vertex_3 - vertex_1,
vertex_3 - vertex_1
]
// calculate the cross product to get the normal
var vector_n =
[
vector_u * vector_v - vector_u * vector_v,
vector_u * vector_v - vector_u * vector_v,
vector_u * vector_v - vector_u * vector_v
]
// calculate the magnitude or length of the normal vector
var magnitude_n = Math.sqrt(vector_n * vector_n + vector_n * vector_n + vector_n * vector_n)
// normalize the normal vector
vector_n =
[
vector_n/magnitude_n,
vector_n/magnitude_n,
vector_n/magnitude_n
]
// the normal of the ground plane
var vector_r = [0,1,0]
// calculate the dot product of vector_n and vector_r
var dot_product = vector_n * vector_r + vector_n * vector_r + vector_n * vector_r
// calculate the angle between vector_n and vector_r
var angle_between = Math.acos(dot_product)
// returned value is less than or equal to pi
return angle_between
}

• Oh wow! Three years later! That's great, thanks for coming back and posting your solution :) Aug 9 '18 at 0:10