240

次の関数をさまざまな言語でどのように実装できますか?

(x,y)次の入力値を指定して、円の円周上の点を計算します。

  • 半径
  • 角度
  • Origin(言語でサポートされている場合はオプションのパラメーター)
4

6 に答える 6

630

円のパラメトリック方程式

x = cx + r * cos(a)
y = cy + r * sin(a)

ここで、 rは半径、cx,cyは原点、aは角度です。

これは、基本的な三角関数を使用して任意の言語に適応するのは非常に簡単です。ほとんどの言語は三角関数の角度にラジアンを使用するため、0..360 度を循環するのではなく、0..2PI ラジアンを循環していることに注意してください。

于 2009-05-08T14:03:36.540 に答える
53

これがC#での私の実装です:

    public static PointF PointOnCircle(float radius, float angleInDegrees, PointF origin)
    {
        // Convert from degrees to radians via multiplication by PI/180        
        float x = (float)(radius * Math.Cos(angleInDegrees * Math.PI / 180F)) + origin.X;
        float y = (float)(radius * Math.Sin(angleInDegrees * Math.PI / 180F)) + origin.Y;

        return new PointF(x, y);
    }
于 2009-05-08T13:57:53.837 に答える
17

複素数がある場合に三角関数が必要なのは誰ですか:

#include <complex.h>
#include <math.h>

#define PI      3.14159265358979323846

typedef complex double Point;

Point point_on_circle ( double radius, double angle_in_degrees, Point centre )
{
    return centre + radius * cexp ( PI * I * ( angle_in_degrees  / 180.0 ) );
}
于 2009-05-08T14:15:40.270 に答える
0
int x = (int)(radius * Math.Cos(degree * Math.PI / 180F)) + cCenterX;
int y = (int)(radius * Math.Sin(degree * Math.PI / 180F)) + cCenterY;

cCenterXcCenterYは円の中心点です

于 2022-02-04T13:08:46.227 に答える