First, find the slope of your middle line. Call it m
. Problem: The middle line might be vertical. This could cause problems. So instead calculate deltaX
(the change in X) and deltaY
(the change in Y) between the points P1 and P2. Thinking of these two numbers as giving you a "direction" is the key to solving this problem.
To make the corners of your rectangle, you want to move from Point 1 in the perpendicular direction. The perpendicular direction is given by -1/m
. To avoid the danger of dividing by 0, a better way to think about it is that you want to move in the X direction by -deltaY
each time you move in the Y direction by deltaX
.
You will want to normalize your "direction" given by -deltaY
and deltaX
. To do this, find the distance between P1 and P2. I will call this distance D
. Now you want to do the following:
For the first corner, start at P1 and move in the x-direction by -deltaY / D
times width/2
. Move in the y-direction by deltaX / D
times width/2
.
For the second corner, start at P1 and move in the x-direction by -deltaY / D
times -width/2
. Move in the y-direction by deltaX / D
times -width/2
.
For the third corner, start at P2 and move in the x-direction by -deltaY / D
times width/2
. Move in the y-direction by deltaX / D
times width/2
.
For the fourth corner, start at P2 and move in the x-direction by -deltaY / D
times -width/2
. Move in the y-direction by deltaX / D
times -width/2
.
Good luck! What we are working with here is called a vector, but my language in the above answer is a little awkward because I've avoided using almost all of the language of vectors while writing it. The word "normalize" slipped in. Look forward to a class in college called "linear algebra," which will make you an expert at this question.