Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cartesian to polar coordinates

Take a look at the example here: http://www.brianhare.com/physics/so.html

Take a look at console.log where I am using these two main functions:

    function distanceBetween2pts(x1, y1, x2, y2) {
        console.log("Particle: ("+x1+","+y1+") Mouse: ("+x2+","+y2+")");
        //  Pythagoras Theorem
        // PQ = sqrt( (x2-x1)^2 + (y2-y1)^2 )
        var x = (x2-x1);
        var y = (y2-y1);

        this.radius = Math.sqrt(x*x + y*y);
        this.x = x;
        this.y = y;
    }

    function polar2cartesian(R, theta) {
        this.x = R * Math.cos(theta);
        this.y= R * Math.sin(theta);
    }

Where when the mouse is above and to the right of the particle (center circle) such as : enter image description here

The console log displays:

Particle: (300,250) Mouse: (326,223)
artan(-27 / 26) = angle: -46.08092418666069 - theta -0.8042638494191191

where it should be arctan(27/26) = angle : 46 : theta = 0.8. because even thouse the mouse is "above" the center, it's reading the y2-y1 as -27 because the coord system is based about 0,0 being top left.

The issue then is when both X and Y are negative making theta positive, when it should be pointing the opposite direction (outward from the center point). I know I could just do a 180 degree trick here but I want to understand what im doing wrong.

like image 280
ParoX Avatar asked Jan 17 '12 17:01

ParoX


People also ask

How do you convert Cartesian coordinates to polar coordinates?

Summary: to convert from Polar Coordinates (r,θ) to Cartesian Coordinates (x,y) : x = r × cos( θ ) y = r × sin( θ )

How do you convert polar coordinates to Cartesian coordinates in C++?

The values are calculated by substituting values for 'r' and 'θ'. Code : class cartesian { float x,y; public: cartesian(){} cartesian(polar p) { x = p.r * cos(p.th); y = p.r * sin(p.th); } void show() { cout<<"\n\nIn Cartesian form : \nx="<<x<<" and y="<<y; } };

How do you convert an equation to a polar coordinate?

To convert from rectangular coordinates to polar coordinates, use one or more of the formulas: cosθ=xr, sinθ=yr, tanθ=yx, and r=√x2+y2.

How are Cartesian and polar coordinates related?

While in cartesian coordinates x, y (and z in three-dimensions) can take values from −∞ to ∞, in polar coordinates r is a positive value (consistent with a distance), and θ can take values in the range [0,2π].


2 Answers

In Javascript and many languages there is an atan2 function to tackle this problem. It is a function which takes 2 arguments (the x and y coordinates), so the system can add or subtract π to the result to produce the correct angle. Instead of calling

Math.atan(y/x)

you just call instead

Math.atan2(y, x)
like image 164
kennytm Avatar answered Sep 30 '22 12:09

kennytm


Don't know how do you want to draw the line, but either of these will solve your problem.

 theta = Math.atan2(distance.y , distance.x);
 theta = Math.PI + Math.atan2(distance.y , distance.x);
like image 25
Diode Avatar answered Sep 30 '22 11:09

Diode