用类形成多边形

时间:2015-12-09 23:24:09

标签: python python-3.x polygon

所以,我的问题是:我正在尝试创建一个程序,它将创建一个具有至少3个点(由坐标x和y组成)或角度的多边形。我想,如果提交的点数或角度少于3个,程序会返回一个错误,表示点数不足。我需要用类创建它。

到目前为止我创造了这个:`

class Polygon:

    number_points = 0
    number_angles = 0



    def __init__(self, coordinate_x, coordinate_y, angles):
        s = []
        self.coordinate_x = coordinate_x
        self.coordinate_y = coordinate_y
        self.angles = angles
        self.s = s.append([coordinate_x, coordinate_y])
        Polygon.number_points = Polygon.number_points + 1
        Nkotnik.number_angles = Polygon.number_angles + 1


    # Here i would like the program to check if there are enough points 
    # and angles to form a polygon and to check if all coordinates are 
    # numbers. If this requirement is not met, the program prints an 
    # error message.
    def creation(self):
        if not isinstance(coordinate_x, (int,float)):
            #raise Exception("That is not a number")
        if Polygon.number_points <= 3:

        `

我的想法是将坐标存储在列表中,然后当用户输入足够的点时,就可以形成多边形。

我不是母语人士,所以如果我需要进一步澄清事情,请随时问:)谢谢你的任何可能的答案:)

2 个答案:

答案 0 :(得分:0)

我在这里看到一个错误:

Polygon.number_points = Polygon.number_points + 1
Nkotnik.number_angles = Polygon.number_angles + 1

Nkotnik应为Polygon。另外,为了缩短它,您可以对Polygon.number_points += 1执行number_anglesdef creation(self):

所以现在,该计划的创建:

def creation(self, points, angles):

这是糟糕的设计。该函数应将点数和角度数作为参数。所以,这样做:

creation

initialization基本上是__init__,因此您应该将其整合到__init__

此外,您的number_points很奇怪。 number_angles__init__应该在Polygon中定义,而不是在对象主体中定义,因为这些变量对于不同的class Polygon: def __init__(self, coord_list, angles): if len(coord_list) // 2 < 3: raise Exception("Side count must be 3 or more.") s = [] self.number_points = 0 self.number_angles = 0 self.coordinates_x = coord_list[::2] self.coordinates_y = coord_list[1::2] self.angles = angles self.s = s.append([coordinate_x, coordinate_y]) self.number_points += len(coord_list // 2) self.number_angles += len(angles) num_sides = int(input('Number of sides: ')) #raw_input if you're using Python 2 points = [] angles = [] for i in range(num_sides): points.append(int(input('X value of point: '))) points.append(int(input('Y value of point: '))) for i in range(num_sides): angles.append(int(input('Angle value: '))) polygon_object = Polygon(points, angles) 对象是不同的。因此,修改后,您的代码如下所示:

{{1}}

你已经完成了!

答案 1 :(得分:0)

您可以在课程创建时进行检查,就像这样,您还需要更多的角度来定义一个点

import collections

PointCartesian = collections.namedtuple("PointCartesian","coordinate_x coordinate_y")
PointPolar = collections.namedtuple("PointPolar","magnitude angle")
#this is a easy way to make a class for points, that I recommend have
#a class too  

class Polygon(object):
    def __init__(self,*argv,**kargv):
        points = list()
        for elem in argv:
            if isinstance(elem,(PointCartesian,PointPolar ) ):
                points.append(elem)
            else:
                raise ValueError("Element "+str(elem)+" of wrong type")
        if len(points) <3:
            raise ValueError("Insufficient data")
        self.points = points

在其他地方,您有例程要求用户提供数据,您可以检查每个输入或将其留给班级。

称之为做这样的事情

Polygon(PointCartesian(1,2),PointCartesian(4,7),PointPolar(5,28.2))
Polygon(*list_of_points)