Calculate the position of two points based on direction and midpoint.

310 Views Asked by At

I'm working on a program that needs to create a wall with only two points and height as input. I have the start point and the end point of the wall and the thickness of the wall.

I need to calculate the four corners of the wall and then draw lines between those points.

I've tried the following steps:

1.) Calculate wall direction

2.) Calculate angle in radians and degrees

3.) Add and Subtract direction * (wall height / 2)

But my points are always a little offset.

direction = endPoint - startPoint
normalDirection = (1.0, 0.0) // this is a direction vector from left to right along the x-axis

dot = dir.x*normDir.x + dir.y*normDir.y
det = dir.x*normDir.y - dir.y*normDir.x
angle = atan2(det, dot)
deg = DEGREES(angle) // this is just a function of my program that does input * 180/pi

topLeftCorner.x = startPoint.x - (direction.x * (wallThickness / 2))
topLeftCorner.y = startPoint.y - (direction.y * (wallThickness / 2))

bottomLeftCorner.x/y same as topLeft but startPoint.x/y + dir * thick / 2

TLDR: I have the black objects as input, and I'm trying to find the red circles position. The black lines linking the two red circles is the wall thickness and the black circles are the start and end points.

Note: the two black circles can be interchangeable meaning the one on the right can be the start point and the one on the left can be the end point and vice-versa. Black lines = input. Red = desired output

2

There are 2 best solutions below

0
On BEST ANSWER

In vector arithmetic, "normal" means perpendicular. It does not mean a default direction. Hence it does not make much sense to use $(1,0)$ as a "normal" vector, unless it is meant to be normal to a vector such as $(0,-1)$ or $(0,2),$ for which it really would be a normal (perpendicular) vector.

If you already have a direction vector $\hat v$ (pronounced "hat vee", which I would usually expect to be a vector whose length is $1$), the "calculation" of the normal vector $\hat n$ from this direction vector is hardly a calculation at all: \begin{align} \hat n_x &= -\hat v_y ,\\ \hat n_y &= \hat v_x .\\ \end{align}

The operation above gives you an output vector $\hat n$ of the same length as the input vector $\hat v$ but in a perpendicular direction. If you start with a vector that is not of unit length, however, and just apply this operation to it, multiplying by $\mathrm{wallThickness}/2$ will give you a wall that is either too thick or too thin. So make sure that each thing you call a "direction" vector is a unit vector (length $1$). If you have gotten a vector $v$ along the wall by the operation $$ v = \mathrm{end} - \mathrm{start}, $$ you can get a unit vector $\hat v$ in the same direction by the operation \begin{align} \hat v_x &= \frac{v_x}{\sqrt{v_x^2 + v_y^2}} ,\\ \hat v_y &= \frac{v_y}{\sqrt{v_x^2 + v_y^2}} . \end{align}

0
On

Bleh, I had to calculate the perpendicular direction vector of my direction variable to make it work!