User Tools

Site Tools


base:approximation_to_distance

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
base:approximation_to_distance [2020-02-09 14:39] – created djmipsbase: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 by noting the coefficient for max is close to one and the coefficient for min is close enough to 1/2 to use that as a faster calculation that's acceptable for a lot of game use.+Note that for 6502 we will use a shift right to calculate the multiply by 1/2.
  
 <code> <code>
Line 16: Line 17:
 ; than (9/8) + one bit uncertainty. ; than (9/8) + one bit uncertainty.
  
- lda x1          ; x1 x2+; input: x1,y1  x2,y2 
 +uses: A xd,yd 
 +; output: approximate distance between x1,y1 and x2,y2 in A + ninth bit in C  
 +; 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   ; abs() + 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   ; abs() + eor #$FF 
- lda #00 + adc #1 
- sec +posydiff:
- sbc yd +
-.posydiff:+
  cmp xd  cmp xd
- bcs .ygreater + bcs ygreater 
- lsr              ; ydelta / 2 + lsr 
- adc xd           ; + xdelta+ clc 
 + adc xd 
 + rts 
 +ygreater: 
 + lsr xd 
 + clc 
 + adc xd
  rts  rts
  
-.ygreater: 
- sta yd 
- lda xd 
- lsr              ; xdelta / 2 
- cls 
- adc yd           ; + ydelta 
- rts 
-  
 </code> </code>
  
base/approximation_to_distance.1581255597.txt.gz · Last modified: 2020-02-09 14:39 by djmips