Normalized Angle to a range from 0 to 360

Share your advanced PureBasic knowledge/code with the community.
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Normalized Angle to a range from 0 to 360

Post 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
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post 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
Anthony Jordan
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Post by eddy »

is it a cross-platform code ?
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

eddy wrote:is it a cross-platform code ?
On Intel it should work, on PPC it shouldn't.
bye,
Daniel
Post Reply