Move a point around a circular path
17,001
Solution 1
use sin and cos
for (double t = 0; t < 2*Pi; t += 0.01) {
x = R*cos(t) + x_0;
y = R*sin(t) + y_0;
}
where:
- (x_0, y_0) is the center of the circle
- R is the raduis
Solution 2
Or in angle's instead of radians...
#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;
}
}
Plots a circle around a center point, 1 degree at a time. You can increment ctr by any amount to adjust the step size.
Solution 3
You can use polar coordinates:
X = R * cos (phi) + center_X
Y = R * sin (phi) + center_Y
and change the phi in the loop.
Solution 4
I believe you confused cos() for sin() for the y-axis. Code should be: point_y = radius * sin(ctr * 3.1415926f / 180.0f) + center_y;
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on June 27, 2022Comments
-
Admin about 2 years
I have a point with 2D coordinates. I need to change the points coordinate values in order to follow a circular path.
How would I implement that using C?