只要最短距离和对应的点就不必排序了直接比较打印结果吧: Private Type POINT x As Long y As Long End TypePrivate Sub Command1_Click() Dim p(1 To 5) As POINT Dim po As POINT Dim result As String Dim temp As Double po.x = 7 po.y = 65 p(1).x = 3 p(1).y = 60 p(2).x = 6 p(2).y = 55 p(3).x = 10 p(3).y = 60 p(4).x = 11 p(4).y = 70 p(5).x = 0 p(5).y = 0 temp = Sqr((po.x - p(1).x) ^ 2 + (po.y - p(1).y) ^ 2) result = "最近距离:" + Str(temp) + ",最近点:1号点。" For i = 2 To UBound(p) If Sqr((po.x - p(i).x) ^ 2 + (po.y - p(i).y) ^ 2) < temp Then temp = Sqr((po.x - p(i).x) ^ 2 + (po.y - p(i).y) ^ 2) result = "最近距离:" + Str(temp) + ",最近点:" + Str(i) + "号点。" ElseIf Sqr((po.x - p(i).x) ^ 2 + (po.y - p(i).y) ^ 2) = temp Then result = result + "最近距离:" + Str(temp) + ",最近点:" + Str(i) + "号点。" End If Next Debug.Print result End Sub
然后如楼上所述,排序!
Private Type POINT
x As Long
y As Long
End TypePrivate Sub Command1_Click()
Dim p(1 To 5) As POINT
Dim po As POINT
Dim result As String
Dim temp As Double
po.x = 7
po.y = 65
p(1).x = 3
p(1).y = 60
p(2).x = 6
p(2).y = 55
p(3).x = 10
p(3).y = 60
p(4).x = 11
p(4).y = 70
p(5).x = 0
p(5).y = 0
temp = Sqr((po.x - p(1).x) ^ 2 + (po.y - p(1).y) ^ 2)
result = "最近距离:" + Str(temp) + ",最近点:1号点。"
For i = 2 To UBound(p)
If Sqr((po.x - p(i).x) ^ 2 + (po.y - p(i).y) ^ 2) < temp Then
temp = Sqr((po.x - p(i).x) ^ 2 + (po.y - p(i).y) ^ 2)
result = "最近距离:" + Str(temp) + ",最近点:" + Str(i) + "号点。"
ElseIf Sqr((po.x - p(i).x) ^ 2 + (po.y - p(i).y) ^ 2) = temp Then
result = result + "最近距离:" + Str(temp) + ",最近点:" + Str(i) + "号点。"
End If
Next
Debug.Print result
End Sub