Home > Software design >  How to find points on circle when center, radius, and angle is given in C?
How to find points on circle when center, radius, and angle is given in C?

Time:09-02

I have a triangle that i am trying to rotate(on 2D plain). I calculated it's centroid so, now i have an imaginary circle with center(centroid),points at 0 degree angle (1st vertex of the trangle). My idea is to rotate each vertex by increasing it's angle(on a loop) and finding new coordinates for the vertex at that angle.


#include <stdio.h>
#include <math.h>

//"the function"

int calculate_new_vertex_for_trangle(int center_x, int center_y,int x_at_0,int y_at_0,int angle)
{
    double radius = sqrt(pow(x_at_0-center_x,2) pow(y_at_0-center_y,2));

    float angle_to_radian = ((22.0/7.0)/180)*angle;

    // googled formula 
    float new_x = center_x   radius*cos(angle_to_radian);
    float new_y = center_y   radius*sin(angle_to_radian);

    printf("x : %f, y : %f \nradius : %f\n",  new_x, new_y,radius);

    return 0;
}

Expected Output

But the result I got was :Image

Numbers at the end of the lines are coordinate's position at respective angle i got using "the function".

CodePudding user response:

I think, a good way to rotate a point around another one, is to use the rotation matrix. You can find more details about this on Wikipedia: https://en.wikipedia.org/wiki/Rotation_matrix

I hope this sample code will help you:

#include <stdio.h>
#include <math.h>

int main()
{

    // Center on circle
    float center_x = 0.0;
    float center_y = 0.0;
    
    // Point on circle
    float point_x = 1.0;
    float point_y = 1.0;
    
    // Rotation angle
    float rotation_deg = 180;
    float rotation_rad = rotation_deg * M_PI / 180.0;
    
    // Rotation matrix
    float a = cos(rotation_rad);
    float b = -sin(rotation_rad);
    float c = sin(rotation_rad);
    float d = cos(rotation_rad);
    
    // Matrix multiplication
    float dx = point_x - center_x;
    float dy = point_y - center_y;
    float new_x = a * dx   b * dy;
    float new_y = c * dx   d * dy;
    
    // Add center point
    new_x  = center_x;
    new_y  = center_y;
    
    printf("old_point = %.2f|%.2f\n", point_x, point_y);
    printf("new_point = %.2f|%.2f\n", new_x, new_y);
    
}

Regards.

CodePudding user response:

This formula:

float new_x = center_x   radius*cos(angle_to_radian);
float new_y = center_y   radius*sin(angle_to_radian);

gives the new position of the vertex at angle=0 after a counter-clockwise rotation around (center_x, center_y). But you want the new position for any vertex.

You can calculate the initial angle (counter-clockwise) for a point at (px,py):

float angle_ini = atan2(px-center_x, py-center_y);

And then use it for the rotated position:

float new_x = center_x   radius*cos(angle_to_radian   angle_ini);
float new_y = center_y   radius*sin(angle_to_radian   angle_ini);

There's a second formula wich achieves the same result, called "rotation transform"

float angle_to_radian = (3.14159265358979/180)*angle;
float sinA = sin(angle_to_radian);
float cosA = cos(angle_to_radian);
float new_x = center_x   (px-center_x)*cosA - (py-center_y)*sinA;
float new_y = center_y   (px-center_x)*sinA   (py-center_y)*cosA;

Notice that with this second way you avoid calculating the radius.

  • Related