This is a small paper I wrote on my own to present my findings on 3D perspective graphing.

Now personally I searched all over the web and quite a few library books looking for 3D perspective transform equations from an arbitary perspective, while pulling my hair out because I knew that what I wanted could be written on a 6'X4' index card but I couldn't find it all in one place anywhere.  So for all of you who are looking for the conclusion up front, here it is:



Notes on Compatability:

Most of the books I've looked at use 'left-sided' vector multiplication where multiply a matrix by a row vector on the left side.  Some of the papers and books also used a "left handed" coordinate system where X is considered posative on the left side of the Y axis.

Since it is more in line with what you will see in a standard Calculus, Physics or introductory Linear Algebra book I will work with 'right sided' multiplication where you multiply a matrix by a column vector on the right side, and I will use a "right hand coordinate system" like all of my physics books demand where X is posative on the right side of the Y axis and x cross y = +z.

Also whereas some books place the observer in the "simple perspective" equations on the negative Z-axis, I place the observer on the posative Z-axis.  I think observing from the -Z-axis may go more easily with a "left-handed" coordinate system.

To convert between 'left-sided' and 'right-sided' multiplication just take the transpose of the matrix.

Also, I'm using the standard "math book definitions" of Theata and Phi where Theata is the angle in the X-Y plane starting from the posative X-axis giong "counter clockwise" toward the Y-axis, and Phi is the angle with the posative Z-axis  giong "downward" toward the X-Yplane.  Sorrry physics buddies, but those definitions always made more sense to me.



Concept 1:  Perspective

Now suppose that you're some distance (d) up on the Z axis looking down.  How would things look to you at least compared to the X-Y plane????  Well let's say there's some point (p) off to one side of you.  The point p has the coordinates (X,0,Z).  But following "line of sight" it would appear to be somewhat farther out on the negative X-axis than it really is.  In the picture it will appear to be at the point p* which has the corrdinates (Xs,0,0).  But how do we make an exact calculation of this?

Well, the easiest way may be to use simular triangles.  The triangle  from p* up to d on the Z-axis down to the origin (O) shares the top angle ( "" in the picture) with the smalle triangle from p up to d and then back down to z.  There are also both right triangles, and since the 3 angles of a triangle must always add up 180 (at least in flat space) the third angle must be equal also.  Therefore <p*dO is simular to <pdz.  Therefore their sides must be proportional.  We can form a proportion between any 2 like sides of the the triangles.  The length of the bottom of the larger triangle is Xs while the length of the bottom of the smaller triangle is just x.  The "height" of the bigger triangle is "d" (the distance to the eye of the observer) while the "height" of the smaller triangle is d - z.

Therefore Xs / d  =  x/(d-z)   OR  Xs = xd/(d - z).

By a simular argument Ys = yd/(d - z).

Xs and Yx are the 'perspective' coordinates in the planes that you might want to use to graph your image on a flat 2D Screen.

(Mortenson, page 512, 513).
 

These equations can also be derived using planes and vector algebra.  See Dr. Shoaff's page at:
http://www.cs.fit.edu/~wds/classes/graphics/VTOP/vtop/
 
 

THERE'S STILL ONE PROBLEM:  Most of the time the observer will NOT be climbing up somewhere up on the +Z-axis, but will be at some ARBITARY point.  So what do you do?



Concept 2:  Rotation

Now if you've taken Calculus 2, you've probably banged your head with "rotating" or "unrotating" conic sections.

You may remember equations like:

Side Note 1
(Notice that the equations are solved for X & Y in terms of X' & Y'.  The reason for this is that X'-Y' is kind of the "rest frame" of the conic section in which it is not at any angle at all, and we want to find out what X & Y in the 'real world' will come out to when the points are all rotated by .  Also it makes it easy to substute into a large conic of the form

A*X^2 + B*X*Y + C*Y^2 + D*X + E*Y + F = 0

and grind it out to find

A*X'^2 + C*Y'^2 + D*X' + E*Y' + F = 0

Incidently my equations up at the top of the page are solved for X' Y' Z', but in a way it's kind of the same idea since I'm "moving the points to the observer's 'rest frame' where you're looking down from the +Z-axis.  But we'll get to this soon enuogh.)
 
 

