base:8bit_atan2_8-bit_angle
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | base:8bit_atan2_8-bit_angle [2015-04-17 04:30] (current) – created - external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== 8-bit atan2 ====== | ||
+ | might be more precise to add a clc adc #$01 after each eor #$ff, you have to modify all the preceding bcs *+4/ bcc *+4 to *+7 to get the branches correct. also you can omit the clc where bcs is used. adding a SEC before all sbc's might increase the accuracy even further. :) | ||
+ | /Oswald | ||
+ | |||
+ | < | ||
+ | ;; Calculate the angle, in a 256-degree circle, between two points. | ||
+ | ;; The trick is to use logarithmic division to get the y/x ratio and | ||
+ | ;; integrate the power function into the atan table. Some branching is | ||
+ | ;; avoided by using a table to adjust for the octants. | ||
+ | ;; In otherwords nothing new or particularily clever but nevertheless | ||
+ | ;; quite useful. | ||
+ | ;; | ||
+ | ;; by Johan Forslöf (doynax) | ||
+ | |||
+ | octant = $fb ;; temporary zeropage variable | ||
+ | |||
+ | atan2 lda x1 | ||
+ | sbc x2 | ||
+ | bcs *+4 | ||
+ | eor #$ff | ||
+ | tax | ||
+ | rol octant | ||
+ | |||
+ | lda y1 | ||
+ | sbc y2 | ||
+ | bcs *+4 | ||
+ | eor #$ff | ||
+ | tay | ||
+ | rol octant | ||
+ | |||
+ | lda log2_tab,x | ||
+ | sbc log2_tab,y | ||
+ | bcc *+4 | ||
+ | eor #$ff | ||
+ | tax | ||
+ | |||
+ | lda octant | ||
+ | rol | ||
+ | and #%111 | ||
+ | tay | ||
+ | |||
+ | lda atan_tab,x | ||
+ | eor octant_adjust, | ||
+ | rts | ||
+ | |||
+ | octant_adjust .byte %00111111 ;; | ||
+ | .byte %00000000 ;; | ||
+ | .byte %11000000 ;; | ||
+ | .byte %11111111 ;; | ||
+ | .byte %01000000 ;; | ||
+ | .byte %01111111 ;; | ||
+ | .byte %10111111 ;; | ||
+ | .byte %10000000 ;; | ||
+ | |||
+ | |||
+ | ;;;;;;;; atan(2^(x/ | ||
+ | |||
+ | atan_tab .byte $00, | ||
+ | .byte $00, | ||
+ | .byte $00, | ||
+ | .byte $00, | ||
+ | .byte $00, | ||
+ | .byte $00, | ||
+ | .byte $00, | ||
+ | .byte $00, | ||
+ | .byte $00, | ||
+ | .byte $00, | ||
+ | .byte $00, | ||
+ | .byte $01, | ||
+ | .byte $01, | ||
+ | .byte $01, | ||
+ | .byte $01, | ||
+ | .byte $02, | ||
+ | .byte $02, | ||
+ | .byte $03, | ||
+ | .byte $03, | ||
+ | .byte $04, | ||
+ | .byte $05, | ||
+ | .byte $06, | ||
+ | .byte $07, | ||
+ | .byte $08, | ||
+ | .byte $09, | ||
+ | .byte $0b, | ||
+ | .byte $0d, | ||
+ | .byte $10, | ||
+ | .byte $12, | ||
+ | .byte $15, | ||
+ | .byte $19, | ||
+ | .byte $1c, | ||
+ | |||
+ | |||
+ | ;;;;;;;; log2(x)*32 ;;;;;;;; | ||
+ | |||
+ | log2_tab .byte $00, | ||
+ | .byte $60, | ||
+ | .byte $80, | ||
+ | .byte $92, | ||
+ | .byte $a0, | ||
+ | .byte $aa, | ||
+ | .byte $b2, | ||
+ | .byte $b9, | ||
+ | .byte $c0, | ||
+ | .byte $c5, | ||
+ | .byte $ca, | ||
+ | .byte $ce, | ||
+ | .byte $d2, | ||
+ | .byte $d6, | ||
+ | .byte $d9, | ||
+ | .byte $dd, | ||
+ | .byte $df, | ||
+ | .byte $e2, | ||
+ | .byte $e5, | ||
+ | .byte $e7, | ||
+ | .byte $ea, | ||
+ | .byte $ec, | ||
+ | .byte $ee, | ||
+ | .byte $f0, | ||
+ | .byte $f2, | ||
+ | .byte $f4, | ||
+ | .byte $f6, | ||
+ | .byte $f8, | ||
+ | .byte $f9, | ||
+ | .byte $fb, | ||
+ | .byte $fd, | ||
+ | .byte $fe, | ||
+ | </ |
base/8bit_atan2_8-bit_angle.txt · Last modified: 2015-04-17 04:30 by 127.0.0.1