Hardgraf
Hardgraf

Reputation: 2616

Simple Cartesian to Polar Coordinates method

Trying to convert some Cartesian to Polar coordinates. Not a mathematician. Start and End values are degrees. Does this make sense?

Point outerScreenPointBefore = CartesianToPolar(Start, End);  

The converter method:

 private Point CartesianToPolar(double x, double y)
    {
        x = Math.Sqrt((x*x) + (y + y));
        y = Math.Atan2(y, x);

        return new Point(x, y);
    }

Upvotes: 0

Views: 7456

Answers (1)

Dennis_E
Dennis_E

Reputation: 8894

Not exactly. When you do

y = Math.Atan2(y, x);

, x has already been given a new value in the previous line:

x = Math.Sqrt((x*x) + (y*y));

So, you need to calculate both values before you assign them:

double radius = Math.Sqrt((x*x) + (y*y));
double angle = Math.Atan2(y, x);
return new Point(radius, angle);

Polar coordinates don't use the notation x and y, but r and θ, so the meaning may not be clear to someone who reads your code.
You might want to create a new struct with properties Radius and Angle.

Upvotes: 7

Related Questions