mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
better structure, format and docs
This commit is contained in:
parent
cfca2aba51
commit
d186f59144
@ -3,39 +3,48 @@
|
||||
* @brief Function to convert a Cartesian co-ordinate to polar form.
|
||||
*/
|
||||
#define _USE_MATH_DEFINES /**< required for MS Visual C */
|
||||
#include <assert.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
give as arguments to the executable two x and y coordinates
|
||||
outputs a polar coordinate
|
||||
*/
|
||||
int main()
|
||||
* @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)
|
||||
{
|
||||
double x, y;
|
||||
double r, theta, thetaFinal;
|
||||
scanf("%lf %lf", &x, &y);
|
||||
r = hypot(x, y);
|
||||
double thetaFinal;
|
||||
|
||||
*r = sqrt(x * x + y * y);
|
||||
|
||||
if (x != 0)
|
||||
{
|
||||
if (y != 0)
|
||||
{
|
||||
theta = atan(y / x);
|
||||
*theta = atan(y / x);
|
||||
if ((x > 0 && y > 0) || (x == -y))
|
||||
{ // Q1
|
||||
thetaFinal = theta;
|
||||
thetaFinal = *theta;
|
||||
}
|
||||
else if (x < 0 && y > 0)
|
||||
{ // Q2
|
||||
thetaFinal = theta + M_PI;
|
||||
thetaFinal = *theta + M_PI;
|
||||
}
|
||||
else if (x < 0 && y < 0)
|
||||
{ // Q3
|
||||
thetaFinal = theta - M_PI;
|
||||
thetaFinal = *theta - M_PI;
|
||||
}
|
||||
else if (x > 0 && y < 0)
|
||||
{ // Q4
|
||||
thetaFinal = 2 * M_PI - theta;
|
||||
thetaFinal = 2 * M_PI - *theta;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -61,5 +70,50 @@ int main()
|
||||
thetaFinal = -M_PI;
|
||||
}
|
||||
}
|
||||
printf("%.2f %.2f\n", r, atan2(y, x));
|
||||
|
||||
*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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user