次の関数をさまざまな言語でどのように実装できますか?
(x,y)
次の入力値を指定して、円の円周上の点を計算します。
- 半径
- 角度
- Origin(言語でサポートされている場合はオプションのパラメーター)
次の関数をさまざまな言語でどのように実装できますか?
(x,y)
次の入力値を指定して、円の円周上の点を計算します。
円のパラメトリック方程式は
x = cx + r * cos(a)
y = cy + r * sin(a)
ここで、 rは半径、cx,cyは原点、aは角度です。
これは、基本的な三角関数を使用して任意の言語に適応するのは非常に簡単です。ほとんどの言語は三角関数の角度にラジアンを使用するため、0..360 度を循環するのではなく、0..2PI ラジアンを循環していることに注意してください。
これが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);
}
複素数がある場合に三角関数が必要なのは誰ですか:
#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 ) );
}
int x = (int)(radius * Math.Cos(degree * Math.PI / 180F)) + cCenterX;
int y = (int)(radius * Math.Sin(degree * Math.PI / 180F)) + cCenterY;
cCenterX
とcCenterY
は円の中心点です