Now in Linear Algebra we might write this as:
  (Leon page 207)
 

Side Note 2
It turns out that these are the equations for rotating any point between any 2 axies.  A good explination of why this is true can be found in Classical Dynamics of particles and systems by Marion and Thorton on pages 3-9.  It even has a general way of working out the elements of the matricies.  If you consider X Y Z to be X1 X2 X3 and Xi to be a generic variable where i=1,2,3, then

Xi'SigmaLambdaij*Xj

Where ij is the element in the ith row and jth column of the transformation matirx.

Lambdaij=cos(X'i<Xj)  -- the cosine of the angle between X'i and Xj

However, please take special note that the equation is solved for Xi', not Xi.  IF you want to solve for Xi you must take the transpose of the matrix, or change your formula to Lambdaij=cos(Xi<X'j) -- notice that the ' is put with the second generic variable axis.

If you don't understand this don't worry.  The orange and blue sections are just little asides I decided to throw in.
 

Now it kind of makes sense that these equations should  have general validity even without looking up all of the complicated explinations in a Classical mechanics book.  After all "X" and "Y" are  just names we use to keep track of certain variables.  We could have just as well called them "T" and "J" or "Q" and "R" or "Y" and "Z."

So then if the general matrix for rotating points Theata degrees from the posative X-Axis toward the posative Y-Axis is:

Then the general matrix for rotating a point Gamma degrees from the posative Y-Axis toward the posative Z-Axis is:

And the general matrix for rotating a point Beta degrees from the posative X-Axis toward the posative Z-Axis is:
***
 

***However you will often see the X-Z matix written differently.  If you measure then angle from the Z-axis instead of the X-axis, then PhiPI/2 - Beta as in the picture at the top of the paper, and all of the sines and cosines will be swapped.  Also you want to move the point "clockwise" toward the posative X-axis instead of "counter-clockwise" then you replace Beta with -Beta which means the "+/- signs" in front of the "sines" will change but  the "+/- signs" in front of the "cosines" will not.
(Mortenson page 350).



Concept 3:  Finding the Right Rotation

Basically What you need to do is to rotate things such that the coordinates of the observer [(XE,YE,ZE) for "eye" of the observer] are on the posative Z axis looking down.

To do this we will rotate the X & Y coordinates of all the points being observed to the negative Y axis.  If Theata is the angle in the X-Y plane with the posative X-axis, then PI/2 - Theata must be the angle the rest of the way to the Y-axis.  From there you have another PI or 180 degrees to go around to the negativeY-axis. PI/2 - TheataPI is  3PI/2 - Theata so the rotation matrix is:

Then we need to rotate all three X Y Z  coordinate up to the posative Z axis.  If we think of the observer as being at the end of R3 which makes an angle of Phi with the posative Z axis, then we rotate by -Phi.  Again, it's -Phi because we're rotating clockwise instead of the "standard" counterclockwise.

Which gives us the equation:

OR

If you convert this to X Y Z coordinates with R2 & R3 you get:




Concept 4: Physical Interpetation

There are a few things we need to understand and look out for.

(1)  One of my early misconceptions was that once I figured out what angles to rotate by, I then had to take the opposites of them before I plugged them into the general matricies.  I got this idea from thinking about when you walk around a car or any physical object, it appears to move in  the opposite direction of what you're moving in.  HOWEVER REMEMBER IT'S COORDINATE SYSTEM WE'RE MOVING HERE, SO ACTUALLY THE SITUATION IS ACTUALLY JUST LIKE SITTING ON A SEA-SAW AND ROCKING UP AND DOWN - IF YOU ONLY LOOK AT THE PERSON ON THE OTHER SIDE [AND YOUR INNER EARS AREN'T WORKING] THEN YOU DON'T NOTICE ANYTHING MOVING].

