Geekboy

Untitled

Mar 30th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. r.arctan: ;in: HL=(x1,y1) DE=(x2,y2)
  2.  ld b,0        ;initializing...
  3.  ld a,e        ;
  4.  rrca \ rrca \ and %00111111
  5.  sub L    ;y1-y2
  6.  jr nc,_  ;result is negative. Set B for flags (bit 0) and adjust Y for pos.
  7.  set 0,b
  8.  neg
  9. _:
  10.  ld e,a      ;D=Y for later
  11.  rlca \ rlca ;shifting so that %00yyyynn is %yyyynn00 for later. n matters not
  12.  ld L,a      ;half-result in L now
  13.  ld a,d
  14.  rrca \ rrca \ and %00111111
  15.  sub H     ;x1-x2
  16.  jr nc,_   ;result is negative. Set B for flags (bit 1) and adjust X for pos.
  17.  set 1,b
  18.  neg
  19. _:
  20.  ld c,a    ;C=X for results later
  21.  rrca \ rrca ;shifting so that %00xxxxnn is %nn00xxxx. n still matters not.
  22.  xor L     ;combine with Y
  23.  and $0F   ;mask out to keep old X low bits
  24.  xor L     ;and we have %yyyyxxxx
  25.  ld L,b    ;saving flags into L for now.
  26.  ld h,e    ;restore H for... stuffs
  27.  ld ix,ArcTanTable
  28.  ld e,a
  29.  ld d,0
  30.  add ix,de ;We have our offset now.
  31. ;So at this point, C=X, H=Y, L=flags.
  32. ;Let's go ahead and do X-interpolation first.
  33.  ld a,c
  34.  and %00000011 ;value-ranking
  35.  ld c,(ix+0)   ;storing non-adjusted "output" value into C
  36.  jr z,r.arctan.skipx
  37.  ld e,a  ;Loop for second half of X interpolate
  38.  cpl     ;ranking first value on inverse of distance to next number
  39.  add a,5 ;So 1=3,2=2,3=1
  40.  ld b,a
  41.  xor a
  42. _:
  43.  add a,c
  44.  djnz -_
  45.  ld c,(ix+1)
  46.  ld b,e
  47. _:
  48.  add a,c
  49.  djnz -_
  50.  rra \ rra
  51.  and %00111111
  52.  ld c,a       ;out by the time we're done with everything.
  53. r.arctan.skipx:
  54.  ld a,h
  55.  ld H,c     ;H=InterpolatedX
  56.  and %00000011 ;value-ranking
  57.  ld c,(ix+0)   ;storing non-adjusted "output" value into C
  58.  jr z,r.arctan.skipy
  59.  ld e,a  ;Loop for second half of Y interpolate
  60.  cpl     ;ranking first value on inverse of distance to next number
  61.  add a,5 ;So 1=3,2=2,3=1
  62.  ld b,a
  63.  xor a
  64. _:
  65.  add a,c
  66.  djnz -_
  67.  ld c,(ix+16)
  68.  ld b,e
  69. _:
  70.  add a,c
  71.  djnz -_
  72.  rra \ rra    ;Also trying to get any leftovers from too many values of 64
  73.  cp 64
  74.  jr z,_
  75.  and %00111111
  76. _:
  77.  ld c,a       ;added. Since 64*4 is a 9 bit number, getting that back.
  78. r.arctan.skipy:
  79.  ld a,c
  80.  add a,H
  81.  rra
  82.  cp 64
  83.  jr z,_
  84.  and %00111111 ;and NOW we have our final angle. (if it wasn't 64...)
  85. _:
  86.  ld b,0        ;Set to 128 if X is negative. For additions later on.      
  87.  bit 1,L
  88.  jr z,_
  89.  ld b,128
  90.  neg           ;negate angle
  91. _:
  92.  bit 0,L
  93.  jr z,_
  94.  neg           ;negate angle again if (-,-) else first neg. Still correct :)
  95. _:
  96.  add a,b       ;completing the angle
  97.  ld c,a        ;saving the completed angle to C
  98.  ret
Advertisement
Add Comment
Please, Sign In to add comment