0

X = 0、Y = 0 の値があり、ベクトルと大きさから新しい点 (X1, Y1) を計算したいとします。ベクトルが 90 度 (ラジアンではない) で、大きさが 10 であるとすると、コードは次のようになります。

x1 = X + (10 * Math.Cos(90 * (Math.PI / 180.0)));
y1 = Y + (10 * Math.Sin(90 * (Math.PI / 180.0)));

そして、結果を確認するために線を引きます

DrawLine(X,Y,x1,y1);

しかし、私のラインは90度ずれすぎています!Cos と Sin に渡す角度から 90 を引くと、すべてうまくいきます。だから、私はそれと一緒に暮らすことができると思います。

スクリーンショットは次のとおりです。 ここに画像の説明を入力

私はそれを 90 度通過しており、線が西から東に向かうことを期待しています。真北を 0 度と仮定します。私が使用している座標系は、X が水平、Y が垂直であることを知っています。

私は何が欠けているのか知りたいだけです。

4

2 に答える 2

2

Unexpected integer truncation

The conversion from degrees to radians resulted in a value near pi/2, but not exactly. (Hard to do as pi is transcendental.) Math.Cos(near_pi/2) resulted in a value near 0.0, but not _exactly 0.0. Let's assume it was negative like -6.1e-17.

Assuming 'X' was a floating point number with an integer value, the result of X + small_negative_number was a number just less than the integer value. Assigning that result to x1 and giving it to the plot() routine, which certainly uses integer values caused a integer truncation down to the next lower integer.

Thus the expected straight line is 1 pixel off.

The general solution is to present plot(x,y) with integer values that are derived from rounded floating point values.

x_int = round(X_floating_point);
于 2013-10-24T23:00:08.627 に答える
0

x と y に問題はありません。

You use COS for x.  COS(0) = 1    ,  COS(90) = 0
You use SIN for y.  SIN(0) = 0    ,  SIN(90) = 1

画面座標系の場合:

x+ is left to right
y+ is top to bottom

そのため、ラインは上から下に移動します。

ご覧のとおり、線はきれいでまっすぐではありません。Math.PI の宣言に従うと、次のように表示されpublic const double PI = 3.14159 ます。それはあまり正確ではありません!PI を次のものに置き換えてみてください。3.14159265358979323846

作ります:

public const double BETTER_PI = 3,14159265358979323846

x1 = X + (10 * Math.Cos(90 * (BETTER_PI / 180.0)));
y1 = Y + (10 * Math.Sin(90 * (BETTER_PI / 180.0)));

そう:

x1 = X + (10 * Math.Cos(1.57079632679)) = X + (10 * 0) = X + 0;
y1 = Y + (10 * Math.Sin(1.57079632679)) = Y + (10 * 1) = Y + 10;
于 2013-09-19T23:16:39.747 に答える