Flash Lite 1.1 で Math.atan2()

基本的に1.1でMathクラスはサポート外なのだけど、無理矢理使えるという事も知っていた。

不安なので今までなるべく使わない様にしていたけど、どうしてもMath.atan2()を使いたい案件が。

挙動を見てみると、どうもおかしい。

左側にある座標への角度を求めようとすると180度足さないといけないという事はトライ&エラーで分かった。

せっかくなのでちゃんと調べてみる。

Flash lite 1.1 で Math.atan2() に変数を使うと変な値が返ってくる – 羊を運ぶ、私と兄

FLASH-JP.COM – フォーラム

やっぱり。

ダイレクトはOKで、変数を入れるとおかしくなるってどういう事よ?w

さらに検証。以下のコードで出力を調べてみる。

[as]
stop();

trace("++++++++++++++++++++");
trace("++++++++++++++++++++ 0 (x=1, y=0)");

math_x = 1;
math_y = 0;

trace(Math.atan2(math_y, math_x) * 180 / Math.PI);
trace(Math.atan2(0, 1) * 180 / Math.PI);

trace("++++++++++++++++++++ 45 (x=1, y=1)");

math_x = 1;
math_y = 1;

trace(Math.atan2(math_y, math_x) * 180 / Math.PI);
trace(Math.atan2(1, 1) * 180 / Math.PI);

trace("++++++++++++++++++++ 90 (x=0, y=1)");

math_x = 0;
math_y = 1;

trace(Math.atan2(math_y, math_x) * 180 / Math.PI);
trace(Math.atan2(1, 0) * 180 / Math.PI);

trace("++++++++++++++++++++ 135 (x=-1, y=1)");

math_x = -1;
math_y = 1;

trace(Math.atan2(math_y, math_x) * 180 / Math.PI);
trace(Math.atan2(1, -1) * 180 / Math.PI);

trace("++++++++++++++++++++ 180 (x=-1, y=0)");

math_x = -1;
math_y = 0;

trace(Math.atan2(math_y, math_x) * 180 / Math.PI);
trace(Math.atan2(0, -1) * 180 / Math.PI);

trace("++++++++++++++++++++ -135 (x=-1, y=-1)");

math_x = -1;
math_y = -1;

trace(Math.atan2(math_y, math_x) * 180 / Math.PI);
trace(Math.atan2(-1, -1) * 180 / Math.PI);

trace("++++++++++++++++++++ -90 (x=0, y=-1)");

math_x = 0;
math_y = -1;

trace(Math.atan2(math_y, math_x) * 180 / Math.PI);
trace(Math.atan2(-1, 0) * 180 / Math.PI);

trace("++++++++++++++++++++ -45 (x=1, y=-1)");

math_x = 1;
math_y = -1;

trace(Math.atan2(math_y, math_x) * 180 / Math.PI);
trace(Math.atan2(-1, 1) * 180 / Math.PI);

[/as]

出力される結果は

Lite 1.1 でパブリッシュ
++++++++++++++++++++ 0 (x=1, y=0)
0
0
++++++++++++++++++++ 45 (x=1, y=1)
45
45
++++++++++++++++++++ 90 (x=0, y=1)
0
90
++++++++++++++++++++ 135 (x=-1, y=1)
-45
135
++++++++++++++++++++ 180 (x=-1, y=0)
0
180
++++++++++++++++++++ -135 (x=-1, y=-1)
45
-135
++++++++++++++++++++ -90 (x=0, y=-1)
0
-90
++++++++++++++++++++ -45 (x=1, y=-1)
-45
-45

90〜270度がおかしいのね。

Lite 2.0 でパブリッシュ
++++++++++++++++++++ 0 (x=1, y=0)
0
0
++++++++++++++++++++ 45 (x=1, y=1)
45
45
++++++++++++++++++++ 90 (x=0, y=1)
90
90
++++++++++++++++++++ 135 (x=-1, y=1)
135
135
++++++++++++++++++++ 180 (x=-1, y=0)
180
180
++++++++++++++++++++ -135 (x=-1, y=-1)
-135
-135
++++++++++++++++++++ -90 (x=0, y=-1)
-90
-90
++++++++++++++++++++ -45 (x=1, y=-1)
-45
-45

こちらは問題なし。

という事で、call用のフレームを作って対応する事に。

[as]
deg = Math.atan2(math_y, math_x) * 180 / Math.PI;

if(math_x == 0 && math_y > 0) deg = 90;
if(math_x == 0 && math_y < 0) deg = -90;
if(math_x < 0) deg += 180;

//ついでに丸める
deg = (deg + 360) % 360;
[/as]

こんな感じでいいのかなぁ。


About this entry