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.
To convert from polar coordinates to rectangular coordinates, use the formulas x=rcosθ and y=rsinθ.
The inverse tangent of √33 is θ=30° θ = 30 ° . This is the result of the conversion to polar coordinates in (r,θ) form.
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).
This would be faster I guess:
function getLengthForDeg(phi){
phi = ((phi+45)%90-45)/180*Math.PI;
return 1/Math.cos(phi);
}
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With