69 lines
1.8 KiB
Python
69 lines
1.8 KiB
Python
from Point import Point
|
|
|
|
|
|
class Rectangle:
|
|
|
|
def __init__(self, p1, p2):
|
|
self.p1 = Point(10, 20)
|
|
self.p2 = Point(20, 10)
|
|
|
|
if type(p1) is Point:
|
|
self.p1 = p1
|
|
|
|
if type(p2) is Point:
|
|
self.p2 = p2
|
|
|
|
def __eq__(self,other):
|
|
if other is Rectangle:
|
|
return self.p1 == other.p1 and self.p2 == other.p2
|
|
return False
|
|
|
|
def __ne__(self, other):
|
|
return not self.__eq__(other)
|
|
|
|
|
|
|
|
|
|
def a(self):
|
|
# return abs(self.p1.x - self.p2.x)
|
|
if self.p1.x > self.p2.x:
|
|
return self.p1.x - self.p2.x
|
|
else:
|
|
return self.p2.x - self.p1.x
|
|
|
|
def b(self):
|
|
# return abs(self.p1.y - self.p2.y)
|
|
if self.p1.y > self.p2.y:
|
|
return self.p1.y - self.p2.y
|
|
else:
|
|
return self.p2.y - self.p1.y
|
|
|
|
def perimeter(self):
|
|
return 2 * (self.a() + self.b())
|
|
|
|
def area(self):
|
|
return self.a() * self.b()
|
|
|
|
def intersection(self, other):
|
|
if self.p1.x <= other.p2.x <= self.p2.x and self.p2.y <= other.p1.y <= self.p1.y:
|
|
return True
|
|
elif self.p1.x <= other.p1.x <= self.p2.x and self.p2.y <= other.p1.y <= self.p1.y:
|
|
return True
|
|
elif other.p1.x <= self.p1.x <= other.p2.x and other.p2.y <= self.p1.y <= other.p1.y:
|
|
return True
|
|
elif other.p1.x <= self.p2.x <= other.p2.x and other.p2.y <= self.p1.y <= other.p1.y:
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
def merge(self, other):
|
|
if type(other) is Rectangle:
|
|
a = min(self.p1.x, other.p1.x)
|
|
b = max(self.p1.y, other.p1.y)
|
|
c = max(self.p2.x, other.p2.x)
|
|
d = min(self.p2.y, other.p2.y)
|
|
|
|
return Rectangle(Point(a, b), Point(c, d))
|
|
else:
|
|
return self
|