Formula for calculating rectangle vertices

2.2k Views Asked by At

I have a programming problem and I've forgotten some of my math rectangle formulas. I have to make a program that creates a rectangle that isn't parallel to the X and Y axis, I assume this means a diagonal rectangle in the first quadrant of a Cartesian graph.

My inputs are the following:

  • coordinates of the vertex closest to the X axis and the origin
  • angle the long side makes with relation to the X axis.
  • area of the rectangle
  • length of the long side

The outputs expected are the following:

  • length of the short side,
  • coordinates of all the rest of the rectangle's vertices.

Given these input how do I calculate the output mentioned?

1

There are 1 best solutions below

0
On

Let's say you have a rectangle with the coordinates $(0,0), (a,0), (0,b), (a,b)$. This is a rectangle with one vertex at the origin, and sides of length $a$ and $b$ which are parallel to the $x$ and $y$ axes.

For each point $(x,y)$ we can rotate it to a new point $(x', y')$ through an angle $\theta$ by this transformation:

$$x' = x \cos \theta - y \sin \theta$$ $$y' = x \sin \theta + y \cos \theta$$

Do this for all four points and you'll end up with your rotated rectangle. The lengths of the sides will be the same as for the unrotated rectangle.

Also, one of the points doesn't have to be the origin. You can do this with any rectangle you want.

So, to get the answer from your inputs, you have some coordinates you're inputting. You know the angle that this is to make with the $x$ axis (let's say $\beta$). So, you'll want to back out the rotation of these points by applying the equations above with an angle of $-\beta$. This will get you the unrotated coordinates. Now, you can position the other points of the rectangle (unrotated) and rotate them through an angle $+\beta$ to get your final coordinates.