# Handy Mathematics Facts for Graphics

email scd@cs.brown.edu with suggested additions or corrections

Eric Weisstein's world of Mathematics (which used to be called Eric's Treasure Trove of Mathematics) is an extremely comprehensive collection of math facts and definitions. Eric has other encyclopedias at www.treasure-troves.com

S.O.S. Mathematics has a variety of algebra, trigonometry, calculus, and differential equations tutorial pages.

Dave Eberly has a web site called Magic Software with several pages of descriptions and code that answers questions from comp.graphics.algorithms.

Steve Hollasch at Microsoft has a very comprehensive page of graphics notes which he would like to turn into a graphics encyclopedia.

Vector math identities and algorithms from Japan.

Paul Bourke has a variety of pages with useful tidbits, many of which are linked to from Steve Hollasch's page. The site is in Australia, so the connection is sometimes quite slow from the U.S.

The graphics group at UC Davis also has notes about computer graphics.

Peter H. Dana has created, as a part of The Geographer's Craft Project, an excellent page documenting geographic coordinate systems.

Josh Levenberg has a page of links to yet more graphics algorithm resources.

## Constants

sqrt(2)
1.4142135623730950488016887242
phi = 1/2(sqrt(5)+1)
1.6180339887498948482045868343656
sqrt(3)
1.7320508075688772935274463415
e
2.71828182845904523536028747135266
pi
3.14159265358979323846264338327950288
Feigenbaum's constant
4.669201609102990671853

Phi is the golden ratio for which 1/phi = phi - 1 and phi2 = phi + 1.

## Platonic Solids and other regular polytopes

SolidVertex coordinates
Tetrahedron ( 1, 1, 1), ( 1, -1, -1), ( -1, 1, -1), ( -1, -1, 1)
Cube (+-1,+-1,+-1)
Octahedron (+-1, 0, 0), ( 0,+-1, 0), ( 0, 0,+-1)
Cubeoctahedron( 0,+-1,+-1), (+-1, 0,+-1), (+-1,+-1, 0)
Icosahedron ( 0,+-p,+-1), (+-1, 0,+-p), (+-p,+-1, 0)
Dodecahedron ( 0,+-i,+-p), (+-p, 0,+-i), (+-i,+-p, 0), (+-1,+-1,+-1)
Icosidodecahedron(+-2, 0, 0), ( 0,+-2, 0), ( 0, 0,+-2), (+-p,+-i,+-1), (+-1,+-p,+-i), (+-i,+-1,+-p)

with golden ratio: p = 1/2(sqrt(5)+1); i = 1/2(sqrt(5)-1) = 1/p = p - 1

### Icosahedra

Another contruction for the icosahedron puts two of the vertices at +/- 1 along one of the major axes and two rings of five vertices with a radius of 2/sqrt(5) from the axis and at heights of +/- 1/sqrt(5) above and below the equator.

## Trigonometric identities

Law of Cosines - a, b, c are lengths of the sides of a triangle, angle C is opposite side c
c2 = a2 + b2 - 2 a b cos C

Angle addition and subtraction
 sin(a + b) = sin a cos b + cos a sin b sin(a - b) = sin a cos b - cos a sin b cos(a + b) = cos a cos b - sin a sin b cos(a - b) = cos a cos b + sin a sin b tan(a + b) = (tan a + tan b) / (1 - tan a tan b) tan(a - b) = (tan a - tan b) / (1 + tab a tan b)

Half-angle relations
 sin2 a = 1/2 (1 - cos 2a) cos2 a = 1/2 (1 + cos 2a)

Trigonometric functions at select points
DegreesRadiansSineCosine
0001
15pi/12 1/2 sqrt(2 - sqrt(3)) 1/2 sqrt(2 + sqrt(3))
18pi/10 1/2 phi-1 1/2 sqrt(phi + 2)
22.5pi/8 1/2 sqrt(2 - sqrt(2)) 1/2 sqrt(2 + sqrt(2))
30pi/6 1/2 1/2 sqrt(3)
36pi/5 1/2 phi-1 sqrt(phi + 2) 1/2 phi
45pi/4 1/2 sqrt(2) 1/2 sqrt(2)

## Rotation matrices

The following computations will rotate a point about an axis rooted at the origin. To rotate an object about a point other than the origin, you must first translate the object to place the point at the origin and then translate it back after the rotation.

Rotate [ P.x P.y P.z ]T about the x-axis by t radians:

