Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- r.arctan: ;in: HL=(x1,y1) DE=(x2,y2)
- ld b,0 ;initializing...
- ld a,e ;
- rrca \ rrca \ and %00111111
- sub L ;y1-y2
- jr nc,_ ;result is negative. Set B for flags (bit 0) and adjust Y for pos.
- set 0,b
- neg
- _:
- ld e,a ;D=Y for later
- rlca \ rlca ;shifting so that %00yyyynn is %yyyynn00 for later. n matters not
- ld L,a ;half-result in L now
- ld a,d
- rrca \ rrca \ and %00111111
- sub H ;x1-x2
- jr nc,_ ;result is negative. Set B for flags (bit 1) and adjust X for pos.
- set 1,b
- neg
- _:
- ld c,a ;C=X for results later
- rrca \ rrca ;shifting so that %00xxxxnn is %nn00xxxx. n still matters not.
- xor L ;combine with Y
- and $0F ;mask out to keep old X low bits
- xor L ;and we have %yyyyxxxx
- ld L,b ;saving flags into L for now.
- ld h,e ;restore H for... stuffs
- ld ix,ArcTanTable
- ld e,a
- ld d,0
- add ix,de ;We have our offset now.
- ;So at this point, C=X, H=Y, L=flags.
- ;Let's go ahead and do X-interpolation first.
- ld a,c
- and %00000011 ;value-ranking
- ld c,(ix+0) ;storing non-adjusted "output" value into C
- jr z,r.arctan.skipx
- ld e,a ;Loop for second half of X interpolate
- cpl ;ranking first value on inverse of distance to next number
- add a,5 ;So 1=3,2=2,3=1
- ld b,a
- xor a
- _:
- add a,c
- djnz -_
- ld c,(ix+1)
- ld b,e
- _:
- add a,c
- djnz -_
- rra \ rra
- and %00111111
- ld c,a ;out by the time we're done with everything.
- r.arctan.skipx:
- ld a,h
- ld H,c ;H=InterpolatedX
- and %00000011 ;value-ranking
- ld c,(ix+0) ;storing non-adjusted "output" value into C
- jr z,r.arctan.skipy
- ld e,a ;Loop for second half of Y interpolate
- cpl ;ranking first value on inverse of distance to next number
- add a,5 ;So 1=3,2=2,3=1
- ld b,a
- xor a
- _:
- add a,c
- djnz -_
- ld c,(ix+16)
- ld b,e
- _:
- add a,c
- djnz -_
- rra \ rra ;Also trying to get any leftovers from too many values of 64
- cp 64
- jr z,_
- and %00111111
- _:
- ld c,a ;added. Since 64*4 is a 9 bit number, getting that back.
- r.arctan.skipy:
- ld a,c
- add a,H
- rra
- cp 64
- jr z,_
- and %00111111 ;and NOW we have our final angle. (if it wasn't 64...)
- _:
- ld b,0 ;Set to 128 if X is negative. For additions later on.
- bit 1,L
- jr z,_
- ld b,128
- neg ;negate angle
- _:
- bit 0,L
- jr z,_
- neg ;negate angle again if (-,-) else first neg. Still correct :)
- _:
- add a,b ;completing the angle
- ld c,a ;saving the completed angle to C
- ret
Advertisement
Add Comment
Please, Sign In to add comment