简体   繁体   English

你如何找到与直线垂直距离的点?

[英]How do you find a point at a given perpendicular distance from a line?

I have a line that I draw in a window and I let the user drag it around.我在窗口中绘制了一条线,然后让用户拖动它。 So, my line is defined by two points: (x1,y1) and (x2,y2).所以,我的线由两点定义:(x1,y1) 和 (x2,y2)。 But now I would like to draw "caps" at the end of my line, that is, short perpendicular lines at each of my end points.但现在我想在我的线的末端画“帽”,即在我的每个端点处画短垂直线。 The caps should be N pixels in length.大写字母的长度应为 N 像素。

Thus, to draw my "cap" line at end point (x1,y1), I need to find two points that form a perpendicular line and where each of its points are N/2 pixels away from the point (x1,y1).因此,要在端点 (x1,y1) 处绘制我的“帽”线,我需要找到形成垂直线的两个点,其中每个点距点 (x1,y1) 的距离为 N/2 个像素。

So how do you calculate a point (x3,y3) given it needs to be at a perpendicular distance N/2 away from the end point (x1,y1) of a known line, ie the line defined by (x1,y1) and (x2,y2)?那么你如何计算一个点 (x3,y3),因为它需要距离一条已知线的端点 (x1,y1) 的垂直距离为 N/2,即由 (x1,y1) 和(x2,y2)?

You need to compute a unit vector that's perpendicular to the line segment.您需要计算垂直于线段的单位向量。 Avoid computing the slope because that can lead to divide by zero errors.避免计算斜率,因为这会导致除以零误差。

dx = x1-x2
dy = y1-y2
dist = sqrt(dx*dx + dy*dy)
dx /= dist
dy /= dist
x3 = x1 + (N/2)*dy
y3 = y1 - (N/2)*dx
x4 = x1 - (N/2)*dy
y4 = y1 + (N/2)*dx

You just evaluate the orthogonal versor and multiply by N/2您只需评估正交向量并乘以 N/2

vx = x2-x1
vy = y2-y1
len = sqrt( vx*vx + vy*vy )
ux = -vy/len
uy = vx/len

x3 = x1 + N/2 * ux
Y3 = y1 + N/2 * uy

x4 = x1 - N/2 * ux
Y4 = y1 - N/2 * uy

Since the vectors from 2 to 1 and 1 to 3 are perpendicular, their dot product is 0.由于从 2 到 1 和 1 到 3 的向量是垂直的,因此它们的点积为 0。

This leaves you with two unknowns: x from 1 to 3 (x13), and y from 1 to 3 (y13)这给你留下了两个未知数:x 从 1 到 3 (x13) 和 y 从 1 到 3 (y13)

Use the Pythagorean theorem to get another equation for those unknowns.使用勾股定理得到这些未知数的另一个方程。

Solve for each unknown by substitution...通过替换解决每个未知数...

This requires squaring and unsquaring, so you lose the sign associated with your equations.这需要平方和不平方,因此您会丢失与方程式相关的符号。

To determine the sign, consider:要确定符号,请考虑:

while x21 is negative, y13 will be positive
while x21 is positive, y13 will be negative
while y21 is positive, x13 will be positive
while y21 is negative, x13 will be negative

Known: point 1 : x1 , y1已知:点 1 : x1 , y1

Known: point 2 : x2 , y2已知:点 2 : x2 , y2

x21 = x1 - x2
y21 = y1 - y2

Known: distance |1->3|已知:距离|1->3| : N/2 : N/2

equation a: Pythagorean theorem方程a:勾股定理

x13^2 + y13^2 = |1->3|^2
x13^2 + y13^2 = (N/2)^2

Known: angle 2-1-3 : right angle已知:角 2-1-3 :直角

vectors 2->1 and 1->3 are perpendicular向量 2->1 和 1->3 是垂直的

2->1 dot 1->3 is 0 2->1 点 1->3 是 0

equation b: dot product = 0方程 b:点积 = 0

x21*x13 + y21*y13 = 2->1 dot 1->3
x21*x13 + y21*y13 = 0

ratio b/w x13 and y13:比率 b/w x13 和 y13:

