python定义类属性,对象属性,实现构造方法和析构方法,完成类属性和对象属性的设置
1条回答 默认 最新
关注 比较麻烦的是怎么判断四边形的形状,如果与坐标不平行,就得计算出所有边长以及角度再比较了
from pygame import Rect class Rectangle(): num = 0 def __init__(self, topleft, topright, bottomright, bottomleft): self.tl = topleft self.tr = topright self.br = bottomright self.bl = bottomleft Rectangle.num += 1 def gettype(self): top = (self.tr[1]-self.tl[1])**2 + (self.tr[0]-self.tl[0])**2 right = (self.tr[1]-self.br[1])**2 + (self.tr[0]-self.br[0])**2 bottom = (self.br[1]-self.bl[1])**2 + (self.br[0]-self.bl[0])**2 left = (self.tl[1]-self.bl[1])**2 + (self.tl[0]-self.bl[0])**2 top_bottom = (self.tr[1]-self.tl[1])*(self.br[0]-self.bl[0])==(self.br[1]-self.bl[1])*(self.tr[0]-self.tl[0]) left_right = (self.tr[1]-self.br[1])*(self.tl[0]-self.bl[0])==(self.tr[0]-self.br[0])*(self.tl[1]-self.bl[1]) parallel = top_bottom+left_right if parallel == 2: diagonal = (self.tr[1]-self.bl[1])**2 + (self.tr[0]-self.bl[0])**2 if bottom + right == diagonal: if top == left: print('正方形') else: print('长方形') else: print('平行四边形') elif parallel == 1: print('梯形') else: print('其他') a = Rectangle((0,0),(0,2),(4,2),(4,0)) a.gettype()
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报