2008-09-25 186 views
36

我在窗口中繪製一條線,並讓用戶拖動它。所以,我的線由兩點定義:(x1,y1)和(x2,y2)。但是現在我想在我的線條的末端繪製「帽子」,也就是說,在我的每個端點上都有短垂線。帽應該是N個像素的長度。因此,爲了在結束點(x1,y1)畫出我的「帽子」線,我需要找到兩個點形成一條垂直線,其中每個點都離開點(x1 ,Y1)。你如何找到距離某條線的給定垂直距離上的點?

因此,如果需要在距離已知線的終點(x1,y1)的垂直距離N/2處計算一個點(x3,y3),即由(x1 ,y1)和(x2,y2)?

+0

有關詳細解決方案,請參見此處(http://stackoverflow.com/a/17195324/183120)。 – legends2k 2013-10-19 05:58:15

回答

72

您需要計算垂直於線段的單位矢量。避免計算斜率,因爲這可能導致除以零錯誤。

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

我一直在想,必須有一種方法來避免那裏令人討厭的sqrt,可能是通過使用Breshenham's Line,但我無法想到它。 – 2008-09-25 15:29:04

5

你只是評估正交versor,如果你想避免開方由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 
1

繁殖,做到以下幾點:

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

由於從2載體到1和1到3是垂直的,它們的點積是0.

這給你留下兩個未知數:x從1到3(x1 3)和y從1到3(y13)

使用畢達哥拉斯定理爲這些未知數獲得另一個方程。

解決每個未知的替代...

這需要平方和unsquaring,讓你失去了你的方程有關的標誌。

確定的標誌,可以考慮:

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

方程:勾股定理

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 

方程:求解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 

方程:求解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 

我通常不會這樣做,但我在工作中解決了這個問題,並認爲徹底解釋它會幫助我鞏固自己的知識。

相關問題