如何计算圆周长的点?

Jus*_*ier 215 algorithm math trigonometry

如何在各种语言中实现以下功能?

(x,y)在给定输入值的情况下计算圆周上的点:

  • 半径
  • 角度
  • Origin(可选参数,如果语言支持)

Pau*_*xon 570

参数方程

x = cx + r * cos(a)
y = cy + r * sin(a)
Run Code Online (Sandbox Code Playgroud)

[R为半径,CX,CY的起源,和一个角度.

这很容易适应任何具有基本触发功能的语言.请注意,大多数语言将使用弧度为三角函数的角度,因此而不是通过循环程度0..360,你骑自行车通过0..2PI弧度.

  • 请注意,`a`必须是弧度 - 这对我来说真的很难理解. (101认同)
  • 我一直试图将这个等式推导出一个小时.谢谢.谁知道你在高中学到的三重身份将会非常有帮助. (12认同)
  • @IsiomaNnodum如果我们都回到这里只是为了记住方程是什么,那就没那么有用了. (10认同)

Jus*_*ier 45

这是我在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);
    }
Run Code Online (Sandbox Code Playgroud)

  • 预先计算转换因子,因此使用硬编码数字输入错误的机会较少. (5认同)

Pet*_*ham 15

当你有复杂的数字时谁需要触发:

#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 ) );
}
Run Code Online (Sandbox Code Playgroud)


Kos*_*asX 9

用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 ];

}
Run Code Online (Sandbox Code Playgroud)

用法:

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

代码笔

/**
 * 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 );
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)