angle between two points
To calculate the angle between two points in C, you can use the following formula:
#include <stdio.h>
#include <math.h>
double calculateAngle(double x1, double y1, double x2, double y2) {
double delta_x = x2 - x1;
double delta_y = y2 - y1;
double angle_rad = atan2(delta_y, delta_x);
double angle_deg = angle_rad * (180 / M_PI);
return angle_deg;
}
int main() {
double x1, y1, x2, y2;
printf("Enter the coordinates of point 1 (x1, y1): ");
scanf("%lf %lf", &x1, &y1);
printf("Enter the coordinates of point 2 (x2, y2): ");
scanf("%lf %lf", &x2, &y2);
double angle = calculateAngle(x1, y1, x2, y2);
printf("The angle between the two points is: %lf degrees\n", angle);
return 0;
}
This code prompts the user to enter the coordinates of two points, calculates the angle between them using the atan2()
function, and then prints the result in degrees.