CUBE.BAS (1/2)

 BBS: Inland Empire Archive
Date: 09-22-93 (17:04)             Number: 21
From: IAN REMMLER                  Refer#: NONE
  To: CALVIN FRENCH                 Recvd: NO  
Subj: CUBE.BAS (1/2)                 Conf: (2) Quik_Bas
 Hey Calvin!

 Well, here's a little example of how I accomplished 3D rotation.
 Like I said, I'm not sure this is exactly how it's supposed to
 be done, but the cube rotates and it stays the shape of a cube
 so I guess it's good enough. I didn't take the time to comment
 it so if you don't understand why I did something just ask.

'>>> Page 1 of CUBE.BAS begins here. TYPE:BAS
DEFINT A-Z

CONST PI = 3.14159265358979#

TYPE coord
  x1 AS INTEGER
  y1 AS INTEGER
  z1 AS INTEGER
  x2 AS INTEGER
  y2 AS INTEGER
  z2 AS INTEGER
END TYPE

DIM pnt(11) AS coord
DIM rot(1, 11) AS coord
DIM tmp AS coord

DIM sn&(359), cs&(359)

FOR n = 0 TO 11
  READ pnt(n).x1, pnt(n).y1, pnt(n).z1, pnt(n).x2, pnt(n).y2, pnt(n)..z2
NEXT

SCREEN 9
WINDOW (-320, -240)-(320, 240)

FOR n = 0 TO 359
  sn&(n) = SIN(n / 180 * PI) * 1024
  cs&(n) = COS(n / 180 * PI) * 1024
NEXT

sx = SGN(RND - .5)
sy = SGN(RND - .5)
GOSUB RandRot

DO
  s = 1 - s
  SCREEN , , s, 1 - s

  FOR n = 0 TO 11
    LINE (rot(s, n).x1, rot(s, n).y1)-(rot(s, n).x2, rot(s, n).y2), 0
  NEXT

  x = x + 10 * sx: y = y + 10 * sy
  IF ABS(x) > 240 THEN sx = -sx: GOSUB RandRot
  IF ABS(y) > 140 THEN sy = -sy: GOSUB RandRot
  a = (a + da) MOD 360: b = (b + db) MOD 360: c = (c + dc) MOD 360

  FOR n = 0 TO 11
    'The following are based on these 2 formulas
          'xnew = cos(angle)*x + sin(angle)*y
          'ynew = -sin(angle)*x + cos(angle)*y

    tmp.z1 = (cs&(a) * pnt(n).z1 + sn&(a) * pnt(n).y1) / 1024
    tmp.y1 = (-sn&(a) * pnt(n).z1 + cs&(a) * pnt(n).y1) / 1024
    tmp.z2 = (cs&(a) * pnt(n).z2 + sn&(a) * pnt(n).y2) / 1024
    tmp.y2 = (-sn&(a) * pnt(n).z2 + cs&(a) * pnt(n).y2) / 1024

    tmp.x1 = (cs&(b) * pnt(n).x1 + sn&(b) * tmp.z1) / 1024
    rot(s, n).z1 = (-sn&(b) * pnt(n).x1 + cs&(b) * tmp.z1) / 1024
    tmp.x2 = (cs&(b) * pnt(n).x2 + sn&(b) * tmp.z2) / 1024
    rot(s, n).z2 = (-sn&(b) * pnt(n).x2 + cs&(b) * tmp.z2) / 1024

    rot(s, n).x1 = (cs&(c) * tmp.x1 + sn&(c) * tmp.y1) / 1024 + x
    rot(s, n).y1 = (-sn&(c) * tmp.x1 + cs&(c) * tmp.y1) / 1024 + y
    rot(s, n).x2 = (cs&(c) * tmp.x2 + sn&(c) * tmp.y2) / 1024 + x
    rot(s, n).y2 = (-sn&(c) * tmp.x2 + cs&(c) * tmp.y2) / 1024 + y

    LINE (rot(s, n).x1, rot(s, n).y1)-(rot(s, n).x2, rot(s, n).y2), 1
  NEXT
LOOP UNTIL LEN(INKEY$)

END

RandRot:
'>>> Page 1 of CUBE.BAS ends here. Continued on next page.

--- Blue Wave/QBBS v2.12 [NR]
 * Origin: Dusty's Palace (210)-625-4479 New Braunfels, TX (1:387/1001.0)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET