-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_line.py
101 lines (70 loc) · 1.98 KB
/
test_line.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import wx
import math
from abc import ABCMeta,abstractmethod
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):
__metaclass__ = 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 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")
prepare_draws.append(a)
for shape in prepare_draws:
print shape.area()
app = wx.App()
Example('Shapes', prepare_draws)
app.MainLoop()