Re: A86: polygons


[Prev][Next][Index][Thread]

Re: A86: polygons




>Does anyone know of a routine that would convert (X,Y,Z) coordinates
>into (X,Y) coordinates?  And also how to 3D rotate?

It depends whether you want to use perspective. You probably do.

If you don't, you can just disregard the z coordinate when graphing since
you'll be looking straight on the z-axis.

BUT, if you do want to use perspective, where farther objects appear
smaller... then you need to use a fairly simple equation:

x = x*pov/(pov-z)
y = y*pov/(pov-z)

x is right(+) and left(-)
y is up(+) and down(-)
z is toward(+) and away(-) from you
pov is the point of view, or the distance between the viewpoint
    and the origin along the z axis.  Just make sure it's a
    number bigger than the biggest possible z to alleviate any
    annoying division by zero.  You'll need to try values and
    find one that looks good.

To do 3D rotations you can use matrix multiplication.  I frequently get
this backwards, so be careful:-).

Rotate about the x-axis:
[[1, 0,           0]
 [0, cos(thetaX),-sin(thetaX)]
 [0, sin(thetaX), cos(thetaX)]]

Rotate about the y-axis:
[[ cos(thetaY), 0, sin(thetaY)]
 [ 0,           1, 0]
 [-sin(thetaY), 0, cos(thetaY)]]

Rotate about the z-axis:
[[cos(thetaZ),-sin(thetaZ), 0]
 [sin(thetaZ), cos(thetaZ), 0]
 [0,           0,           1]]


the rotation matrix is multiplied by the (X,Y,Z) coordinates in the form:

[[cos(thetaZ),-sin(thetaZ), 0]     [[X]      [[Xcos(thetaZ)-Ysin(thetaZ)]
 [sin(thetaZ), cos(thetaZ), 0]  *   [Y]   =   [Xsin(thetaZ)+Ycos(thetaZ)]
 [0,           0,           1]]     [Z]]      [Z]]

To give you the rotated points.

then for the point of view(let's use 25):

x = (Xcos(thetaZ)-Ysin(thetaZ))*25/(25-Z)
y = (Xsin(thetaZ)+Ycos(thetaZ))*25/(25-Z)


I'd advise precalculating the rotation matrix rather than recalculating
the trig functions for each point.  Also, you can multiply two or three
rotation matrices together and only multiply the point to be rotated
once.

I hope this helps.


Sincerely,
The Unibomer

Jared Ivey
Unibomer@Juno.com
http://www.geocities.com/SiliconValley/Vista/7342

"If A equals success, then the formula is A equals X plus Y plus Z.  X is
work.  Y is play.  Z is keep your mouth shut." -- Albert Einstein


_____________________________________________________________________
You don't need to buy Internet access to use free Internet e-mail.
Get completely free e-mail from Juno at http://www.juno.com
Or call Juno at (800) 654-JUNO [654-5866]


Follow-Ups: References: