TheAlgorithms-C/math/cartesian_to_polar.c

124 lines
2.6 KiB
C
Raw Normal View History

2020-07-11 05:40:46 +03:00
/**
* @file
* @brief Function to convert a Cartesian co-ordinate to polar form.
*/
#define _USE_MATH_DEFINES /**< required for MS Visual C */
2020-07-11 06:55:04 +03:00
#include <assert.h>
#include <math.h>
#include <stdio.h>
2020-07-11 06:55:04 +03:00
#include <stdlib.h>
/**
2020-07-11 06:55:04 +03:00
* @brief Function to convert cartesian coordinates to polar.
*\f{eqnarray*}{
r &=& \sqrt{x^2+y^2}\\
\theta &=& \atan\frac{y}{x}
\f}
* @param [in] x absicca value
* @param [in] y ordinate value
* @param [out] r pointer to store polar radius
* @param [out] theta pointer to store polar angle (in radian)
*/
void to_polar(double x, double y, double *r, double *theta)
{
2020-07-11 06:59:51 +03:00
double thetaFinal = 0.f;
2020-07-11 06:55:04 +03:00
*r = sqrt(x * x + y * y);
if (x != 0)
{
if (y != 0)
{
2020-07-11 06:55:04 +03:00
*theta = atan(y / x);
if ((x > 0 && y > 0) || (x == -y))
{ // Q1
2020-07-11 06:55:04 +03:00
thetaFinal = *theta;
}
else if (x < 0 && y > 0)
{ // Q2
2020-07-11 06:55:04 +03:00
thetaFinal = *theta + M_PI;
}
else if (x < 0 && y < 0)
{ // Q3
2020-07-11 06:55:04 +03:00
thetaFinal = *theta - M_PI;
}
else if (x > 0 && y < 0)
{ // Q4
2020-07-11 06:55:04 +03:00
thetaFinal = 2 * M_PI - *theta;
}
2020-07-11 06:59:51 +03:00
else
{
fprintf(stderr, "Should not reach here!\n");
}
}
}
2020-07-11 06:59:51 +03:00
else
{ // exceptions when no actual angle is present
if (y > 0)
{
2020-07-11 05:40:46 +03:00
thetaFinal = M_PI / 2;
}
else
{
2020-07-11 05:40:46 +03:00
thetaFinal = -(M_PI / 2);
}
}
if (y == 0)
{
if (x > 0)
{
thetaFinal = 0;
}
else
{
2020-07-11 05:40:46 +03:00
thetaFinal = -M_PI;
}
}
2020-07-11 06:55:04 +03:00
*theta = thetaFinal;
}
/**
* @brief Generate a random number in the given limits
*
* @param lim1 lower limit
* @param lim2 upper limit
* @return random number in the given range
*/
double get_rand(double lim1, double lim2)
{
double r = (double)rand() / RAND_MAX; // value in [0,1)
return (lim2 - lim1) * r + lim1; // scale to range
}
/**
* @brief Test implementation
*
*/
void test()
{
srand(10);
int NUM_TESTS = 5;
for (int i = 0; i < NUM_TESTS; i++)
{
double r, theta;
printf("Test %d.... ", i);
double x = get_rand(-5, 5);
double y = get_rand(-5, 5);
printf("(%.2g, %.2g).... ", x, y);
to_polar(x, y, &r, &theta);
assert(fabs(r - hypot(x, y)) < 0.01);
assert(fabs(theta - atan2(y, x)) < 0.01);
printf("passed\n");
}
}
/** Main function */
int main()
{
test();
return 0;
}