Why does my BASIC rotation function not work?

Viewed 30

I wrote a program to rotate a checkerboard floor:

00 CLS
01 B = (A  * PI)/180
02 Y = 1
03 Z = ((256/Y) * COS(B)) - (((256*X)/(256*Y)) * SIN(B))
04 X = -255
05 W = ((256/Y) * SIN(B)) + (((256*X)/(256*Y)) * COS(B))
06 COLOR 2 + (8 * ((INT(Z) + INT(W)) MOD 2))
07 PSET (INT(X/2) + 128,128 + INT(Y/2))
08 X = X + 2
09 IF X<256 THEN GOTO 05
10 Y = Y + 2
11 IF Y<256 THEN GOTO 03
12 A = ((A + 1) MOD 360)
14 GOTO 00

However, instead of rotating, it distorts said floor bizarrely. How can I fix the code above?

1 Answers

I wrote a program to rotate a checkerboard floor:

If this were the case then I would have expected to see a nice checkerboard floor when the angle was still set at 0. Sadly this was not the case, so the real problem is not only with the rotation!

Next code written for QBasic draws a green checkerboard floor of 320x320 pixels. The (logical) center is located at (0,0) and I'm using a simple coordinates shift towards the middle of the screen for optimal visualization.

SCREEN 12
FOR y = -160 TO 159
  b% = INT((y + 160) / 40)
  FOR x = -160 TO 159
    PSET (320 + x, 240 + y), 2 + 8 * ((INT((x + 160) / 40) + b%) MOD 2)
  NEXT x
NEXT y
DO
LOOP UNTIL INKEY$ = CHR$(27)

However, instead of rotating, it distorts said floor bizarrely. How can I fix the code above?

For rotating, you need next formulas that transform coordinates:

xx = x * cos(a) - y * sin(a)
yy = x * sin(a) + y * cos(a)

These formulas remain easy because I will be rotating around the origin of the screen at (0,0). Only as a last step will I shift the points towards their final destination which is the center of the screen. A negative angle is used because the screen is a left-hand coordinate system; where Y increases from top to bottom.

SCREEN 12
FOR angle = 0 TO 360 STEP 10
  d% = ((d% + 1) AND 7)
  LOCATE 1, 1: PRINT "Angle :"; angle
  a = angle * -3.141592653589793# / 180
  cosine = COS(a)
  sine = SIN(a)
  FOR y = -80 TO 79
    b% = INT((y + 80) / 20)
    FOR x = -80 TO 79
      c% = d% + 8 * ((INT((x + 80) / 20) + b%) MOD 2)
      PSET (320 + (x * cosine - y * sine), 240 + (x * sine + y * cosine)), c%
    NEXT x
  NEXT y
  IF INKEY$ <> "" THEN EXIT FOR
NEXT angle
DO
LOOP UNTIL INKEY$ = CHR$(27)

It's important to note that the decision about the pixel color is made based on the original coordinates, unlike in your program where that decision was based on the transformed coordinates.
The code snippets end when the user presses the ESC key.

Screenshot

Related