在创建的点的随机列表中在两个点之间创建最小距离。创建我的距离值列表时遇到问题

时间:2019-10-07 17:48:40

标签: python-3.x

这是大型Point中的函数。这个问题的目的是创建一个空的距离值列表(已经创建了def calc_distance),然后遍历创建的随机坐标值列表,并找到最小距离值及其在随机列表中的索引坐标值。预先谢谢你!

import random

class Point:
    def __init__(self, x, y, z=0.0):
        self.x = x
        self.y = y
        self.z = z
        self.d = 2
        if self.z != 0:
            self.d = 3

    def print_coordinate(self):
        print("The coordinates are ({}, {}, {})".format(self.x, self.y, self.z))

    def calc_distance(self, pt):
        isinstance((x, y, z, pt.x, pt.y, pt.z), float)
        return ((((pt.x - self.x) ** 2 + (pt.y - self.y) ** 2) + (pt.z - self.z) ** 2) ** 0.5)


#d1 = Point(4.12, -234.4, 34.1)
#d2 = Point(-63.2, 34.6, -23.1)
#d1.print_coordinate()
#d2.print_coordinate()
#d1.calc_distance(d2)


    def __repr__(self):
        return f'({str(self.x)}, {str(self.y)})'

#Q6

    def float_N(self, num_point, dimension, lower_bound, upper_bound):
        points = []

        for _ in range(num_point):
            if dimension == '2-D':
                point = Point(random.uniform(lower_bound, upper_bound), random.uniform(lower_bound, upper_bound))
                points.append(point)
            else:
                pass
                # if 3-D coordinates
        return points

#print(float_N(100, '2-D', 0, 100))

# Q7 generate 100 Point class instances

#a = Point(0, 0)
#list1 = a.float_N(100, '2-D', 0, 100)
#print(list1) #= 100 Point class instances

#Q8 closest pt from list to (50, 50)

    def closest_pt(self):

        dvalues = []

        list1 = a.float_N(100, '2-D', 0, 100)
        min = (((pt.x - self.x) ** 2 + (pt.y - self.y) ** 2) ** 0.5)

        for _ in range(len(list1)):
            dvalue = (((pt.x - self.x) ** 2 + (pt.y - self.y) ** 2) ** 0.5)
            # or dvalue = Point.calc_distance(self, pt)
            return dvalue
        dvalues.append(dvalue)
        try:
            if dvalue < min:
                min = dvalue
        except NameError:
            min = dvalue



a = Point(0, 0)
b = Point(50, 50)
print(a.float_N(100, '2-D', 0, 100)) #= 100 Point class instances ---> Q7 <--- (list1)
list1 = a.float_N(100, '2-D', 0, 100)
print("Minimum Point is ", min(list1, key=dvalues))
return min((a.float_N(100, '2-D', 0, 100)), key=dvalues)
print(b.closest_pt(dvalue))

我不断得到dvalue的定义。 我在检索距离值列表时遇到困难。我可以获取随机坐标值,但不能获取它们在点(50,50)之间的距离值,因此无法进一步工作并为最小距离值编制索引。

0 个答案:

没有答案