Geometry made easy with the Floating Point Processor

Started by Charles Pegge, July 12, 2007, 01:18:07 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Charles Pegge

Converting Cartesian to Polar Coordinates and back again

PowerBasic


' PolarCart
' Full 360 Degree Geometry
' Two complemenatary functions converting cartesian
' coordinates to polar coords and vice versa.
'
' avoids problems with quadrants and also avoids tan infinity

' Charles E V Pegge
' 12 July 2007

' PowerBasic ver 8x

#COMPILE EXE
#DIM ALL

SUB RadAng(BYREF v AS DOUBLE )

' replaces x and y coordinates with radius and angle (in radians)

#REGISTER NONE

'--------------------------'
  ! mov ebx,v              ' get pointer for vector v
  ! fld qword ptr [ebx+8]  ' y coordinate
  ! fld qword ptr [ebx]    ' x coordinate
  ! fld st(1)              ' load copy of y
  ! fmul st(0),st(0)       ' square y
  ! fld st(1)              ' load copy of x
  ! fmul st(0),st(0)       ' square x
  ! faddp st(1),st(0)      ' add x squared to y squared and pop
  ! fsqrt                  ' square root the result
  ! fstp qword ptr [ebx]   ' store this radius and pop
'--------------------------'
                           ' we are now left with the original y and x on the fpu stack
  ! fpatan                 ' radians arctan(y/x) and pop x off the stack
  ! fstp qword ptr [ebx+8] ' store this angle and pop
'--------------------------'
END SUB

SUB XandY(BYREF v AS DOUBLE )

' Replaces radius and angle (in radians) with X and Y coordinates

#REGISTER NONE

'--------------------------'
  ! mov ebx,v              ' get vector pointer
  ! fld qword ptr [ebx]    ' get radius
  ! fld qword ptr [ebx+8]  ' get angle
  ! fsincos                ' convert to sine and cosine
  ! fmul st(0),st(2)       ' multiply by radius
  ! fstp qword ptr [ebx]   ' store x and pop
  ! fmul st(0),st(1)       ' multiply by radius
  ! fstp qword ptr [ebx+8] ' store y and pop
  ! fcomp st(0)            ' pop radius off stack and discard
'--------------------------'
END SUB




FUNCTION PBMAIN () AS LONG: DIM i AS LONG

' test the functions

DIM v(15) AS DOUBLE

v(2)=3  ' X
v(3)=-4 ' Y

radang(v(2))

MSGBOX "Radius:"+STR$(v(2))+"  angle:"+STR$(v(3))

XandY(v(2))

MSGBOX "X:"+STR$(v(2))+"  Y:"+STR$(v(3))


END FUNCTION


Kent Sarikaya

I just got my PowerBasic tonight Charles, Nice to see that routine, I am sure I will be using those sometime in the future.
Hope to get up to speed with PowerBasic and contribute more in the coming months.