Calculate a point normal to a line

I have the line L given by (x1, y1) (x2, y2) and you want to calculate the coordinates of the point:

  • located on the normal crossing L at half length
  • - some distance D from L

Examples:

  • If the line (x1, a) (x2, a) (horizontal) contains the coordinates, the calculated point will be ((x2-x1) / 2, D).
  • If the string (a, y1) (a, y2) (vertical) contains the coordinates of the calculated point, it would be (D, (y2-y1) / 2).

But I do not know how to calculate the coordinates in a general way for all lines regardless of the angle (-Pi to Pi).

Thanks in advance!

+3
source share
1 answer

The center between both points is determined

((x1+x2)/2, (y1+y2)/2)

while (abnormal) normal

(-(y2-y1), (x2-x1))

If we normalize this vector, we get

(-(y2-y1), (x2-x1)) / sqrt((x2-x1)^2+(y2-y1)^2)

and if we combine both, we will find two points

((x1+x2)/2, (y1+y2)/2) +- D * (-(y2-y1), (x2-x1)) / sqrt((x2-x1)^2+(y2-y1)^2)

.

+8

All Articles