Assembly code snippets
Details
Title | Angle from delta values |
---|---|
Author | Thomas |
Submitted by: | Thomas |
Date added: | 2002-02-18 15:57:14 |
Date modified: | 2002-02-18 15:57:14 |
Comments
This proc takes the horizontal and vertical delta values of two points and returns the angle between them in tenths of degrees (range 0-3599). I just converted some C example I found on the web (didn't mention the original author).
Coordinates and angles are defined like this:
positive Y: up, negative Y: down, positive X: right, negative X: left. Angles: up: 0, right: 900, down 1800, left 2700.
Snippet
; Converts horizontal and vertical delta values into
; tenths of degrees (range 0-3599).
; Converted from C example by unkown author.
DeltaToAngle proc deltaX:DWORD, deltaY:DWORD
LOCAL temp:DWORD
.data
dta_rangeval_000 REAL4 1800.0
.code
mov ecx, deltaX
mov edx, deltaY
or edx, edx
jnz @dta1
mov eax, 900
cmp ecx, 0
jg @F
mov eax, 2700
@@:
ret
@dta1:
xor eax, eax
or ecx, ecx
jnz @dta2
cmp edx, eax
jg @F
mov eax, 1800
@@:
ret
ret
@dta2:
finit
fild deltaY
fild deltaX
fpatan ;ST(0)=angle in radians
fldpi ;ST(1)=angle in radians, ST(0)=pi
fdivp ST(1),ST(0) ;ST(0)=angle/pi
fmul dta_rangeval_000 ;ST(0)=(angle/pi) * 1800 = angle in degrees (*10)
fistp temp ;temp=ST(0)=angle in degrees (*10)
mov eax, 900
sub eax, temp
@@:
cmp eax, 0
jge @F
add eax, 3600
jmp @B
@@:
@@:
cmp eax, 3600
jle @F
sub eax, 3600
jmp @B
@@:
ret
DeltaToAngle endp
; tenths of degrees (range 0-3599).
; Converted from C example by unkown author.
DeltaToAngle proc deltaX:DWORD, deltaY:DWORD
LOCAL temp:DWORD
.data
dta_rangeval_000 REAL4 1800.0
.code
mov ecx, deltaX
mov edx, deltaY
or edx, edx
jnz @dta1
mov eax, 900
cmp ecx, 0
jg @F
mov eax, 2700
@@:
ret
@dta1:
xor eax, eax
or ecx, ecx
jnz @dta2
cmp edx, eax
jg @F
mov eax, 1800
@@:
ret
ret
@dta2:
finit
fild deltaY
fild deltaX
fpatan ;ST(0)=angle in radians
fldpi ;ST(1)=angle in radians, ST(0)=pi
fdivp ST(1),ST(0) ;ST(0)=angle/pi
fmul dta_rangeval_000 ;ST(0)=(angle/pi) * 1800 = angle in degrees (*10)
fistp temp ;temp=ST(0)=angle in degrees (*10)
mov eax, 900
sub eax, temp
@@:
cmp eax, 0
jge @F
add eax, 3600
jmp @B
@@:
@@:
cmp eax, 3600
jle @F
sub eax, 3600
jmp @B
@@:
ret
DeltaToAngle endp