Page 1 of 1

Normalized Angle to a range from 0 to 360

Posted: Sat Aug 29, 2009 10:00 pm
by eddy

Code: Select all

Procedure.f NormalizedAngle(degrees.f)
   If degrees<0 or degrees>359
      degrees-Int(degrees/360.0)*360.0
      If degrees<0 : degrees+360 : EndIf
   EndIf
   ProcedureReturn degrees
EndProcedure

NewList a.f()
AddElement(a()) : a()=370.5
AddElement(a()) : a()=10.5
AddElement(a()) : a()=-370.5
AddElement(a()) : a()=-10.5

ForEach a()
   Debug NormalizedAngle(a())
Next

Posted: Sun Aug 30, 2009 10:11 am
by akj
Here is another (previously published) way of doing it.

Code: Select all

Procedure.f WrapValue(angle.f)
; Wrap a value to the range [0..360)
; www.purebasic.fr/french/viewtopic.php?p=92402
!FILD dword[@f] ; st0 = 360
!FLD dword[p.v_angle]
!FPREM
!FADD st1,st0
!FLDZ
!FCOMIP st1
!FCMOVNBE st0,st1
!FSTP st1
ProcedureReturn
!@@:DD 360
EndProcedure

Posted: Sun Aug 30, 2009 3:24 pm
by eddy
is it a cross-platform code ?

Posted: Sun Aug 30, 2009 4:05 pm
by DarkDragon
eddy wrote:is it a cross-platform code ?
On Intel it should work, on PPC it shouldn't.