## Volume Under a Triangle

```In 3D space with Cartesian coordinates, suppose we are given the
positions of three points, and we regard these points as the vertices
of a triangle.  If we regard the z axis as vertical height, what is
the volume "under" this triangle above the z=0 plane?  We can obviously
perform a double integration, with the limits of the "inner" integral
being linear functions of the parameter of the outer integral (by
treating the triangle in two parts), but there are simpler ways.

One approach is to number the points P1, P2, P3 such that

z1 < = z2 < = z3

and let P4 be the intersection of the segment P1-P3 with the z=z2
plane.  Now we have two right tetrahedrons whose volumes (relative to
the z2 plane) are obviously one third the base times the altitude.
So, if we let A1 and A2 denote the areas of triangles P2P3P4 and
P1P2P4 projected onto a constant z plane, it follows that the volume
under the original triangle is simply

V  =   A1 (z3-z2)/3  -  A2 (z2-z1)/3  +  (A1+A2)z2

where
A1  =  [(x4y2-x2y4)+(x3y4-x4y3)+(x2y3-x3y2)]/2
A2  =  [(x1y2-x2y1)+(x4y1-x1y4)+(x2y4-x4y2)]/2

and of course the values of x4 and y4 are given by

x4 = x1 + ((z2-z1)/(z3-z1)) (x3-x1)
y4 = y1 + ((z2-z1)/(z3-z1)) (y3-y1)

Substituting into the equation for the volume gives the result

V  =  (z1+z2+z3)(x1y2-x2y1+x2y3-x3y2+x3y1-x1y3)/6

which shows that the volume under the original triangle is just the
projected area of the triangle times the average of the three "heights".
(By the way, the sign of the area expression will be positive or
negative depending on whether we numbered our points clockwise or
counter-clockwise.)

For another approach, let's again number the three points P1, P2, P3
such that z1 < z2 < z3.  A flat plane parallel to the xy plane at
z = z2  will cut the triangle on a line from the vertex P2 to some
point P4 on the opposite edge.  We now have two triangles, P2P4P3 and
P2P4P2, sharing the common base P2P4.  Since this base is a line of
constant z, the value of z at other points of the triangle is
just a linear function of the distance from this line.

Now we make use of the fact that the average value of the height (or
of any variable proportional to the height) of uniformly distributed
points in a triangle is 1/3 the value at the apex.  To prove this,
suppose the average height of a given triangle of height H is h.  Now
draw a line parallel to the base at height H/2, and then draw lines
through the points of intersection parallel to the opposite edges.
This divides the original triangle into four identical triangles of
height H/2, each similar to the original. By similarity, the average
heights of these four triangles above the original base are h/2,
H/2-h/2, h/2, and H/2+h/2, so the average height of the combined
set is (H+h)/4, and this must equal h for the original triangle.
Therefore h = H/3.

It follows that the average value of z on the triangle P2P4P3 is
z2 + (z3-z2)/3 and the average value of z on the triangle P2P4P1 is
z2 - (z2-z1)/3.  Now let a and b denote the altitudes of these two
triangles respectively, and let c denote the length of the line P2P4.
The areas of these triangles are ac/2 and bc/2, so we can express
the average value of z over the entire triangle as the weighted
average

(ac/2)[z2 + (z3-z2)/3] + (bc/2)[z2 - (z2-z1)/3]
-----------------------------------------------
(ac/2) + (bc/2)

Cancelling factors, this can be written as

(a/b)z3 + z1
(2/3) z2  +  --------------
3(a/b) + 3

By similar triangles we know a/b = (z3-z2)/(z2-z1), and substituting
this into the above expression gives the result (z1+z2+z3)/3 as the
average value of z over the original triangle.
```