atan2のLua実装
atan2 - 永字八法の続き。
たろう 2010/03/28 08:01 えっと、これ、僕が作っているゲームで使ってもいいんですよね?
atan2 - 永字八法の続きでもあります。
法的な問題は全くないんだけれども、そのページのスクリプトはそもそもバグがあるし、現在ではもっといい方法があるので。
nslua.dllを使ってください。
system.luaに書き加える内容
NSExec("_luasub atan2") function NSCOM_atan2() local res1 = NSPopIntRef() -- atan2 NSPopComma() local res2 = NSPopIntRef() -- distance NSPopComma() local x = NSPopInt() NSPopComma() local y = NSPopInt() if x==0 and y==0 then NSSetIntValue(res1, 0) -- (x, y)が原点Oだった場合、0を返す。 NSSetIntValue(res2, 0) -- (x, y)と原点Oの距離。 else NSSetIntValue(res1, math.floor(math.deg(math.atan2(y, x)))) -- そうでなければ、計算する。 NSSetIntValue(res2, math.floor(math.sqrt(x*x+y*y))) -- (x, y)と原点Oの距離。 end end
使い方
atan2 %(結果を受け取る変数1),%(結果を受け取る変数2),x,y
x座標とy座標を受け取って、原点Oに対する角度(結果1)と距離(結果2)を返します。
atan2を使うだけなら、これだけで充分です。いやもうほんと。