46 lines
1.1 KiB
Plaintext
46 lines
1.1 KiB
Plaintext
' Closest Pair Problem
|
|
s="0.654682,0.925557,0.409382,0.619391,0.891663,0.888594,0.716629,0.996200,0.477721,0.946355,0.925092,0.818220,0.624291,0.142924,0.211332,0.221507,0.293786,0.691701,0.839186,0.728260,"
|
|
i=0
|
|
While s<>""
|
|
i=i+1
|
|
For j=1 To 2
|
|
k=Text.GetIndexOf(s,",")
|
|
ss=Text.GetSubText(s,1,k-1)
|
|
s=Text.GetSubTextToEnd(s,k+1)
|
|
pxy[i][j]=ss
|
|
EndFor
|
|
EndWhile
|
|
n=i
|
|
i=1
|
|
j=2
|
|
dd=Math.Power(pxy[i][1]-pxy[j][1],2)+Math.Power(pxy[i][2]-pxy[j][2],2)
|
|
ddmin=dd
|
|
ii=i
|
|
jj=j
|
|
For i=1 To n
|
|
For j=1 To n
|
|
dd=Math.Power(pxy[i][1]-pxy[j][1],2)+Math.Power(pxy[i][2]-pxy[j][2],2)
|
|
If dd>0 Then
|
|
If dd<ddmin Then
|
|
ddmin=dd
|
|
ii=i
|
|
jj=j
|
|
EndIf
|
|
EndIf
|
|
EndFor
|
|
EndFor
|
|
sqrt1=ddmin
|
|
sqrt2=ddmin/2
|
|
For i=1 To 20
|
|
If sqrt1=sqrt2 Then
|
|
Goto exitfor
|
|
EndIf
|
|
sqrt1=sqrt2
|
|
sqrt2=(sqrt1+(ddmin/sqrt1))/2
|
|
EndFor
|
|
exitfor:
|
|
TextWindow.WriteLine("the minimum distance "+sqrt2)
|
|
TextWindow.WriteLine("is between the points:")
|
|
TextWindow.WriteLine(" ["+pxy[ii][1]+","+pxy[ii][2]+"] and")
|
|
TextWindow.WriteLine(" ["+pxy[jj][1]+","+pxy[jj][2]+"]")
|