2011-02-14 64 views
0

嗨的終點正在尋找一些幫助尋找對角線上的一個點時,我有起點和線

我有我的論壇圖片框畫出對角線,我需要知道如果用戶點擊了線

我開始點和線的終點和鼠標x,y位置

所以我基本上需要找出如果X,鼠標的y爲上該線。

任何人都可以幫忙嗎?

由於

回答

3

實施例:行開始點(A)爲(0,0),結束點(B)是(10,5)。因此 斜率線的是:

m(slope) = (y2 - y1)/(x2 - x1) 
     = (5 - 0)/(10 - 0) 
     = 5/10 
     = 0.5 

要檢查是否您的點(x,y)的(C)是上線就必須有從A-> C和C->乙相同的斜率。所以再次進行相同的計算。說點是(4,2)

m(AC) = (2 - 0)/(4 - 0) 
     = 2/4 
     = 0.5 

m(CB) = (5 - 2)/(10 - 4) 
     = 3/6 
     = 0.5 

因此,這一點將在AB線上。

如果點爲(20,10)

m(AC) = (10 - 0)/(20 - 0) 
     = 10/20 
     = 0.5 

然而:

m(CB) = (5 - 10)/(10 - 20) 
     = -5/-10 
     = -0.5 

類似地,如果點是(2,2)

m(AC) = (2 - 0)/(2 - 0) 
     = 2/2 
     = 1 

m(CB) = (5 - 2)/(10 - 2) 
     = 3/8 
     = 0.375 

所以對於一個點是在線上m(AB) == m(AC) == m(CB)

由於您可能無法獲取小數值,因此您可能需要一些解決方法才能執行,並且您的線條寬度可能超過一個像素,但這些基本原則應該會引導您完成。

1

給定兩點,(2,4)和(-1,-2)確定線的斜率截距形式。

1. Determine the slope 

y1-y2 4-(-2) 6 
----- = ------= --- = 2 = M 
x1-x2 2-(-1) 3 


2. To slope intercept form using one of the original points and slope from above. 

(y - y1) = m(x - x1) 

(y - 4) = 2(x - 2) 

y - 4 = 2x - 4 

y = 2x + 0 (0 is y intercept) 

y = 2x (y = 2x + 0) is in slope intercept form 


3. To determine if a point lies on the line, plug and chug with the new point. 

    new point (1,2) does y = 2x? 2 = 2(1) = true so (1,2) is on the line. 
    new point (2,2) does y = 2x? 2 = 2(2) = false so (2,2) is not on the line. 

在你原來的問題中你說過的路線,但我認爲你可能是指線段。如果您的意思是後者,您還需要驗證新的x和y是否位於給定段的範圍內。

該代碼將是這個樣子

Dim pta As Point = New Point(2, 4) 
    Dim ptb As Point = New Point(-1, -2) 

    Dim M As Double 
    If pta.X - ptb.X <> 0 Then 
     M = (pta.Y - ptb.Y)/(pta.X - ptb.X) 
    End If 

    '(y - pta.y) = M(x - pta.x) 
    'y - pta.y = Mx - m(pta.x) 
    'y = Mx - M(pta.x) + pta.y 

    Dim yIntercept As Double = (-M * pta.X) + pta.Y 

    Dim ptN1 As Point = New Point(1, 2) 
    Dim ptN2 As Point = New Point(2, 2) 

    If ptN1.Y = (M * (ptN1.X)) + yIntercept Then 
     Stop 
    Else 
     Stop 
    End If 

    If ptN2.Y = (M * (ptN2.X)) + yIntercept Then 
     Stop 
    Else 
     Stop 
    End If