# Observations on cartesianToPolar

#1

As I understand polar coordinates the angle is always positive, ie you turn round to the left (from facing “East”) and walk the distance “radius”. This means that for a point in the -x/+y sector, for example, the angle is greater than pi/2 radians (ie greater than 90 deg). This is not what the function returns so it is strictly only correct for +x/+y coordinates - in particular, the theta calculation for y < 0 is not valid.

Is this not the case?

A further point: I spent some time wondering what M_PI_2 represented. Did I miss something earlier in the text? because constants don’t seem to appear before p145.

Hope I’m not being awkward, here.

#2

If you command-click on M_PI_2 it will jump to where it is defined in the source headers and then you see:

It’s fun to click around on everything and see where it came from!

#3

Yes, thank you. I eventually found that, but not without reference to the index. I did guess what was happening but being a total novice I expected everything to be fairly explicit in the chapter itself.

#4

Not defending the author, he did ask you to read the Math.h help in the terminal - You would have found the M_PI_2 at the end of the file.

I think the whole example was kinda confusing and I hope the author will change it in the coming editions - I like Math but don’t love it, so I don’t want to think about formulas while trying to understand the code and the concept.

#5

Thanks for mentioning M_PI_2, it kinda threw me off there as well. I was guessing from it’s name (and from what is actually happening at that part in the program) that it meant pi/2, but I had no clue where it came from. True, it was mentioned to read through math.h in the man pages, however that turned out to be a bit tedious and I put it off for now and never got to the mention of M_PI_2.
Actually using 3.14159265/2 worked just as well though

#6

To address this comment, if theta is positive it means if we are looking at a set of x-y coordinates and we start from the positive x axis then we will move counter-clockwise radians. However, a negative angle in reference to this situation means we start at the positive x axis and move clock-wise radians.

Heres an Example.
pi/2 rad = 90 degrees this is the same angle as say -3pi/2 rad = -270 degrees. They both will make us land on the positive y axis.

So these two statements should be equivalent if we are strictly using mathematical context.

#7

Mathematically, the angle in polar coordinates can assume any value, positive or negative. Is a good practice to define the angle in the interval [0, 2 * PI). Some math books define in the interval [-PI, PI).
The code in the book uses atan(y / x) to calculate the angle. But, the atan function is defined as follow:
y = atan(x) <=> tan(y) = x and -PI/2 < y < PI/2.
Note that the possible results of the atan function is in the first or fourth quadrants. Then, without correction, it only is appropriate to calculate coordinates with the positive values of x on cartesian system.

Follow the code about coordinates in the book (with corrections described in two notes bellow the code):

[code]#include <stdio.h>
#include <math.h>

void cartesianToPolar(double x, double y, double *rPtr, double *thetaPtr)
{
*rPtr = sqrt(x * x + y * y);

``````// Calculate theta
double theta;
if (x == 0.0) {
if (y == 0.0) {
theta = 0.0;	// technically considered undefined.
} else if (y > 0) {
theta = M_PI_2;
} else {
theta = -M_PI_2;
}
} else {
theta = atan(y / x);
}
// Store theta in the supplied address
*thetaPtr = theta;
``````

}

int main(int argc, const char * argv[])
{
double x = 3.0;
double y = 4.0;
double angle;

``````cartesianToPolar(x, y, &radius, &angle);

return 0;
``````

}
[/code]

Note 1: For consistence, I have modified the types of the first two parameters (x, and y) from float to double. The type of the temporary local variable theta was modified too from float to double.
Note 2: In the book the printf call to show the result put the radian unit in radius information, instead of in the angle. I have corrected this mistake in the code above.

Executing the code sample in the book, we get the following correct answer, once x is positive (3.0):

But, any coordinates with negative x values will be miscalculated.

For example, if x = -10.0, and y = 10.0, we get the following output:

Is easy to observe that the conversion is incorrect. The radius is correct (14.14 that is, approximately, equal to 10*sqrt(2)), but the angle is incorrect (0.79 that is, approximately, equal to PI/4). Note that while the cartesian coordinates (-10.0, -10.0) is in third quadrant, the polar coordinates (14.14, 0.79) is in the first quadrant. The correct angle is 5 * PI /4 (approximately 3.93).

Follow, a solution using the acos function instead of atan, once the function is defined as follow:
y = acos(x) <=> cos(y) = x, and 0 <= y <= PI.

Follow, my suggestion of modification in book code:

[code]#include <stdio.h>
#include <math.h>

void cartesianToPolar(double x, double y, double *rPtr, double *thetaPtr) {
*rPtr = sqrt(x * x + y * y);

``````// Calculate theta
if (*rPtr == 0.0)
*thetaPtr = 0.0;	// technically considered undefined.
else {
*thetaPtr = acos(x / *rPtr);
if (y < 0)
*thetaPtr = 2 * M_PI - *thetaPtr;
}
``````

}

int main(int argc, const char * argv[])
{
double x = -10.0;
double y = -10.0;
``````cartesianToPolar(x, y, &radius, &angle);