How to find a point on the perimeter of a square?

406 Views Asked by At

Given the four corners $(x, y)$ of a square, the center $(x, y)$, and a starting direction (ex: $45^\circ$) around a $360^\circ$ rotation ($0^\circ/360^\circ$ at the top), how do you find out the intercept on the perimeter as the direction is incrementally stepped?

What I'm basically doing is taking a circle of radius $(r)$, determining it's area $(A)$, converting that to length $(L)$ of the sides of the square, and then determining the corners $(x, y)$ given the starting center point and the above described direction. This allows for the square to rotate it's starting position depending on the direction. I used the equations below to determine the corners (assuming $(x, y)$ is the center of the circle):

$$\left(x - \frac{L}{2} * (\sin(\text{rotation}) - \cos(\text{rotation})), y + \frac{L}{2} * (\sin(\text{rotation}) + \cos(\text{rotation}))\right)$$ $$\left(x + \frac{L}{2} * (\sin(\text{rotation}) + \cos(\text{rotation})), y + \frac{L}{2} * (\sin(\text{rotation}) - \cos(\text{rotation}))\right)$$ $$\left(x + \frac{L}{2} * (\sin(\text{rotation}) - \cos(\text{rotation})), y - \frac{L}{2} * (\sin(\text{rotation}) + \cos(\text{rotation}))\right)$$ $$\left(x - \frac{L}{2} * (\sin(\text{rotation}) + \cos(\text{rotation})), y - \frac{L}{2} * (\sin(\text{rotation}) - \cos(\text{rotation}))\right)$$

Example Diagram

2

There are 2 best solutions below

1
On BEST ANSWER

I am guessing you mean what would be the point on the perimeter of a square from a line in polar coordinates as $\theta=\theta_0\implies y=x\,\tan(\theta)$; this equation just gives the line with slope $\tan(\theta)$. This is the same as asking what the “sine and cosine” for a square are. Imagine this as the “square analogue” of the trigonometric functions defined in parametric coordinates as:

$$\mathrm{(cos_ \square(\theta),sin_ \square(\theta))=a|x|+a|y|=1}$$

To find theses “square” functions, simply construct and solve for x in terms of theta only:

$$\mathrm{y=x\,tan(\theta),a|x|+a|y|=1\implies a|x|+a|x||tan(\theta)|=1\implies a|x|(1+|tan(\theta)|)=1\implies |x|=\frac 1{a|tan(\theta)|+a}\implies x=cos_\square(\theta)=\pm \frac 1{a|tan(\theta)|+a},y=sin_\square(\theta) \pm\frac {tan(\theta)}{a|tan(\theta)|+a}}$$

These functions have that the tangent square function is just the tangent function and the (co)secant analogous are just reciprocals of x and y as seen above.

Values for the square functions. You really just need values from $0\le\theta\le\frac{\pi}{4}$ as you can either switch x and y or negate the results based on the quadrant; here is an explanation. Remember that $$\mathrm{x=cos_\square(\theta)=\pm \frac 1{a|tan(\theta)|+a},y=sin_\square(\theta)= \pm\frac {tan(\theta)}{a|tan(\theta)|+a}:}$$

$\theta=0:x=\frac1a,y=0;\theta=\frac\pi6:x=\frac{1}{\frac a{\sqrt3}+a},y=\frac{\sqrt3}{\sqrt3a+3a},\theta=\frac\pi4:x=y=\frac{1}{2a}$

This will also mean that the rest of these “square” based trigonometric functions are. In fact, this method always gives the new modified tangenr function as tan(θ) because of the setup:

$$\mathrm{\frac{y}{x}=tan_\square(\theta)=\frac{\pm\frac {tan(\theta)}{a|tan(\theta)|+a}}{\pm \frac 1{a|tan(\theta)|+a}}=tan(\theta)\implies cot_{\square}(\theta)=cot(\theta),sec_{\square}(\theta)= \frac{1}{cos_\square(\theta)}=\pm(a|tan(\theta)|+a),csc_\square(\theta)=\frac{1}{sin_{\square}}=\frac{1}{ \pm\frac {tan(\theta)}{a|tan(\theta)|+a}}= \pm\frac{a|tan(\theta)|+a} {tan(\theta)}=\pm (a\,sgn(tan(\theta))+a\,cot(\theta))}$$ Please see this

complete graph

for proof of these equations and visuals of the other (co)secant graphs. Tell me if you meant something else. Please correct me and give me feedback.

1
On

One would first solve the problem in a normalized coordinate system, where the square's edges run parallel to the axes, and then rotate the solutions to the rotated-recentered square. Let $\vec x_C$ be the two Cartesian coordinates of the square center, and $\vec x_1$ be the coordinates of a square corner. [Obviously $\vec x_3=2\vec x_C-\vec x_1$ is the opposite corner.] $L=\sqrt{2}|\vec x_1-\vec x_C|$ is the side length of the square.

Define the angle $\omega$ that rotates from normalized to the rotated/decentered coordinate system via $$ \vec x_1= \left(\begin{array}{cc}\cos\omega & -\sin\omega \\ \sin\omega & \cos\omega\end{array}\right)\cdot\left(\begin{array}{c}L/2 \\ L/2\end{array}\right)+\vec x_C = \frac{L}{\sqrt 2}\left(\begin{array}{c}\cos(\omega+45^\circ) \\ \sin(\omega+45^\circ) \end{array}\right)+\vec x_C, $$ where we have obviously chosen $(L/2,L/2)$ to be the coordinates of corner $\vec x_1$ in the normalized coordinate system. So $\omega$ is computed by the arctan of the ratio of the two components of $\vec x_1-\vec x_C$.

Let $\alpha$ be a direction in the normalized coordinate system. The ray intercepts the square in that coordinate system at $$ \vec x_i = \left(\begin{array}{c}L/2 \\ (L/2)\tan\alpha\end{array}\right),\quad -45^\circ\le \alpha \le 45^\circ $$ $$ \vec x_i = \left(\begin{array}{c}(L/2)\cot \alpha \\ L/2\end{array}\right),\quad 45^\circ\le \alpha \le 135^\circ $$ $$ \vec x_i = \left(\begin{array}{c}-L/2 \\ -(L/2)\tan\alpha\end{array}\right),\quad 135^\circ\le \alpha \le 225^\circ $$ $$ \vec x_i = \left(\begin{array}{c}-(L/2)\cot\alpha \\ -L/2\end{array}\right),\quad -135^\circ\le \alpha \le -45^\circ $$ for the four quadrants.

And the coordinates of interception in the rotated coordinate system are $$ \left(\begin{array}{cc}\cos\omega & -\sin\omega \\ \sin\omega & \cos\omega\end{array}\right)\cdot \vec x_i+\vec x_C $$ and the ray runs into the direction $\omega+\alpha$.