base:approximation_to_distance
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
base:approximation_to_distance [2020-02-09 14:39] – created djmips | base:approximation_to_distance [2020-02-11 01:45] (current) – djmips | ||
---|---|---|---|
Line 7: | Line 7: | ||
+ | The formula is d = max(|xd|, |yd|) + 1/2 × min(|xd|, |yd|) where xd = (x1-x2) and yd = (y1-y2) | ||
- | Note that for 6502 we can take the approximation even further | + | Note that for 6502 we will use a shift right to calculate |
< | < | ||
Line 16: | Line 17: | ||
; than (9/8) + one bit uncertainty. | ; than (9/8) + one bit uncertainty. | ||
- | | + | ; input: |
+ | ; uses: A xd,yd | ||
+ | ; output: approximate distance between | ||
+ | ; If the actual distance is 228 or less the result estimate will fit in 8 bits | ||
+ | |||
+ | Dist: | ||
+ | lda x1 | ||
sec | sec | ||
sbc x2 | sbc x2 | ||
sta xd | sta xd | ||
- | bpl .posxdiff | + | bcs posxdiff |
- | lda #00 ; | + | eor #$FF |
- | sec | + | adc #1 |
- | sbc xd | + | posxdiff: |
- | .posxdiff: | + | |
sta xd | sta xd | ||
- | lda y1 ; y1 - y2 | + | lda y1 |
sec | sec | ||
sbc y2 | sbc y2 | ||
- | sta yd | + | bcs posydiff |
- | bpl .posydiff | + | eor #$FF |
- | lda #00 | + | adc #1 |
- | sec | + | posydiff: |
- | sbc yd | + | |
- | .posydiff: | + | |
cmp xd | cmp xd | ||
- | | + | bcs ygreater |
- | | + | lsr |
- | adc xd ; + xdelta | + | clc |
+ | adc xd | ||
+ | rts | ||
+ | ygreater: | ||
+ | lsr xd | ||
+ | clc | ||
+ | adc xd | ||
rts | rts | ||
- | .ygreater: | ||
- | sta yd | ||
- | lda xd | ||
- | | ||
- | cls | ||
- | adc yd ; + ydelta | ||
- | rts | ||
- | |||
</ | </ | ||
base/approximation_to_distance.txt · Last modified: 2020-02-11 01:45 by djmips