Draw square with polar coordinates
Asked Answered
B

5

8

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.

Beauregard answered 25/1, 2011 at 0:44 Comment(4)
You really only need to know the answer for 1/8th of the square ...Dacoit
Yes, but the problem is to get every angle to correspond to the correct part of that 1/8 angle.Beauregard
Hey @Beauregard - come back and pick MonoMano's answer as it's the one that's actually right :-)Dacoit
@Dacoit - Thanks for being honest. :-)Beauregard
E
12

This would be faster I guess:

function getLengthForDeg(phi){
    phi = ((phi+45)%90-45)/180*Math.PI;
    return 1/Math.cos(phi);
}
Endmost answered 25/1, 2011 at 1:4 Comment(7)
I think you're missing a square root calculation, but I may be misunderstanding something.Dacoit
I do like the way you get the angle normalized.Dacoit
Cos(phi)=a/c where a is 1 (the horizontal edge) and we want to know c. Then c=a/cos(phi). Now, you mentioned that you have 8 pies, so you need to lock the interval of phi between -45 and 45 thats why I have (phi+45)%90-45 there. But I don't see the point using sqrt.Endmost
Uhh ... cos phi is the x-coordinate of the point on the unit circle intersected by a radius at angle phi, right?Dacoit
Well, yeah, but the needed data is 1/cos(x), because of the definition of cos.Endmost
Thanks, MonoMano. I believe that your function would be quicker than Pointy's.Beauregard
@Beauregard also it'll draw you an actual square and not a weird pincushion or something :-)Dacoit
K
4

I'm not familiar enough with Javascript, but in the format used in Wolfram Alpha, the formula for the radius from the angle is:

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

Knowing answered 20/3, 2014 at 0:22 Comment(2)
This doesn't seem right... if you only have an angle, there's no telling how long the radius would be.Fixing
@Fixing Since the OP specifically asked about a square of length 1, you don't need the radius.Reading
A
2

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. https://mcmap.net/q/1261660/-draw-square-with-polar-coordinates
* 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);
}
Anteversion answered 5/11, 2012 at 13:24 Comment(0)
V
1

I came up with an equation that can work with the TI calculators because of their theta step function in the window section of their polar graph function. I don't know if it helps you. I guess it can only work if you are able to configure the theta (or degree) step.

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 the theta step to pi/4 in order to plot the main points needed to create the image of a square

Again, this works with TI Calculators.

Vaticide answered 10/5, 2012 at 3:17 Comment(0)
P
0

This works, but uses the round function and absolute value function:

Abs((Round(1/2+Sin(((4t)+Pi)/2))Sec(t))+(Round(-Sin(((4t)+Pi)/2)+1/2)Csc(t)))

Sec = secant; Csc = cosecant; t = theta This uses radians, not degrees!

Photochronograph answered 10/3 at 1:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.