x21*x13 = -y21*y13
x13 = -(y21/x21)y13

x13 = -phi*y13

equation a: solved for y13 with ratio方程a:用ratio求解y13

  plug x13 into a
phi^2*y13^2 + y13^2 = |1->3|^2

  factor out y13
y13^2 * (phi^2 + 1) = 

  plug in phi
y13^2 * (y21^2/x21^2 + 1) = 

  multiply both sides by x21^2
y13^2 * (y21^2 + x21^2) = |1->3|^2 * x21^2

  plug in Pythagorean theorem of 2->1
y13^2 * |2->1|^2 = |1->3|^2 * x21^2

  take square root of both sides
y13 * |2->1| = |1->3| * x21

  divide both sides by the length of 1->2
y13 = (|1->3|/|2->1|) *x21

  lets call the ratio of 1->3 to 2->1 lengths psi
y13 = psi * x21

  check the signs
    when x21 is negative, y13 will be positive
    when x21 is positive, y13 will be negative

y13 = -psi * x21

equation a: solved for x13 with ratio方程 a:用比率求解 x13

  plug y13 into a
x13^2 + x13^2/phi^2 = |1->3|^2

  factor out x13
x13^2 * (1 + 1/phi^2) = 

  plug in phi
x13^2 * (1 + x21^2/y21^2) = 

  multiply both sides by y21^2
x13^2 * (y21^2 + x21^2) = |1->3|^2 * y21^2

  plug in Pythagorean theorem of 2->1
x13^2 * |2->1|^2 = |1->3|^2 * y21^2

  take square root of both sides
x13 * |2->1| = |1->3| * y21

  divide both sides by the length of 2->1
x13 = (|1->3|/|2->1|) *y21

  lets call the ratio of |1->3| to |2->1| psi
x13 = psi * y21

  check the signs
    when y21 is negative, x13 will be negative
    when y21 is positive, x13 will be negative

x13 = psi * y21

to condense凝结

x21 = x1 - x2
y21 = y1 - y2

|2->1| = sqrt( x21^2 + y^21^2 )
|1->3| = N/2

psi = |1->3|/|2->1|

y13 = -psi * x21
x13 =  psi * y21

I normally wouldn't do this, but I solved it at work and thought that explaining it thoroughly would help me solidify my knowledge.我通常不会这样做,但我在工作中解决了它,并认为彻底解释它会帮助我巩固我的知识。

If you want to avoid a sqrt, do the following:如果要避免 sqrt,请执行以下操作:

in: line_length, cap_length, rotation, position of line centre

define points:
  tl (-line_length/2, cap_length)
  tr (line_length/2, cap_length)
  bl (-line_length/2, -cap_length)
  br (line_length/2, -cap_length)

rotate the four points by 'rotation'
offset four points by 'position'

drawline (midpoint tl,bl to midpoint tr,br)
drawline (tl to bl)
drawline (tr to br)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定两个点集A和B,您如何找到距B最远的A点? - Given two point sets A and B, how do you find the point in A farthest away from B? 最小垂直3D平面算法中点与线的距离 - Minimum perpendicular Distance of a point to a line in 3D plane algorithm 如何在 python 的表面上找到点的垂直投影 - how to find perpendicular projection of point on a surface in python 找到形成与直线AB垂直的直线BC的点C - Find point C that forms perpendicular line BC to line AB 给定Vector3和距离的列表,如何计算分段线上的点? - How do I calculate a point on a segmented line given a list of Vector3 and a distance? 在距平面上N个其他点最小距离的线上找到一个点 - Find a point on line at minimal distance from N other point on the plane 查找与形式为Ax + By + C = 0的给定线垂直的线的算法 - Algorithm to find lines perpendicular to a given line of the form Ax+By+C=0 地球上点到线的距离 - Distance from point to line on Earth 查找点到多边形的距离 - Find distance from a point to a polygon 如何以垂直角度在blob上采样线? (在Python / OpenCV中,除非你建议切换到别的东西) - How do I sample a line across a blob at a perpendicular angle? (in Python/OpenCV unless you suggest switching to something else)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM