Python 用类形成多边形
所以,我的问题是:我正在尝试创建一个程序,它将创建一个至少有3个点(由坐标x和y组成)或角度的多边形。我想,如果提交的点或角度少于3个,程序将返回一个错误,说明点数不足。我需要用类来创建这个 到目前为止,我已经创建了以下内容:`Python 用类形成多边形,python,python-3.x,polygon,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 =
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:
`
类多边形:
点数=0
角度数=0
定义初始(自身、坐标x、坐标y、角度):
s=[]
self.coordinate\u x=坐标
self.coordinate\u y=坐标
自我角度=角度
self.s=s.append([坐标x,坐标y])
Polygon.number_points=Polygon.number_points+1
Nkotnik.number_angles=多边形.number_angles+1
#这里我想让程序检查是否有足够的分数
#和角度形成一个多边形,并检查所有坐标是否正确
#数字。如果不满足此要求,程序将打印
#错误消息。
def创建(自我):
如果不存在(坐标_x,(int,float)):
#引发异常(“这不是一个数字”)
如果Polygon.number_points我在这里看到一个错误:
Polygon.number_points = Polygon.number_points + 1
Nkotnik.number_angles = Polygon.number_angles + 1
Nkotnik
应该是Polygon
。此外,为了使其更短,您可以执行Polygon.number\u points+=1
和number\u angles
的操作
现在,创建程序:
def creation(self):
这是个糟糕的设计。该函数应将点的数量和角度的数量作为参数。因此,请这样做:
def creation(self, points, angles):
但是创建
基本上是初始化
,因此您应该将其集成到初始化中
另外,您的\uuuuu init\uuuuu
很奇怪<代码>数字点
和数字角度
应在中定义,而不是在对象体中定义,因为这些变量对于不同的多边形
对象是不同的。因此,修改后的代码如下所示:
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)
类多边形:
定义初始(自身、坐标列表、角度):
如果len(协调列表)//2<3:
raise异常(“侧面计数必须为3或更多。”)
s=[]
self.number_points=0
self.number_角度=0
self.coordinates\u x=坐标列表[::2]
self.coordinates_y=coord_list[1::2]
自我角度=角度
self.s=s.append([坐标x,坐标y])
self.number\u points+=len(坐标列表//2)
self.number_angles+=len(角度)
num_sides=int(输入('Number of sides:'))#如果使用的是Python 2,则为原始输入
点数=[]
角度=[]
对于范围内的i(数侧):
points.append(int(输入('X点的值:'))
points.append(int(输入('Y点的值:'))
对于范围内的i(数侧):
angles.append(int(输入('角度值:'))
多边形\对象=多边形(点、角度)
你完了 您可以在类中的创建时进行检查,就像这样,您还需要更多的角度来定义点
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
谢谢你的贡献!最后一行(多边形\对象=多边形(点、角度))做什么D@Jerfislav这将创建一个多边形
对象,并将其分配给一个变量,以便您以后可以访问它。
Polygon(PointCartesian(1,2),PointCartesian(4,7),PointPolar(5,28.2))
Polygon(*list_of_points)