Deprecated: Array and string offset access syntax with curly braces is deprecated in /homepages/21/d38531796/htdocs/jose/smfforum/Sources/Subs.php on line 3825
Geometry made easy with the Floating Point Processor

### Author Topic: Geometry made easy with the Floating Point Processor  (Read 5450 times)

0 Members and 1 Guest are viewing this topic.

#### Charles Pegge

• Global Moderator
• Hero Member
• Posts: 744
• User-Rate: +30/-1
##### Geometry made easy with the Floating Point Processor
« on: July 12, 2007, 01:18:07 PM »
Converting Cartesian to Polar Coordinates and back again

PowerBasic

Code: [Select]
`' 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 ALLSUB 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 SUBSUB 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 SUBFUNCTION PBMAIN () AS LONG: DIM i AS LONG' test the functionsDIM v(15) AS DOUBLEv(2)=3  ' Xv(3)=-4 ' Yradang(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`
« Last Edit: July 13, 2007, 10:20:00 AM by Charles Pegge »

#### Kent Sarikaya

• Full Member
• Posts: 173
• User-Rate: +8/-4
##### Re: Geometry made easy with the Floating Point Processor
« Reply #1 on: July 13, 2007, 05:32:45 AM »
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.