Hallo Christoph,
man kann das Ganze auch berechnen. Dazu braucht man aber keine höheren
mathematischen Funktionen. Vermutlich ist das die beste Methode:
'Benötigt werden: 1 Picturebox, 1 Label
Option Explicit
Private X1!(), Y1!(), X2!(), Y2!(), a!(), b!(), L%
Private Sub Form_Activate()
Dim i%, SW%, SH%, DX!, DY!
L = 20 'Linien
Me.ScaleMode = 3
Randomize
ReDim X1(L - 1), Y1(L - 1), X2(L - 1), Y2(L - 1), a(L - 1), b(L - 1)
With Picture1
.ScaleMode = 3
.BackColor = vbBlack
.Move 10, 10, 400, 300
Label1.Move .Left + .Width + 10, 10
.AutoRedraw = True
SW = .ScaleWidth
SH = .ScaleHeight
For i = 0 To L - 1
Do
X1(i) = Int(Rnd * SW)
Y1(i) = Int(Rnd * SH)
X2(i) = Int(Rnd * SW)
Y2(i) = Int(Rnd * SH)
DX = X2(i) - X1(i)
DY = Y2(i) - Y1(i)
Loop While DX = 0 And DY = 0
If Abs(DX) > Abs(DY) Then
a(i) = DY / DX
b(i) = Y1(i) - a(i) * X1(i)
Else
a(i) = DX / DY
b(i) = X1(i) - a(i) * Y1(i)
End If
Picture1.Line (X1(i), Y1(i))-(X2(i), Y2(i)), Rnd * vbWhite
Next i
End With
'y1=a*x1+b
'y2=a*x2+b
'b=y1-a*x1
'a=(y2-y1)/(x2-x1)
End Sub
Private Sub Form_Click()
Unload Me
End Sub
Private Sub Form_Unload(Cancel As Integer)
End
End Sub
Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As _
Single, Y As Single)
Dim i%, MausX!, MausY!, RX!, RY!, Sens%
MausX = X
MausY = Y
Sens = 5 'Genauigkeit
Label1.Caption = "Nichts"
For i = 0 To L - 1
If Abs(X2(i) - X1(i)) > Abs(Y2(i) - Y1(i)) Then
If (MausX >= X1(i) And MausX <= X2(i)) Or _
(MausX >= X2(i) And MausX <= X1(i)) Then
RY = a(i) * MausX + b(i)
If Abs(RY - MausY) < Sens Then
Label1.Caption = "Linie " & i
Exit For
End If
End If
ElseIf (MausY >= Y1(i) And MausY <= Y2(i)) Or _
(MausY >= Y2(i) And MausY <= Y1(i)) Then
RX = a(i) * MausY + b(i)
If Abs(RX - MausX) < Sens Then
Label1.Caption = "Linie " & i
Exit For
End If
End If
Next i
End Sub
Gruß
Zardoz |