fastest sqrt version, fix percentage
This commit is contained in:
parent
3ec786f416
commit
d9a10a7d07
26
magnitest.p8
26
magnitest.p8
@ -22,6 +22,16 @@ function bbs28999algopt(dx, dy)
|
||||
return (n*n + 1)^0.5 * d
|
||||
end
|
||||
|
||||
-- profiler says 54 cycles
|
||||
-- 26 lua, 28 system
|
||||
function bbs28999algopt2(dx, dy)
|
||||
local d,n=abs(dx),abs(dy)
|
||||
if (d<n) d,n=n,d
|
||||
n/=d
|
||||
return sqrt(n*n + 1) * d
|
||||
end
|
||||
|
||||
|
||||
-- profiler says 18 cycles
|
||||
-- 18 lua, 0 system
|
||||
function trigalg(dx, dy)
|
||||
@ -37,6 +47,7 @@ function _init()
|
||||
tresult=0
|
||||
bresult=0
|
||||
delta=0
|
||||
dp=0
|
||||
end
|
||||
|
||||
function _update60()
|
||||
@ -45,25 +56,30 @@ function _update60()
|
||||
if (btn(2)) gdy -= acc
|
||||
if (btn(3)) gdy += acc
|
||||
if (btn(4) and btn(5)) gdx,gdy=0,0
|
||||
gdx=mid(-16000,16000,gdx)
|
||||
gdy=mid(-16000,16000,gdy)
|
||||
|
||||
if btn() == 0 or btn(4) then
|
||||
acc = 1
|
||||
elseif btn(5) then
|
||||
acc *= 1.1
|
||||
acc *= 1.05
|
||||
else
|
||||
acc *= 1.02
|
||||
end
|
||||
if (acc > 1024) acc=1024
|
||||
|
||||
local a,b=abs(gdx),abs(gdy)
|
||||
if (b>a) a,b=b,a
|
||||
magnitude = 1
|
||||
while magnitude*64 < a do
|
||||
while magnitude < a/64 do
|
||||
magnitude *= 2
|
||||
end
|
||||
|
||||
bresult=bbs28999algopt(gdx,gdy)
|
||||
bresult=bbs28999algopt2(gdx,gdy)
|
||||
tresult=trigalg(gdx,gdy)
|
||||
delta=tresult-bresult
|
||||
dp=(2*abs(delta)/abs(abs(bresult)+abs(tresult)))*100
|
||||
if (delta == 0) dp=0
|
||||
end
|
||||
|
||||
cols = {
|
||||
@ -81,11 +97,13 @@ cols = {
|
||||
|
||||
function _draw()
|
||||
cls()
|
||||
camera()
|
||||
print("x: "..tostr(gdx).." y: "..tostr(gdy).."\nbase: "..tostr(bresult).."\ntrig: "..tostr(tresult).."\n\ndiff: "..tostr(delta).."\n %:"..tostr(dp),0,0,13)
|
||||
print("⬆️⬇️⬅️➡️:move ❎:fast 🅾️:slow\n❎+🅾️:reset position",0,117,12)
|
||||
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,13)
|
||||
end
|
||||
|
||||
__gfx__
|
||||
|
@ -352,6 +352,11 @@ end,function(dx, dy)
|
||||
if (d<n) d,n=n,d
|
||||
n/=d
|
||||
return (n*n + 1)^0.5 * d
|
||||
end,function(dx,dy)
|
||||
local d,n=abs(dx),abs(dy)
|
||||
if (d<n) d,n=n,d
|
||||
n/=d
|
||||
return sqrt(n*n + 1) * d
|
||||
end,function(dx,dy)
|
||||
local s = sin(atan2(dx,dy))
|
||||
if (s==0) return abs(dx)
|
||||
|
Loading…
x
Reference in New Issue
Block a user