Qbasicnews.com

Full Version: Rotating a point
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
I'm trying to rotate a point around the center. I'm having trouble though, here is the code (BTW, I'm very new at 3d programming, although this is 2d right now):

Code:
SCREEN 13

TYPE PointType
x AS INTEGER
y AS INTEGER
END TYPE

CONST PI = 3.14

DIM p AS PointType             'The point to be rotated
DIM theta AS INTEGER           'Current angle of the pixel
DIM xCenter AS INTEGER         'The xCenter of the screen
DIM yCenter AS INTEGER         'The yCenter of the screen
DIM LENS AS INTEGER            'The z value of our LENS

FOR i = 0 TO 360          'Set up the Cosine and Sine tables
  c!(i) = COS(i * PI / 180)
  s!(i) = SIN(i * PI / 180)
NEXT i

xCenter = 160              'Set the symbolic constants
yCenter = 100
LENS = 256

p.x = 1
p.y = 0
theta = 0

DO WHILE INKEY$ <> ""
nx = p.x * c!(theta) - p.y * s!(theta)
ny = p.y * c!(theta) + p.x * s!(theta)
PSET (p.x, p.y), 0
p.x = nx
p.y = ny
PSET (x, y), 15
theta = theta + 1
IF theta >= 360 THEN
theta = 0
END IF
LOOP

I'm getting a "Subscript out of range" error when setting up the Cosine and Sine tables. I probably have 100's of errors in this code, so bare with me.

Also, how do I turn on the Syntax highlighting when using the Code display?
you have to dim a matrix/array if its over 3 units in size. so:
Code:
DIM c(360), s(360)
I'd put an AS in there, but I'm not sure what a ! is a shortcut for...
Here is some code, tried in FB, should work in QB.

Things I changed:

* added the arrays s! and c! to store the sin and cos tables in.
* added xCenter and yCenter to the rotated values
* removed the p.x = nx and p.y = ny - this is unnecessary; the only thing you need to be modifying is theta.
* added a bit of a delay so you can see it rotating

[syntax="QBasic"]
SCREEN 13

TYPE PointType
x AS INTEGER
y AS INTEGER
END TYPE

CONST PI = 3.14

DIM p AS PointType 'The point to be rotated
DIM theta AS INTEGER 'Current angle of the pixel
DIM xCenter AS INTEGER 'The xCenter of the screen
DIM yCenter AS INTEGER 'The yCenter of the screen
DIM LENS AS INTEGER 'The z value of our LENS
dim c!(0 to 360) ' cos table
dim s!(0 to 360) ' sin table

FOR i = 0 TO 360 'Set up the Cosine and Sine tables
c!(i) = COS(i * PI / 180)
s!(i) = SIN(i * PI / 180)
NEXT i

xCenter = 160 'Set the symbolic constants
yCenter = 100
LENS = 256 ' what's this?

p.x = 5
p.y = 5
theta = 0

DO WHILE INKEY$ = ""

' erase the old point
pset (nx, ny), 0

' calculate the rotated point using the current angle
nx = xCenter + (p.x * c!(theta) - p.y * s!(theta))
ny = yCenter + (p.y * c!(theta) + p.x * s!(theta))

' draw the rotated point
PSET (nx, ny), 15

' add 10 since adding 1 is too slow
theta = theta + 10
IF theta >= 360 THEN theta = 0

' a small delay
start! = timer
while timer < start! + 0.1: wend

LOOP
[/syntax]
Big Grin Thanks Shift. I see my mistakes. Looks like I got most of the other stuff working ok. The "LENS" constant is for use in 3D rotations, which I plan to do next. It's just to get used to using it.
I'll have to try using it when I get back on my computer. Thanks again.

[Edit]Deleter, "!" is the shortcut for Single, and "#" is the shortcut for Double.[/edit]
Well...
I did the code you sent me, and it worked! Big Grin Thank you. I can now do a 2d rotation without a problem. A milestone in the world of 3d programming!

But...
Now I'm trying to do a 3d rotation. I'm rotating a line around a point (the center of the screen), but when I run the program (no QB errors, yay) all it does is display a dot in the center of the screen Sad . If you don't mind helping me one more time:

Code:
SCREEN 13

TYPE Vector
  x AS INTEGER
  y AS INTEGER
  z AS INTEGER
END TYPE

CONST xCenter = 160
CONST yCenter = 100
CONST LENS = 256
CONST PI = 3.14

DIM v AS Vector
DIM c!(360)
DIM s!(360)
DIM theta AS INTEGER

FOR i = 0 TO 360
  c!(i) = COS(i * PI / 180)
  s!(i) = SIN(i * PI / 180)
NEXT i


v.x = 5
v.y = 5
v.z = 5
theta = 0

DO WHILE INKEY$ = ""
  'Erase old line
  LINE (xCenter, yCenter)-(px, py), 0

  'X axis
  ny = y * c!(theta) - z * s!(theta)
  nz = z * c!(theta) + y * s!(theta)
  y = ny
  z = nz

  'Y axis
  nx = x * c!(theta) - z * s!(theta)
  nz = z * c!(theta) + x * s!(theta)
  x = nx
  z = nz

  'Z axis
  nx = x * c!(theta) - y * s!(theta)
  ny = y * c!(theta) + x * s!(theta)
  
  
  rx = nx
  ry = ny
  rz = nz

  'Convert to 2d
  d = LENS - z
  px = xCenter + (LENS * x / d)
  py = yCenter - (LENS * y / d)

  'Draw the new line
  LINE (xCenter, yCenter)-(px, py), 15

  'Increase the angle

  theta = theta + 1

  'Delay between renders
  start! = TIMER
  WHILE TIMER < start! + .001: WEND

LOOP

Thank you. Tongue
Anyone care to help? :-?

Anonymous

dunno shi'ite about this, but from what i cn tell ur "v" TYPE Vector varibale isnt even being used, only initialize din the beginning..?

seems rx, ry, rz; nx, ny, nz ahould be dimmed as the vector type.

maybe its cuz in the beginning you put 5's into that vector, but then use a totally other var that that vector (that would equal 0). sounds logical that if you have a non-moving point, SOMETHING is probably at 0, lol
Well, here is the new code, it still doesn't seem to be rotating right. Rel, Syn9, either of you 3D guru's care to help?

Code:
SCREEN 13

TYPE Vector
  x AS INTEGER
  y AS INTEGER
  z AS INTEGER
END TYPE

CONST xCenter = 160
CONST yCenter = 100
CONST LENS = 256
CONST PI = 3.14

DIM v AS Vector
DIM c!(360)
DIM s!(360)
DIM theta AS INTEGER

FOR i = 0 TO 360
  c!(i) = COS(i * PI / 180)
  s!(i) = SIN(i * PI / 180)
NEXT i


v.x = 25
v.y = 25
v.z = 10
theta = 0

DO WHILE INKEY$ = ""
  'Erase old line
  LINE (xCenter, yCenter)-(px, py), 0

  'X axis
  ny = v.y * c!(theta) - v.z * s!(theta)
  nz = v.z * c!(theta) + v.y * s!(theta)
  y = ny
  z = nz

  'Y axis
  nx = v.x * c!(theta) - v.z * s!(theta)
  nz = v.z * c!(theta) + v.x * s!(theta)
  x = nx
  z = nz

  'Z axis
  nx = v.x * c!(theta) - v.y * s!(theta)
  ny = v.y * c!(theta) + v.x * s!(theta)
  
  
  rx = nx
  ry = ny
  rz = nz

  'Convert to 2d
  d = LENS - z
  px = xCenter + (LENS * x / d)
  py = yCenter - (LENS * y / d)

  'Draw the new line
  LINE (xCenter, yCenter)-(px, py), 15

  'Increase the angle

  theta = theta + 5

  IF theta > 360 THEN
    theta = 0
  END IF

  'Delay between renders
  start! = TIMER
  WHILE TIMER < start! + .001: WEND

LOOP

And once again, how do I turn on the Qbasic Syntax Highlighting?

Anonymous

its rotating, youre just not "looking" at it from a front on angle
Oh, I get you. I thought about that. Thanks! Big Grin
Pages: 1 2