Draw a square with polar coordinates

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

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

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

In other words, when you draw a function with a polar graphic, it draws a square.

I believe the function will be something like this:

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

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

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

Any help would be appreciated. Thanks in advance.

+7
source share
4 answers

That would be faster, I think:

 function getLengthForDeg(phi){ phi = ((phi+45)%90-45)/180*Math.PI; return 1/Math.cos(phi); } 
+9
source

I came up with an equation that can work with TI calculators due to their theta step function in the window section of their polar graph function. I do not know if this will help you. I think it can only work if you can set up a theta step (or degree).

 r = (((s*sqrt(2)) - 5)/4) sin(4(x - (pi/8)) + (((s*sqrt(2)) + s)/4) 

where s is the length of the side of the desired square

Set theta step to pi / 4 to build the basic points needed to create a square image

Again, this works with TI calculators.

+1
source

The original entry is marked with Javascript, but I need it for typed languages ​​(like C), where you cannot use the float module.

MonoMano's answer is correct, but for those who come here who need the same, here MonoMano responds to changes for C / ObjC / Java, etc.:

 /** cf 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); } 
+1
source

I am not well versed in Javascript, but in the format used in Wolfram Alpha, the formula is the radius from the corner:

min (1 / abs (cos (theta)), 1 / abs (syn (theta))))

+1
source

All Articles