'paramètres pour une droite Private Type Droite X1 As Double X2 As Double Y1 As Double Y2 As Double a As Double b As Double End Type Dim P1 As Droite Dim P2 As Droite Dim P3 As Droite Dim P4 As Droite Dim D1 As Droite Private Sub Form_Load() Call Calculs End Sub Private Sub Calculs() Dim X As Integer Dim Y As Integer Dim Lx As Integer Dim Ly As Integer 'dimensions pour PictureBox X = Picture1.Left Y = Picture1.Top Lx = Picture1.Width - 1 Ly = Picture1.Height - 1 'définitions des 4 droites: les 4 côtés de la PictureBox P1.X1 = X P1.Y1 = Y P1.X2 = X + Lx P1.Y2 = Y P1.b = Y P2.X1 = X + Lx P2.Y1 = Y P2.X2 = X + Lx P2.Y2 = Y + Ly P3.X1 = X P3.Y1 = Y + Ly P3.X2 = X + Lx P3.Y2 = Y + Ly P3.b = Y + Ly P4.X1 = X P4.Y1 = Y P4.X2 = X P4.Y2 = Y + Ly 'calcul paramètres a et b pour y = ax + b Call Equation(P2) Call Equation(P4) End Sub Private Sub Equation(d As Droite) 'calcul paramètres a et b dans y = ax + b Dim z As Double z = d.X1 - d.X2 'ici on triche pour éviter la division par zéro If z 0 Then z 0.0001 d.b = (d.X1 * d.Y2 - d.X2 * d.Y1) / z d.a = (d.Y1 - d.b) / d.X1 If d.a 0 Then d.a 0.0001 End Sub Private Function Test(d As Droite) As Boolean Test = Intersection(d, P1) If Test Then Exit Function Test = Intersection(d, P2) If Test Then Exit Function Test = Intersection(d, P3) If Test Then Exit Function Test = Intersection(d, P4) End Function Private Function Intersection(d As Droite, p As Droite) As Boolean 'calcul coordonnées x et y du point d'intersection des 2 droites Dim X As Double Dim Y As Double X = (p.b - d.b) / (d.a - p.a) Y = d.a * X + d.b Intersection = False 'teste si le point est en dehors de la Ligne If d.X1 < d.X2 Then If X < d.X1 Or X > d.X2 Then Exit Function Else If X < d.X2 Or X > d.X1 Then Exit Function End If 'teste si le point est sur le segment de droite If p.Y1 = p.Y2 Then If X < p.X1 Or X > p.X2 Then Exit Function Else If Y < p.Y1 Or Y > p.Y2 Then Exit Function End If Intersection = True End Function
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionDim bb As Boolean Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) bb = True End Sub Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) If bb = True Then Line1.X2 = X Line1.Y2 = Y D1.X1 = Line1.X1 D1.Y1 = Line1.Y1 D1.X2 = Line1.X2 D1.Y2 = Line1.Y2 Call Equation(D1) 'résultat à mettre où on veut Text1 = Test(D1) End If End Sub Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) bb = False End Sub