-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiguresclass.py
More file actions
229 lines (144 loc) · 5.18 KB
/
figuresclass.py
File metadata and controls
229 lines (144 loc) · 5.18 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
"""
Author: Danny Krizanc
Date: Oct 2106
This file contains a skeleton
implementation of geometric
figures including circles, triangles, rectangles and squares. Methods
available for each include those for computing the perimeter and area.
They all inherit from the class Figure which has one method for comparing figures
by their area.
The Point class is provided to be used by the other classes.
"""
from math import sqrt,pi
class Point(object):
'''Class Point implements a point on the Cartesian plane (x,y) using
two instance variables x and y indicating their co-ordinates'''
def __init__(self,x=0,y=0):
'''Constructor for Point defaults to (0,0)'''
self.x = x
self.y = y
def __str__(self):
'''To string method for printing of a Point as a tuple'''
return '('+str(self.x)+','+str(self.y)+')'
def __repr__(self):
'''Representation of Point'''
return 'Point('+str(self.x)+','+str(self.y)+')'
def getx(self):
return self.x
def gety(self):
return self.y
def setx(self,x):
self.x = x
def sety(self,y):
self.y = y
def distance_to(self,point):
'''Computes the distance between self and point'''
return sqrt((self.x-point.x)**2+(self.y-point.y)**2)
class Figure(object):
'''Super class Figure of Circle, Rectangle, Square. Implements
__cmp__(self,other) ordering by area.'''
def __cmp__(self,other):
'''Returns comparison of self area and other area'''
if self.area()>other.area():
return 1
elif self.area()==other.area():
return 0
else:
return -1
class Circle(Figure):
'''Class Circle implements a circle given its center, a Point, and its
radius, a float or int. Subclass of Figure.'''
def __init__(self,p,r):
'''Constructor for Circle takes a Point p and radius r'''
self.center = p
self.radius = r
def __str__(self):
'''To string method for printing a circle'''
return 'Circle with center'+str(self.center)+' and radius '+str(self.radius)+''
def __repr__(self):
'''Representation of circle'''
return 'Circle('+repr(self.center)+','+repr(self.radius)+')'
def set_radius(self,r):
self.radius = r
def set_center(self,p):
self.center = p
def get_radius(self):
return self.radius
def get_center(self):
return self.center
def diameter(self):
'''Returns the diameter of circle'''
return 2*self.radius
def perimeter(self):
'''Returns the perimeter of circle'''
return 2*pi*self.radius
def area(self):
'''Returns the area of circle'''
return pi*self.radius**2
class Triangle(Figure):
'''Class Triangle implements a triangle represented by its three corners.
Subclass of Figure.'''
def __init__(self,p1,p2,p3):
'''Constructor for a Triangle'''
self.p1 = p1
self.p2 = p2
self.p3 = p3
def __str__(self):
'''To string method for Triangle'''
return 'Triangle with corners '+str(self.p1)+','+str(self.p2)+','+str(self.p3)+''
def __repr__(self):
'''Representation of circle'''
return 'Triangle('+repr(self.p1)+','+repr(self.p2)+','+repr(self.p3)+')'
def perimeter(self):
'''Returns the perimeter of triangle'''
return (self.p1.distance_to(self.p2)+
self.p2.distance_to(self.p3)+
self.p3.distance_to(self.p1))
def area(self):
'''Returns the area of triangle using Heron's formula'''
s1 = self.p1.distance_to(self.p2)
s2 = self.p2.distance_to(self.p3)
s3 = self.p3.distance_to(self.p1)
s = (s1+s2+s3)/2.0
return sqrt(s*(s-s1)*(s-s2)*(s-s3))
class Rectangle(Figure):
def __init__(self,l,h,x=0,y=0):
self.o = (x,y)
self.h = h
self.l = l
self.tl = (x,y+h)
self.tr = (x+l,y+h)
self.br = (x+l,y)
def __str__(self):
return 'Rectangle with vertices '+str(self.o)+','+str(self.tl)+','+str(self.tr)+','+str(self.br)+''
def __repr__(self):
return 'Rectangle('+repr(self.l)+','+repr(self.h)+','+repr(self.o[0])+','+repr(self.o[1])+')'
## def perimeter(self):
## '''Returns the perimeter of triangle'''
##
## return (self.p1.distance_to(self.p2)+
## self.p2.distance_to(self.p3)+
## self.p3.distance_to(self.p1))
##
## def area(self):
## '''Returns the area of triangle using Heron's formula'''
##
## s1 = self.p1.distance_to(self.p2)
## s2 = self.p2.distance_to(self.p3)
## s3 = self.p3.distance_to(self.p1)
## s = (s1+s2+s3)/2.0
## return sqrt(s*(s-s1)*(s-s2)*(s-s3))
'''Create some figures and sort by area'''
x = Point()
y = Point(3,0)
z = Point(0,4)
t1 = Triangle(x,y,z)
y = Point(8,0)
z = Point(4,3)
t2 = Triangle(x,y,z)
c1 = Circle(x,sqrt(2.0))
c2 = Circle(y,5)
L = [c1,c2,t2,t1]
print L
L.sort()
print L