1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| ' Return True if this point is on the object.
Public Overrides Function IsAt(ByVal x As Integer, ByVal y As Integer) As Boolean
Return PointNearSegment(x, y, X1, Y1, X2, Y2)
End Function
Public Function PointNearSegment(ByVal px As Integer, ByVal py As Integer, ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer, Optional ByVal close_distance As Integer = 2) As Boolean
Return DistToSegment(px, py, x1, y1, x2, y2) <= close_distance
que signifie close_distance*? pourquoi à son initialisation close_distance = 2 ?
à quoi sert close_distance*?
End Function
Private Function DistToSegment(ByVal px As Integer, ByVal py As Integer, ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer) As Double
Dim dx As Double
Dim dy As Double
Dim t As Double
dx = X2 - X1
dy = Y2 - Y1
If dx = 0 And dy = 0 Then
' It's a point not a line segment.
dx = px - X1
dy = py - Y1
DistToSegment = Sqrt(dx * dx + dy * dy)
Exit Function
End If
t = (px + py - X1 - Y1) / (dx + dy) que signifie t quel terme mathématiques*? pourquoi cette formule et à quoi servent les comparaisons ci-dessous .
If t < 0 Then
dx = px - X1
dy = py - Y1
ElseIf t > 1 Then
dx = px - X2
dy = py - Y2
Else
Dim x3 As Double = X1 + t * dx
Dim y3 As Double = Y1 + t * dy
dx = px - x3
dy = py - y3
End If
DistToSegment = Sqrt(dx * dx + dy * dy)
End Function |
Partager