2010年7月23日 星期五

兩直線間的關係

兩直線間的關係
在2維平面中兩條直線間的關係共有3種:
1.相交於一點
2.重疊
3.平行
輸入4個點,分別為兩條直線。
輸出兩條直線的關係。
輸入說明:
每組輸入包含8個數字,x1 y1 x2 y2 x3 y3 x4 y4 ,代表4個點(x1,y1)..(x4,y4)
一條直線會通過(x1,y1)、(x2,y2), 另一條線通過另外兩點。
輸出說明:
請輸出1或2或3,分別代表1.相交於一點,2.重疊,3.平行。
輸入範例:
4 2 0 2 1 -1 2 2
1 3 2 4 -1 1 -2 0
3 5 0 2 1 5 -1 3
輸出範例:
1
2
3

6 則留言:

  1. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    While Not EOF(1)
    Input #1, X1, Y1, X2, Y2, X3, Y3, X4, Y4
    m1 = (Y2 - Y1) / (X2 - X1)
    m2 = (Y4 - Y3) / (X4 - X3)
    If m1 <> m2 Then
    Print #2, 1
    Else
    If (X3 - X1) <> 0 Then m3 = (Y3 - Y1) / (X3 - X1) Else m3 = X1
    If (X4 - X2) <> 0 Then m4 = (Y4 - Y2) / (X4 - X2) Else m4 = X2
    If (X4 - X1) <> 0 Then m5 = (Y4 - Y1) / (X4 - X1) Else m5 = X1
    If (X3 - X2) <> 0 Then m6 = (Y3 - Y2) / (X3 - X2) Else m6 = X2
    If m3 = m4 And m5 = m6 Then
    Print #2, 2
    Else
    Print #2, 3
    End If
    End If
    Wend
    Close #1
    Close #2
    End
    End Sub

    輸入
    4 2 0 2 1 -1 2 2
    1 3 2 4 -1 1 -2 0
    3 5 0 2 1 5 -1 3
    1 -1 5 3 1 5 -1 3
    2 -3 -6 5 1 -1 3 5

    輸出
    1
    2
    3
    3
    1

    回覆刪除
  2. Private Sub Form_Load()
    Me.Hide
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do Until EOF(1)
    Input #1, X1, Y1, X2, Y2, x3, y3, X4, Y4
    If (Y1 - Y2) <> 0 Then m1 = (X1 - X2) / (Y1 - Y2) Else m1 = 0
    If (y3 - Y4) <> 0 Then m2 = (x3 - X4) / (y3 - Y4) Else m2 = 0
    If m1 = m2 Then
    If (X1 - x3) = m1 * (Y1 - y3) Then Print #2, 2 Else Print #2, 3
    Else
    Print #2, 1
    End If
    Loop
    Close #2
    Close #1
    End
    End Sub

    回覆刪除
  3. Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do While Not EOF(1)
    Input #1, X1, Y1, X2, Y2, x3, y3, x4, y4
    m1 = (Y2 - Y1) / (X2 - X1): If X2 - X1 = 0 Then m1 = 0
    m2 = (y4 - y3) / (x4 - x3): If x4 - x3 = 0 Then m2 = 0
    If m1 = m2 Then
    If (X1 - x3) = m1 * (Y1 - y3) Then
    Print #2, 2
    Else
    Print #2, 3
    End If
    Else
    Print #2, 1
    End If
    Loop
    Close #1
    Close #2
    End Sub

    回覆刪除
  4. Private Sub Form_Load()
    Open App.Path & "\in.txt" For Input As #1
    Open App.Path & "\out.txt" For Output As #2
    Do While Not EOF(1)
    Input #1, X1, Y1, X2, Y2, x3, y3, x4, y4
    If X2 - X1 = 0 Then m1 = 0 Else m1 = (Y2 - Y1) / (X2 - X1)
    If x4 - x3 = 0 Then m2 = 0 Else m2 = (y4 - y3) / (x4 - x3)
    If m1 = m2 Then
    If (X1 - x3) = m1 * (Y1 - y3) Then
    Print #2, 2
    Else
    Print #2, 3
    End If
    Else
    Print #2, 1
    End If
    Loop
    Close #1
    Close #2
    End Sub

    回覆刪除
  5. 這題小白阿揚阿瑋都ok, 當然,要注意阿瑋第一次po的程式中的那個0的小錯誤。
    熊掌

    回覆刪除
  6. Dim x1, x2, x3, x4, y1, y2, y3, y4
    Private Sub Form_Load()
    Open App.Path & "/in.txt" For Input As #1
    Open App.Path & "/out.txt" For Output As #2
    Do While Not EOF(1)
    Input #1, x1, y1, x2, y2, x3, y3, x4, y4
    a = (y2 - y1) / (x2 - x1)
    b = (y4 - y3) / (x4 - x3)
    If a <> b Then
    Print #2, 1
    Else
    c = x1 / (x2 - x1)
    d = x3 / (x4 - x3)
    If y1 - c * (y2 - y1) = y3 - d * (y4 - y3) Then
    Print #2, 2
    Else
    Print #2, 3
    End If
    End If
    Loop
    Close #2
    Close #1
    End Sub

    回覆刪除