```   [ P.x' ]   [  1    0       0    ]   [ P.x ]
[ P.y' ] = [  0  cos(t) -sin(t) ] * [ P.y ]
[ P.z' ]   [  0  sin(t)  cos(t) ]   [ P.z ]```
rotates from the y-axis towards the z-axis.

Rotate [ P.x P.y P.z ]T about the y-axis by t radians:

```   [ P.x' ]   [  cos(t)  0  sin(t) ]   [ P.x ]
[ P.y' ] = [    0     1    0    ] * [ P.y ]
[ P.z' ]   [ -sin(t)  0  cos(t) ]   [ P.z ]```
rotates from the z-axis towards the x-axis.

Rotate [ P.x P.y P.z ]T about the z-axis by t radians:

```   [ P.x' ]   [  cos(t) -sin(t)  0 ]   [ P.x ]
[ P.y' ] = [  sin(t)  cos(t)  0 ] * [ P.y ]
[ P.z' ]   [    0       0     1 ]   [ P.z ]```
rotates from the x-axis towards the y-axis.

Rotate [ P.x P.y P.z ]T about the unit vector [ x y z ]T by t radians:

Let s = sin(t), c = cos(t), c_1 = 1 - c, and precompute xy*c_1, xz*c_1, yz*c_1, xs, ys, and zs (for the sake of efficiency.)

```   [ P.x' ]   [ xx*c_1+c   xy*c_1-zs  xz*c_1+ys ]   [ P.x ]
[ P.y' ] = [ xy*c_1+zs  yy*c_1+c   yz*c_1-xs ] * [ P.y ]
[ P.z' ]   [ xz*c_1-ys  yz*c_1+xs  zz*c_1+c  ]   [ P.z ]```

More elaborate descriptions can be found in chapters five and six of "The Book", _Computer Graphics: Principles and Practice, 2nd Ed._ by Foley, van Dam, Feiner, and Hughes.

## Line-line intersection

Given two lines, one passing through the points P0 and P1 and the other through Q0 and Q1, we want to find the point of intersection. We start by defining one of the lines explicitly as

P = P0 + t(P1 - P0)
and the other implicitly as
dot(N, Q0 - P) = 0
where N is the normal to the line, (-y, x) of the vector along the line.

Substitute the explicit equation into the implicit one and solve for t to get

t = dot(N, Q0 - P0) / dot(N, P1 - P0)

Finally, substitute t into the explicit equation to solve for P, the point of intersection.

## Line-plane intersection

We want to compute the intersection between a line (defined by a point Pline and a vector Vline) and a plane passing through the points A, B, and C in 3-space. We define points P on the plane using its normal,

N = normalize(cross(A-C, B-C)),
and one of the other points it contains, such as C by the implicit equation
dot(N, C-P) = 0.
If the explicit equation for points P on the line is
P = Pline + t Vline,
then we can substitute this into the plane equation and solve for t to get
t = dot(N, C-Pline) / dot(N, Vline).
Substituting this value of t back into the line equation gives us the point of intersection between the line and the plane.

## Line-triangle intersection

To test for intersection between a line and a triangle, first compute the point of intersection between the line and the plane containing the triangle as described above.

Next, we want to see if this point lies within the triangle. Since this is an inherently 2-D problem, we simplify the test by projecting the triangle and point onto one of the primary coordinate planes. To avoid loss of precision, we want to drop the component for which the plane normal has the largest absolute value. We will call these new points a, b, and c for the triangle and p for the point.

Finally, we compute the barycentric coordinates (u, v, w) for the point as

denom = cross(a-c, b-c)
u = cross(p-c, b-c) / denom
v = cross(a-c, p-c) / denom
w = 1 - u - v

The point p is in the triangle if u and v are between zero and one and sum to less than one. (Computing w is unnecessary.)

Note that this is the 2D scalar cross product:

cross((x0, y0), (x1, y1)) = x0 y1 - x1 y0

## Spherical triangle - radial ray intersection

To determine if a radial ray intersects a spherical triangle on the unit sphere, compute the intersection of the ray with the plane passing through the triangle's vertices and test for intersection with the planar triangle.

Alternatively, if

dot(P, cross(A, B))
is positive, then the radial ray P is on the "inside" of the triangle edge AB. (Note that A, B, and P need not be normalized.) If the ray is on the inside of all three triangle edges, then it is contained within the triangle. This method does not, however, give positional coordinates within the triangle.

# Misc. links

A guide to a huge number of file formats.

back to my home page

Steven C. Dollins
Last modified: Tue Feb 19 15:54:17 EST 2002