开发者

Calculate a point normal to a line

开发者 https://www.devze.com 2023-02-15 03:55 出处:网络
I have a line L specified by (x1,y1)(x2,y2) and want to calculate the coordinates of the point that is:

I have a line L specified by (x1,y1)(x2,y2) and want to calculate the coordinates of the point that is:

  • located on the normal that intersects L at the half of its length
  • is a certain distance D away from L

Examples:

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

But i dont know how to calculate the coordi开发者_开发技巧nates in a generic way for all lines regardless of the angle (-Pi to Pi).

Thanks in advance!


The center between both points is given by

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

while the (unnormalized) normal is

(-(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 find the two points

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

which both fulfill your requirements.

0

精彩评论

暂无评论...
验证码 换一张
取 消