如何在一条线的给定垂直距离处找到一个点?

时间:2008-09-25 15:12:20

标签: algorithm math graphics language-agnostic geometry

我在一个窗口画了一条线,我让用户拖动它。所以,我的线由两点定义:(x1,y1)和(x2,y2)。但是现在我想在我的线末端绘制“帽子”,也就是说,在我的每个终点处都有短垂线。大写字母的长度应为N像素。

因此,为了在终点(x1,y1)绘制我的“帽”线,我需要找到两个形成垂直线的点,并且每个点的距离都是N / 2个像素点(x1, Y1)。

那么你如何计算一个点(x3,y3),因为它需要与已知线的终点(x1,y1)保持垂直距离N / 2,即由(x1, y1)和(x2,y2)?

4 个答案:

答案 0 :(得分:80)

您需要计算垂直于线段的单位矢量。避免计算斜率,因为这会导致除以零误差。

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

答案 1 :(得分:5)

您只需评估正交值,然后乘以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

答案 2 :(得分:3)

由于从2到1和1到3的向量是垂直的,它们的点积是0。

这会留下两个未知数:x从1到3(x13),y从1到3(y13)

使用毕达哥拉斯定理得到那些未知数的另一个等式。

通过替换解决每个未知......

这需要平方和不平衡,因此您丢失了与方程相关的符号。

要确定标志,请考虑:

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

已知:第1点:x1,y1

已知:第2点:x2,y2

x21 = x1 - x2
y21 = y1 - y2

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

方程式a:毕达哥拉斯定理

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

已知:角度2-1-3:直角

载体2-> 1和1-> 3是垂直的

2-> 1点1-> 3是0

等式b:点积= 0

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

比率b / w x13和y13:

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

x13 = -phi*y13
方程a:用比率

求解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

等式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

压缩

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

我通常不会这样做,但我在工作中解决了这个问题,并认为彻底解释它会帮助我巩固我的知识。

答案 3 :(得分:1)

如果您想避开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)