如何在各种语言中实现以下函数?

计算圆周长上的(x,y)点,给定输入值:

半径 角 起源(可选参数,如果语言支持)


当前回答

我在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);
}

其他回答

在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.sin(angle);
    const y = cy + radius * Math.cos(angle);
    return [ x, y ];

}

用法:

const [ x, y ] = pointsOnCircle({ radius: 100, angle: 180, cx: 150, cy: 150 });
console.log( x, y );

Codepen

/** * 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.sin(angle); const y = cy + radius * Math.cos(angle); return [ x, y ]; } const canvas = document.querySelector("canvas"); const ctx = canvas.getContext("2d"); function draw( x, y ){ ctx.clearRect( 0, 0, canvas.width, canvas.height ); ctx.beginPath(); ctx.strokeStyle = "orange"; ctx.arc( 100, 100, 80, 0, 2 * Math.PI); ctx.lineWidth = 3; ctx.stroke(); ctx.closePath(); ctx.beginPath(); ctx.fillStyle = "indigo"; ctx.arc( x, y, 6, 0, 2 * Math.PI); ctx.fill(); ctx.closePath(); } let angle = 0; // In degrees setInterval(function(){ const [ x, y ] = pointsOnCircle({ radius: 80, angle: angle++, cx: 100, cy: 100 }); console.log( x, y ); draw( x, y ); document.querySelector("#degrees").innerHTML = angle + "&deg;"; document.querySelector("#points").textContent = x.toFixed() + "," + y.toFixed(); }, 100 ); <p>Degrees: <span id="degrees">0</span></p> <p>Points on Circle (x,y): <span id="points">0,0</span></p> <canvas width="200" height="200" style="border: 1px solid"></canvas>

圆的参数方程是

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

r是半径,cx cy是原点,a是角度。

这很容易适应任何带有基本三角函数的语言。请注意,大多数语言会在三角函数中使用弧度作为角度,因此,而不是在0..360度,你在0。2π弧度。

我在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是圆的中心点