次の機能をさまざまな言語で実装するにはどうすればよいですか?
(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 ) );
}
JavaScript(ES6)で実装:
/**
    * Calculate x and y in circle's circumference
    * @param {Object} input - The input parameters
    * @param {number} input.radius - The circle's radius
    * @param {number} input.angle - The angle in degrees
    * @param {number} input.cx - The circle's origin x
    * @param {number} input.cy - The circle's origin y
    * @returns {Array[number,number]} The calculated x and y
*/
function pointsOnCircle({ radius, angle, cx, cy }){
    angle = angle * ( Math.PI / 180 ); // Convert from Degrees to Radians
    const x = cx + radius * Math.cos(angle);
    const y = cy + radius * Math.sin(angle);
    return [ x, y ];
}使用法:
const [ x, y ] = pointsOnCircle({ radius: 100, angle: 180, cx: 150, cy: 150 });
console.log( x, y );
aラジアンである必要があることに注意してください。初心者として理解するのは非常に困難でした。