Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw square with polar coordinates

I have a square, where the closest points are 1 unit away from the center. See my ascii diagram below:

+-----------+
|           |
|  x        |
|-----+     |
|           |
|           |
+-----------+

Therefore, the distance from the origin to the corners is the sqrt(2). I need a function that returns the distance from the origin to a point on the square at any angle. For example, for an input of 0, the function would return 1. For an input of 45, the function would return the distance to a corner, the square root of 2. Then for 90, it would return 1 again.

In other words, when you graph the function with polar graphing, it will draw a square.

I believe that the function would be something like this:

f(x) = sqrt(tan(x)^2+1)

The only problem is that the function above will not graph the sides of the square. I need something that draws all 4 sides.

I know that there is a trigonometric function for something similar to this, but I will be using this function in javascript, so I will only be able to use the standard trigonometry functions.

Any help will be appreciated. Thanks in advance.

like image 584
Joel Avatar asked Jan 25 '11 00:01

Joel


People also ask

How do you draw a rectangle in polar coordinates?

To convert from polar coordinates to rectangular coordinates, use the formulas x=rcosθ and y=rsinθ.

What is the polar form of √ 3?

The inverse tangent of √33 is θ=30° θ = 30 ° . This is the result of the conversion to polar coordinates in (r,θ) form.

What is the equation for a square?

The Formula for the Area of A Square The area of a square is equal to (side) × (side) square units. The area of a square when the diagonal, d, is given is d2÷2 square units. For example, The area of a square with each side 8 feet long is 8 × 8 or 64 square feet (ft2).


2 Answers

This would be faster I guess:

function getLengthForDeg(phi){
    phi = ((phi+45)%90-45)/180*Math.PI;
    return 1/Math.cos(phi);
}
like image 125
Mano Kovacs Avatar answered Sep 19 '22 23:09

Mano Kovacs


Original post is tagged Javascript, but I needed this for typed languages (e.g. C) where you can't modulus a float.

MonoMano's answer is correct, but for anyone else coming here needing the same thing, here's MonoMano's answer modified for C / ObjC / Java / etc:

/** c.f. http://stackoverflow.com/a/4788992/153422
* M_PI_2 is a constant: "PI / 2"
* M_PI_4 is a constant: "PI / 4"
*/
double getSquarePolarRadiusForRad(double phi){
    double phiInPiBy4Range = phi;
    while( phiInPiBy4Range > M_PI_4 )
        phiInPiBy4Range -= M_PI_2;
    while( phiInPiBy4Range < - M_PI_4 )
        phiInPiBy4Range += M_PI_2;

    return 1/cos(phiInPiBy4Range);
}
like image 24
Adam Avatar answered Sep 17 '22 23:09

Adam