Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 你如何在一条直线的给定垂直距离上找到一个点?_Algorithm_Math_Graphics_Geometry_Coordinates - Fatal编程技术网

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

Algorithm 你如何在一条直线的给定垂直距离上找到一个点?,algorithm,math,graphics,geometry,coordinates,Algorithm,Math,Graphics,Geometry,Coordinates,我在窗口中画了一条线,让用户拖动它。因此,我的直线由两点定义:(x1,y1)和(x2,y2)。但是现在我想在我的线的末端画“caps”,也就是说,在我的每个端点画一条短的垂直线。大写字母长度应为N个像素 因此,要在端点(x1,y1)绘制“帽”线,我需要找到两个形成垂直线的点,其中每个点距离该点(x1,y1)有N/2个像素 那么,如果一个点(x3,y3)需要与已知直线的端点(x1,y1)保持N/2的垂直距离,即(x1,y1)和(x2,y2)定义的直线,那么如何计算该点呢?您需要计算一个垂直于线段的

我在窗口中画了一条线,让用户拖动它。因此,我的直线由两点定义:(x1,y1)和(x2,y2)。但是现在我想在我的线的末端画“caps”,也就是说,在我的每个端点画一条短的垂直线。大写字母长度应为N个像素

因此,要在端点(x1,y1)绘制“帽”线,我需要找到两个形成垂直线的点,其中每个点距离该点(x1,y1)有N/2个像素


那么,如果一个点(x3,y3)需要与已知直线的端点(x1,y1)保持N/2的垂直距离,即(x1,y1)和(x2,y2)定义的直线,那么如何计算该点呢?

您需要计算一个垂直于线段的单位向量。避免计算斜率,因为这可能导致被零除的错误

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

你只需要计算正交向量,然后乘以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

如果要避免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)

因为从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
  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
方程a:用比例求解x13

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

我通常不会这样做,但我在工作中解决了它,并认为彻底解释它将有助于我巩固知识。

我一直在想,一定有一种方法可以避免这种令人讨厌的sqrt,可能是通过使用Breshenham的线,但我不能马上想到。我认为你的计算或第3点和第4点中有符号错误。在最后四行中使用(+-++)或(-++-),否?在JSFIDLE上组合公式的可视化:您可以通过修改线来更改ax、ay和bx的值,并看到垂直线相应地调整…@PaulTomblin一个近似解决方案是将线本身翻转90度,w/
(dx,dy)->(-dy,dx)
;然后通过
N/L_aprx
近似缩放它,其中
L_aprx=(|dx+|dy)*0.83
。也许已经足够好了。两个刻度的长度将
(10%(+-10%)
关闭。任何人都可以解释这个伪代码的作用,我确实知道它是有效的,但我的问题是如何工作?对于详细的解决方案。