围绕圆形路径移动一个点

Bob*_*lle 3 c math

我有一个2D坐标点.我需要更改点坐标值以遵循循环路径.

我如何使用C实现它?

Me *_*ain 13

使用罪和cos

for (double t = 0; t < 2*Pi; t += 0.01) {
    x = R*cos(t) + x_0;
    y = R*sin(t) + y_0;
}
Run Code Online (Sandbox Code Playgroud)

哪里:

  • (x_0,y_0)是圆的中心
  • R是raduis


小智 7

或者以角度而不是弧度......

#include <math.h>

void Circle(float center_x, float center_y, float radius)
{
    float point_x, point_y;
    int ctr;
    for (ctr = 0; ctr < 360; ctr += 1)
    {
        point_x = radius * cos(ctr * 3.1415926f / 180.0f) + center_x;
        point_y = radius * cos(ctr * 3.1415926f / 180.0f) + center_y;
    }
}
Run Code Online (Sandbox Code Playgroud)

围绕中心点绘制一个圆,每次1度.您可以按任意数量递增ctr来调整步长.


TMS*_*TMS 6

你可以使用极坐标:

X = R * cos (phi) + center_X
Y = R * sin (phi) + center_Y
Run Code Online (Sandbox Code Playgroud)

并改变循环中的phi.