python_exercises/thinkcspy/Chapter-17-18-Exercises/rectangle.py

90 lines
2.3 KiB
Python
Raw Normal View History

2024-02-19 13:11:26 -07:00
# Runestone.Academy thinkcspy course
# Chapter 18
# Problem 7 - 14
from points import Point
2024-02-16 16:56:28 -07:00
class Rectangle:
def __init__(self, pos, width, height):
self.pos = pos
if width < 0:
width = 0
if height < 0:
height = 0
2024-02-16 16:56:28 -07:00
self.width = width
self.height = height
self.lowerX = self.pos.getX()
self.upperX = self.lowerX + self.width
self.lowerY = self.pos.getY()
self.upperY = self.lowerY + self.height
2024-02-16 16:56:28 -07:00
def getWidth(self):
return self.width
def getHeight(self):
return self.height
def getCoords(self):
return [self.lowerX, self.lowerY, self.upperX, self.upperY]
2024-02-16 16:56:28 -07:00
def __str__(self):
return "Lower Left Coordinates: {}; Width: {}; Height: {}".format(self.pos, self.width, self.height)
def area(self):
return self.width * self.height
def perimeter(self):
return self.width * 2 + self.height * 2
def transpose(self):
width = self.width
self.width = self.height
self.height = width
return self
2024-02-16 18:11:57 -07:00
def contains(self, pnt):
pntX = pnt.getX()
pntY = pnt.getY()
if pntX > self.lowerX and pntX < self.upperX and pntY > self.lowerY and pntY < self.upperY:
2024-02-16 18:11:57 -07:00
return True
2024-02-16 18:11:57 -07:00
return False
2024-02-16 18:31:28 -07:00
def diagonal(self):
pnt = Point(self.width, self.height)
return pnt.distanceFromOrigin()
def collission(r1, r2):
conditionW = False
conditionH = False
2024-02-16 22:44:39 -07:00
r2Arr = r2.getCoords()
r1Arr = r1.getCoords()
# Compare widths and heights
2024-02-16 22:44:39 -07:00
if r2.width > r1.width:
if r1Arr[0] in range(r2Arr[0], r2Arr[2] + 1) or r1Arr[2] in range(r2Arr[0], r2Arr[2] + 1):
conditionW = True
2024-02-16 22:44:39 -07:00
elif r2Arr[0] in range(r1Arr[0], r1Arr[2] + 1) or r2Arr[2] in range(r1Arr[0], r1Arr[2] + 1):
conditionW = True
2024-02-16 22:44:39 -07:00
if r2.height > r1.height:
if r1Arr[1] in range(r2Arr[1], r2Arr[3] + 1) or r1Arr[3] in range(r2Arr[1], r2Arr[3] + 1):
conditionH = True
2024-02-16 22:44:39 -07:00
elif r2Arr[1] in range(r1Arr[1], r1Arr[3] + 1) or r2Arr[3] in range(r1Arr[1], r1Arr[3] + 1):
conditionH = True
if conditionW and conditionH:
return "Collission!"
return "All safe for now."
2024-02-16 22:44:39 -07:00
r = Rectangle(Point(-23, 25), 100, 50)
s = Rectangle(Point(10, 27), 10, 4)
2024-02-16 16:56:28 -07:00
print(collission(r, s))