# How to determine the position of neighbor points?

I have two points (red points represented on the image below), they form a line that is described by a specific formula (y = mx + n), I need to determine the coordinates of 4 other points (blue points) that are places on the two sides of the red segment and on the perpendicular lines that pass through the red points. The blue points are placed at equal distance from the red segment and this distance is known. My problem is to determine the positions of these 4 blue points if I know the position of red points (x1, y1, x2, y2) and the distance from the red segment. How can I do this?

P.S. This question will help me to solve a problem in determining some coordinates that I will use in the interaction between mouse and a drawing surface in a application environment (I asked it here: http://stackoverflow.com/questions/11485316/ispointinpath-for-stroked-lines-and-polylines)

-
The math is always simpler if you think of lines as $a x + b y + c = 0$ where $(a,b)$ is a vector perpendicular to the line and $h=-c/\sqrt{a^2+b^2}$ is the minimum distance of the line to the origin. – ja72 Sep 12 '14 at 11:53

The vector $s=(1,-m)$ is perpendicular to the red line. Let's normalize it, then we get unit vector $s=\left(\frac{1}{\sqrt{m^2+1}},-\frac{m}{\sqrt{m^2+1}}\right)$ perpendicular to the red line. Hence equation (in parametric form) of line passing through some point $(x_i,y_i)$ (where $i=1,2$) perpendicular to the red line is $$x=s_x t+x_i\qquad y=s_y t+y_i$$ Since $s$ is a unit vector, you just need to take $t=\pm d$ to get the desired points. Thus their coordinates are

For the first red point: $$\left(\frac{d}{\sqrt{1+m^2}}+x_1,\frac{md}{\sqrt{1+m^2}}+y_1\right)\\ \left(\frac{-d}{\sqrt{1+m^2}}+x_1,\frac{-md}{\sqrt{1+m^2}}+y_1\right)\\$$

For the second red point: $$\left(\frac{d}{\sqrt{1+m^2}}+x_2,\frac{md}{\sqrt{1+m^2}}+y_2\right)\\ \left(\frac{-d}{\sqrt{1+m^2}}+x_2,\frac{-md}{\sqrt{1+m^2}}+y_2\right)$$

-
I guess all answers are very good, but this one is the most simple to understand and implements in an application environment. – micnic Jul 17 '12 at 16:00

If $m=0$ the problem is trivial (you need vertical shifts just adjust the $y$-coordinate accordingly).

So we assume now $m \neq 0$. The slopes of perpendicular lines in the plane multiply out to -1, so the slope of both perpendicular lines will be $-1/m$, and so the direction in which you will need to move is given by the vector $[1, -1/m]$, which can be made of length 1 by dividing it by its own length, which is $L=\sqrt{1+1/m^2}$.

Assuming the coordinates of the red points are $[x_i,y_i]$ you now need the blue points, located at $[x_i, y_i] + d/L [1,-1/m]$.

-

You need to find a unit direction that is perpendicular to the red line. The red line direction is given by $h=(x_1-x_2,y_1-y_2)$, so a perpendicular direction can be found by rotating $\pm 90^{\circ}$.

This gives a direction $d = (y_2-y_1,x_1-x_2)$, and it is convenient to change the length to $1$, do we divide by the length to get $$\hat{d} = (\hat{d}_x,\hat{d}_y) = \frac{1}{\sqrt{(x_1-x_2)^2+(y_1-y_2)^2}} (y_2-y_1,x_1-x_2).$$

Then the four points you want are given by $$(x_1+\delta \hat{d}_x, y_1+\delta \hat{d}_y), (x_1-\delta \hat{d}_x, y_1-\delta \hat{d}_y)$$ $$(x_2+\delta \hat{d}_x, y_2+\delta \hat{d}_y), (x_2-\delta \hat{d}_x, y_2-\delta \hat{d}_y),$$ where $\delta$ is the distance of the new points from the ends of the red line.

-

Wouldn't it be nice if our point $(x_1,y_1)$ were at the origin, and the other point $(x_2,y_2)$ were on the positive $x$-axis? The solution would then be very easy.

So drag $(x_1,y_1)$ to the origin. Of course we have to drag $(x_2,y_2)$ the same distance in the same direction, to $(x_2-x_1, y_2-y_1)$. Then rotate the new line about the origin, until $(x_x-x_1,y_2,y_1)$ is rotated to a position on the positive $x$-axis.

Now solve the problem for the new position (trivial), finding the four appropriate blue points. Finally, transform back. (Undo the rotation, then undo the drag, in that order.) The four points we found easily get transformed to the points we were looking for.

-