52

ウィンドウに描画する線があり、ユーザーにドラッグさせます。したがって、私の線は(x1、y1)と(x2、y2)の2つの点で定義されます。しかし、ここで、線の端に「キャップ」を描画します。つまり、各端点に短い垂線を描画します。キャップの長さはNピクセルである必要があります。

したがって、終点(x1、y1)に「キャップ」ラインを描画するには、垂直線を形成し、各ポイントがポイント(x1、y1)からN/2ピクセル離れている2つのポイントを見つける必要があります。

では、既知の線、つまり(x1、y1)とで定義される線の終点(x1、y1)から垂直距離N / 2にある必要がある場合、点(x3、y3)をどのように計算しますか? (x2、y2)?

4

4 に答える 4

91

線分に垂直な単位ベクトルを計算する必要があります。ゼロ除算のエラーにつながる可能性があるため、勾配の計算は避けてください。

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
于 2008-09-25T15:22:17.083 に答える
7

直交バーサーを評価し、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
于 2008-09-25T15:25:03.597 に答える
4

2から1および1から3のベクトルは垂直であるため、それらの内積は0です。

これにより、2つの未知数が残ります。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

普段はやらないのですが、職場で解決して、徹底的に説明することで知識が固まると思いました。

于 2011-03-26T01:25:22.267 に答える
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)
于 2008-09-25T15:39:52.110 に答える