0

最近接ペア アルゴリズムが実装されています。ランダム サイズのランダム ポイントのリスト内のどのポイントが最も近いかを把握しようとしています。問題のポイントが得られず、再び距離を返すか、何も得られないかのどちらかです。私はどんな批判にもオープンですが、理論的には距離が新しく低く、温度がこれを示している場合、ポイントのペアを変数として追加または設定できるため、これらの問題が発生する理由について混乱していますまたはリスト内。

import math
from math import*
from random import *
from graphics import*

def ClosestPair(P,N):
    #Closest Pair of Points are compared, and the distance between the two pairs
    #is output as a number.
    #
    ret = []
    d = math.inf
    for i in range(N):
        for j in range(i+1,N):
            temp = -1
            d = min(d,sqrt((((P[i].getX() - P[j].getX())**2) + ((P[i].getY() - P[j].getY())**2))))
            if temp < d:
                temp = d
            else:
                ret.append(P[i])
                ret.append(P[j])
    return ret

def main():
    #X&Y is lists that get filled with N values of N size
    #X&Y are appended as the X's and Y's of a Point to Array.
    #Then If ClosestPair = 0 prints closest pair saying points are 
    #overlapping
    #Else prints the Distance and Pair of Points that are Closest
    x = []
    y = []
    array = []

    print('Enter A Size for N')
    n = int(input('N = '))
    for a in range(n):
        x.append(randrange(n))
        y.append(randrange(n))
        array.append(Point(x[a],y[a]))
    #print(array)

    if ClosestPair(array,n) == 0:
        print("The Closest Pair of Points are On top of One another!")
    else:
        print("The Distance between the Closest Points is: ", ClosestPair(array,n), "Points Away")

main()
4

1 に答える 1

0

内側のループが間違っています。次のようになります。

   temp = min(d,sqrt((((P[i].getX() - P[j].getX())**2) + ((P[i].getY() - P[j].getY())**2))))
   if temp < d:
       temp = d
       ret = [P[i], P[j]]

また、関数はペアを返します。0 と比較しても意味がありません。必要に応じて距離とポイントを返すか、距離を再計算します。

于 2018-01-23T12:28:56.240 に答える