If you haven't already done it ...
First get a vector "V" that is perpendicular to your triangle or plane. If your triangle is specified by its vertices A,B,C this would be the vector product ("cross product") of any two sides, e.g. V = (B-A)x(C-A). Function sgMakeNormal does this and normalises the length of the result to 1.
I believe you want the angle "a" between V and the Z axis; this angle will be 0 when the triangle is horizontal, and increase toward 90 degrees as the slope of the triangle approaches vertical.
R R = sqrt(Vx*Vx + Vy*Vy)
\ | Vz
|V| \ |
\a| a = arctan(R/Vz)
Or, if your vector V is normalised to unit length, as it is if you use sgMakeNormal(V, A, B, C) then a = arccos(Vz). This uses slightly more computation to get the same result.
\ | Vz
|V| \ |
=1 \a| a = arccos(Vz)
The angle "a" will be negative when the vector V has a negative Z component, i.e. for triangles that are "upside down". If you don't know or don't care which way up they are, you can just ignore the sign.
So I suggest:
/* Get the unsigned slope of a triangle, 0 for horizontal up to pi/2 for vertical. */
float TriangleSlopeRadians( const sgVec3 a, const sgVec3 b, const sgVec3 c )
/* Get a unit-length vector perpendicular to the plane. */
sgMakeNormal( V, a, b, c );
/* Get the slope (angle between V and Z axis). */
return fabs( acos( V ) ); /* Ignore the sign; return positive values */
(I haven't tried to compile or test this.)
There are other ways to implement it, of course, but I hope this helps.
Norman Vine wrote:
> >David Megginson writes:
> >>Does anyone have a quick formula for calculating the slope of a
> >>triangular plane in the XYZ coordinate space, relative to the z axis?
> >>I know it cannot be that tricky, but I'm not much of a geometry guy.
> The PLib code should be all you need in that it creates
> the plane normal vector plus the offset from the origin.
> FWIW - This is a 'standard' definition of a Plane for
> geometrical computing.
> >FYI there is code in PLib for most 'Plane' operations
> >several sgMakePlane() variants in sg.h