'=========================================================================== ' Subject: COMPUTE 3D COORDINATES Date: 09-23-99 (08:21) ' Author: Robert L. Roach Code: QB, QBasic, PDS ' Origin: rroach@cvd-pro.com Packet: ALGOR.ABC '=========================================================================== 'SPCOORDS - My utility program that gets used in all my 3D grafix. It 'assumes an xyz coordinate system with the z-plane (or x-y plane) on the 'ground. The viewpoint is defined by phi, theta, and r (angle around the 'z-axis from the x-axis, angle above the z-plane, and radius from origin. 'Points on an object are then projected onto a plane perpendicular to the 'line of sight through the origin. Perspective arises if you are close to 'the object. SPCOORDS consists of two QuickBASIC subroutines, both very 'short, in fact, I've added it below for your mirth. 'Below is the essence of SPCOORDS. You define the viewpoint, GOSUB 1000 to 'compute several terms which result from the viewpoint and then use GOSUB '2000 to get the projected coordinates (XX,YY) from any (XP,YP,ZP) point. 'GOSUB 1000 is separated from GOSUB 2000 for computational efficiency. '-the following would be somewhere in your main program: REM----- GET VIEWPOINT PI = 3.141592654 PHI = 15. 'Angle around z-axis from x-axis THETA = 20. 'Angle up from x-y plane (elevation) RADIUS = 2000. GOSUB 1000 -these are the two GOSUBs: 1000 REM--------------- VIEW ANGLE --------------------- REM This routine computes stuff for GOSUB 2000 REM which depend only on the viewpoint and only REM needs to be called once for each viewpoint. REM-------------------------------------------------------------- RV = RADIUS PH = PI * PHI / 180 TH = PI * THETA / 180 CP = COS(PH) SP = SIN(PH) CT = COS(TH) ST = SIN(TH) XV = RV * CP * CT YV = RV * SP * CT ZV = RV * ST REM---------------------------------------------- RETURN 2000 REM---------- SPACECOORDS -------------------- REM REM This subroutine computes the 3d coords. Given REM (XP,YP,ZP) the projected coords (XX,YY) are REM returned. REM REM------------------------------------------------ RP = (XP * XV + YP * YV + ZP * ZV) / RV XR = RP * CP * CT YR = RP * SP * CT ZR = RP * ST DX = (XP - XR) * (RV + RP) / RV DY = (YP - YR) * (RV + RP) / RV DZ = (ZP - ZR) * (RV + RP) / RV XX = -DX * SP + DY * CP YY = -(DX * CP + DY * SP) * ST + DZ * CT RETURN