(2) One of  my other misconceptions was that after I had to multiply by a 3rd matix un-doing what the first matrix did with Theata. But actually since everything is being graphed from the observer's view point, we actually want to leave it the way it is so that "vertical" points will graph up and down the Y axis properly.

(3)  Why rotate to the Negative Y-Axis???  Well, the one basic assumption of these equations is that in the end the points are going to be graphed on a screen with X & Y axies.  It also assumes that the X axis will be posative to the right side of the screen, and that the Y axis will be posative to the top of the screen.  To make this work, you need to rotate to the negative Y-axis and then up to the posative Z-axis ........... unless of course you plan to turn your calculator or computer screen upside down or stand on your head :o)

(4)  Projectoins don't move points that are already inside the 'projection plane.'

These equations actually describe a projection that "maps" points in 3D space down to the 2D X-Y plane.  Points with Z'=0 will not be moved.  (The Z' is important because it's the Z after the rotations were made.)

Therefore the equations assume that objects in the "view plane" are already at the perfect size for the observer's eye.



Concept 5:  Homogenous matrices

Now the whole idea of this is to go from  3D coordinate (X,Y,Z) down to 2D coordinates (X,Y), yet believe it or not, there is actually a reason to go up to 4Dimentional matricies.  This is because if you multiply a 4X4 matirx by a 4X1 vector:

 

Then TX will be added to X, TY will be added to Y and TZ will be added to Z.  This way you can "translate" or move around your points while doing your matrix multplication.  And it turns out that this is faster than adding vectors to the points first and then rotating them by the matrix on the TI-85 and TI-86 calculators.

You can also "stretch" or "scale" you points by placing scale factors along the diagonal instead of 1's:

This can be useful if you want to manipulate a set of points when you're graphing it -- say to turn a sphere into an ellipsoid.
(Mortenson page 367,368).

These can by conbined with the matrix transformatoins above as:

Notice that the "stretch" is done before the translation.  This is important because if you do it the other way you're actually moving the center of the object by (TX*SX, TY*SY, TZ*SZ).

If you expand this out you get:



Update May 6, 2003.

As you can find in any good calclulus book (E.g. Larson et al p752-3), the symetric equations for a line in 3-space are:

Using  the point to be projected (Xo,Yo,Zo) as point 1, and the eye point (XE,YE,ZE) as point 2, and applying some fairly 'simple' vector algebra, it's not too hard to find a 3D line between the two points.  To project the point (Xo,Yo,Zo) to the X-Y plane, you 'just' plug in zero for Z, and solve for X & Y to get:

I don't know why I didn't see this sooner!!!  This  means that you should be able to project points from ANY perspective without using any matrix rotations at all!!!  However, you may still want to do matrix multiplication to stretch & move the 'basic object'.   You may then want to multiply by some general X-Y & Y-Z matricies to rotate the 'basic object' before you "take it's photograph."

This having been said, I think the method above of rotating all the points so that the observer is looking down the +Z axis will run FASTER on a computer [as long as you're planing to use matrix manipulations at all] since you only have to create the total matrix ONCE, before the loop, and this new method requires 2 extra multiplications, and 2 extra subtractions in the middle of all the loops.


Biblography

http://www.cs.fit.edu/~wds/classes/graphics/Lectures/lectures/
    http://www.cs.fit.edu/~wds/classes/graphics/VTOP/vtop/

Larson, Roland E., Robert P. Hostetler and Bruce H. Edwards. Calculus: with Analytic Geometry.
        5th ed.  Lexington:  D.C. Heath and Company, 1994.

Leon, Steven J. Linear Algebra: with Applications. 6th ed. Upper Saddle River, NJ: Prentice Hall, Inc. 2002.

Marion, Jerry B, and Stephen T. Thornton. Classical Dynamica of Particles and Systems.  4th ed.
        New York:  Harcourt Brace & Co.,  1995.

Mortenson, Michael E.  Geometric Modeling.  New York:  John Wiley & Sons,  1985.