Tag Archives: point

Calculate a point at a distance perpendicular to a vector offset (2D)

To calculate a point at a distance perpendicular to a vector offset

Point perpendicular to offset

First the point on the vector P' at the required offset needs to be calculated. This easy to do:

P' = A + (B-A) * offset

P'_x = A_x + (B_x - A_x) * offset
P'_y = A_y + (B_y - A_y) * offset

The vector to move the point P’ on is perpendicular to \overline{AB}. This vector can be calculated using a 90 degree rotation matrix:

\begin{bmatrix}  cos \alpha   & -sin \alpha \\   sin \alpha  & cos \alpha \\ \end{bmatrix}   = \begin{bmatrix}  cos 90  & -sin 90 \\   sin 90  & cos 90\\ \end{bmatrix} = \begin{bmatrix}  0  & -1 \\   1  &  0 \\   \end{bmatrix}

The matrix shows in the the first row x'= -y and the second row shows y'= x

You can safely ignore the stuff above if you are only interested in getting the job done. The final formula is:

\begin{pmatrix}x'\\y' \end{pmatrix}= \begin{pmatrix}-y\\x \end{pmatrix}

We will call the vector (\overline{P'P}) vector C.

C =\begin{pmatrix}- (B_y - A_y)\\   B_x - A_x \end{pmatrix}

Vector C needs to be normalized (divided by it’s length / magnitude).

\hat{C} =\dfrac{C}{\left \|C \right \|}

The resulting formula becomes:

P = (A + (B-A) * offset) + \hat {C} * distance

A positive distance will get the point above the vector, a negative below the vector.

Lastest update in March 2022, inital post in October 2011

Get a offset (or point) on a vector perpendicular to a point

 

To find the offset on a vector perpendicular to a point use the dot product

Assume the vector is defined by P1 and P2  and the point by P

in 2d

            (P.x - P1.x) * (P2.x - P1.x) + (P.y - P1.y) * (P2.y - P1.y)
offset =    -----------------------------------------------------------------
            (P2.x - P1.x) * (P2.x - P1.x) + (P2.y - P1.y) * (P2.y - P1.y)

or 3d

            (P.x - P1.x) * (P2.x - P1.x) + (P.y - P1.y) * (P2.y - P1.y) + (P.z - P1.z) * (P2.z- P1.z)
offset =    ---------------------------------------------------------------------------------------------------
            (P2.x - P1.x) * (P2.x - P1.x) + (P2.y - P1.y) * (P2.y - P1.y) + (P2.z - P1.z) * (P2.z - P1.z)

notes:

If vector P1,P2 has been normalized (length 1) the division by the length is obviously not necessary. If not the result is divided the square length. Although divided by the length (square root) the result will equal that of a normalized vector. Keep in mind that the dot project is a projection on the vector thus requiring a additional division by the length!

 

Calculating the point on the vector can be done using

P.x =  P1.x + (P2.x - P1.x) * o;

P.y =  P1.y + (P2.y - P1.y) * o;

P.z =  P1.z + (P2.z - P1.z) * o;  (3d)
Lastest update in June 2011, inital post in June 2011