1 Star 0 Fork 0

mr_nobody/python_wx2048

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
polygon.py 4.25 KB
一键复制 编辑 原始数据 按行查看 历史
mr_nobody 提交于 2017-03-17 10:56 . finish polygon
import math
from abc import ABCMeta, abstractmethod
import wx
'''基类点'''
class Point(object):
def __init__(self, x, y):
self.x = x
self.y = y
def __sub__(self, other):
return Point(self.x - other.x, self.y - other.y)
def __add__(self, other):
return Point(self.x + other.x, self.y + other.y)
@property
def xy(self):
return (self.x, self.y)
def __str__(self):
return "x={0}, y={1}".format(self.x, self.y)
def __repr__(self):
return str(self.xy)
@staticmethod
def dist(a, b):
return math.sqrt((a.x - b.x)**2 + (a.y - b.y)**2)
'''基类多边形'''
class Polygon(object):
__class__ = ABCMeta
def __init__(self, points_list, **kwargs):
for point in points_list:
assert isinstance(point, Point), "input must be Point type"
self.points = points_list[:]
self.points.append(points_list[0])
self.color = kwargs.get('color', '#000000')
def drawPoints(self):
points_xy = []
for point in self.points:
points_xy.append(point.xy)
print(points_xy)
return tuple(points_xy)
@abstractmethod
def area(self):
raise('not implement')
def __lt__(self, other):
assert isinstance(other, Polygon)
return self.area < other.area
'''子类矩形'''
class RectAngle(Polygon):
def __init__(self, startPoint, w, h, **kwargs):
self._w = w
self._h = h
Polygon.__init__(self, [startPoint, startPoint + Point(w, 0), startPoint + Point(w, h), startPoint + Point(0, h)], **kwargs)
def area(self):
return self._w * self._h
'''子类三角形'''
class TriAngle(Polygon):
def __init__(self, startPoint, secondPoint, thirdPoint, **kwargs):
# 判断三个是不是在一条直线上
assert not (startPoint.x == secondPoint.x == thirdPoint.x or startPoint.y == secondPoint.y == thirdPoint.y), '三个点在一条直线上'
# 计算三角形的三边长
self._a = Point.dist(startPoint, secondPoint)
self._b = Point.dist(startPoint, thirdPoint)
self._c = Point.dist(thirdPoint, secondPoint)
self._p = (self._a + self._b + self._c)/2
Polygon.__init__(self, [startPoint, secondPoint, thirdPoint], **kwargs)
def area(self):
return (self._p * (self._p - self._a) * (self._p - self._b) * (self._p - self._c))**0.5
# return (self._p, self._a, self._b, self._c)
'''子类圆'''
class Circle(Polygon):
def __init__(self, centerPoint, r, n, point=Point, **kwargs):
self._r = r
self.CenterPoint = centerPoint
self.Point = point
# 根据中心点和边数求出圆的点的位置
# # 找出起始点
# 根据边数据算出角度
self.angles = [x * ( 360 / n ) for x in range(n)] # 根据边数将圆分成不同的角度, 用来计算点的位置
self.pointList = self.getPointList()
Polygon.__init__(self, self.pointList, **kwargs)
def getPointList(self):
pointList = []
for angle in self.angles:
x = math.cos(angle) * self._r
y = math.sin(angle) * self._r
point = self.Point(self.CenterPoint.x + x, self.CenterPoint.y + y)
pointList.append(point)
return pointList
def area(self):
return math.pi * self._r**2
class Example(wx.Frame):
def __init__(self, title, shapes):
super(Example, self).__init__(None, title = title, size = (600, 400))
self.shapes = shapes
self.Bind(wx.EVT_PAINT, self.OnPaint)
self.Centre()
self.Show()
def OnPaint(self, e):
dc = wx.PaintDC(self)
for shape in self.shapes:
dc.SetPen(wx.Pen(shape.color))
dc.DrawLines(shape.drawPoints())
if __name__ == '__main__':
prepare_draws = []
start_p = Point(50, 60)
a = RectAngle(start_p, 100, 80, color="#ff0000")
b = TriAngle(Point(200, 200), Point(200, 300), Point(300, 300), color="#000000")
c = Circle(Point(400, 150), 120, 7000, color="#1A9C09")
prepare_draws.append(a)
prepare_draws.append(b)
prepare_draws.append(c)
for shape in prepare_draws:
print(shape.area())
app = wx.App()
Example('Shapes', prepare_draws)
app.MainLoop()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/mr_nobody/python_wx2048.git
[email protected]:mr_nobody/python_wx2048.git
mr_nobody
python_wx2048
python_wx2048
master

搜索帮助