testing vector magnitude calculation
This commit is contained in:
96
magnitest.p8
Normal file
96
magnitest.p8
Normal file
@ -0,0 +1,96 @@
|
||||
pico-8 cartridge // http://www.pico-8.com
|
||||
version 42
|
||||
__lua__
|
||||
--magnitest.p8
|
||||
--accuracy and perf tests for
|
||||
--vector calculation logic
|
||||
|
||||
-- profiler says 67 cycles
|
||||
-- 39 lua, 28 system
|
||||
function bbs28999alg(dx, dy)
|
||||
local d = max(abs(dx),abs(dy))
|
||||
local n = min(abs(dx),abs(dy)) / d
|
||||
return sqrt(n*n + 1) * d
|
||||
end
|
||||
|
||||
-- profiler says 56 cycles
|
||||
-- 24 lua, 32 system
|
||||
function bbs28999algopt(dx, dy)
|
||||
local d,n=abs(dx),abs(dy)
|
||||
if (d<n) d,n=n,d
|
||||
n/=d
|
||||
return (n*n + 1)^0.5 * d
|
||||
end
|
||||
|
||||
-- profiler says 18 cycles
|
||||
-- 18 lua, 0 system
|
||||
function trigalg(dx, dy)
|
||||
local s = sin(atan2(dx,dy))
|
||||
if (s==0) return abs(dx)
|
||||
return abs(dy/s)
|
||||
end
|
||||
-->8
|
||||
function _init()
|
||||
gdx,gdy=0,0
|
||||
acc=1
|
||||
magnitude = 1
|
||||
tresult=0
|
||||
bresult=0
|
||||
delta=0
|
||||
end
|
||||
|
||||
function _update60()
|
||||
if (btn(0)) gdx -= acc
|
||||
if (btn(1)) gdx += acc
|
||||
if (btn(2)) gdy -= acc
|
||||
if (btn(3)) gdy += acc
|
||||
if (btn(4) and btn(5)) gdx,gdy=0,0
|
||||
|
||||
if btn == 0 or btn(4) then
|
||||
acc = 1
|
||||
elseif btn(5) then
|
||||
acc *= 1.1
|
||||
else
|
||||
acc *= 1.02
|
||||
end
|
||||
|
||||
local a,b=abs(gdx),abs(gdy)
|
||||
if (b>a) a,b=b,a
|
||||
magnitude = 1
|
||||
while magnitude < a do
|
||||
magnitude *= 2
|
||||
end
|
||||
|
||||
bresult=bbs28999algopt(gdx,gdy)
|
||||
tresult=trigalg(gdx,gdy)
|
||||
delta=tresult-bresult
|
||||
end
|
||||
|
||||
cols = {
|
||||
6,7,
|
||||
[4]=10,
|
||||
[8]=9,
|
||||
[16]=4,
|
||||
[32]=14,
|
||||
[64]=15,
|
||||
[128]=11,
|
||||
[256]=12,
|
||||
[512]=13,
|
||||
[1024]=8,
|
||||
}
|
||||
|
||||
function _draw()
|
||||
camera(-63,-63)
|
||||
line(0,0,gdx/magnitude,gdy/magnitude,cols[magnitude] or 2)
|
||||
rectfill(0,0,gdx/magnitude,0,5)
|
||||
rectfill(0,0,0,gdy/magnitude,5)
|
||||
print("x: "..tostr(gdx).." y: "..tostr(gdy).."\nbase: "..tostr(bresult).."\ntrig: "..tostr(tresult).."\n\ndiff: "..tostr(delta),-63,-63)
|
||||
end
|
||||
|
||||
__gfx__
|
||||
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
00077000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||||
00700700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
Reference in New Issue
Block a user