Answer the question
In order to leave comments, you need to log in
How to solve the rectangle problem with Python?
Given two rectangles whose sides are parallel or perpendicular
to the coordinate axes. The coordinates of the lower left corner of each of
them and the lengths of their sides are known. One of the rectangles will be called the first, the other -
the second.
a) Determine whether all points of the first rectangle belong to the second.
b) Determine whether all points of one of the rectangles belong to the other.
c)* Determine if these rectangles intersect.
can't decide sub-points
x1 = int(input("x1 = "))
y1 = x1
x2 = int(input("x2 = "))
y2 = x2
a1 = int(input("dlina = "))
b1 = a1
a2 = int(input("dlina_2 = "))
b2 = a2
if ((x1<=x2 and x2<=x1+a1) and (y1<=y2 and y2<=y1+b1) ):
print("Пересекаются")
else:
print("не пересекаются")
Answer the question
In order to leave comments, you need to log in
class Rectangle(object):
def __init__(self, x, y, w, h):
self.x1 = x
self.x2 = x + w
self.y1 = y
self.y2 = y + h
def is_involved(self, other):
if all([other.x1 <= self.x1,
other.y1 <= self.y1,
other.x2 >= self.x2,
other.y2 >= self.y2]):
return True
return False
def is_intersected(self, other):
if any([all([other.x1 <= self.x1 <= other.x2, other.y1 <= self.y1 <= other.y2]),
all([other.x1 <= self.x2 <= other.x2, other.y1 <= self.y1 <= other.y2]),
all([other.x1 <= self.x1 <= other.x2, other.y1 <= self.y2 <= other.y2]),
all([other.x1 <= self.x2 <= other.x2, other.y1 <= self.y2 <= other.y2])]):
return True
return False
print("Rectangle 1:")
rect1 = Rectangle(int(input("x = ")), int(input("y = ")), int(input("width = ")), int(input("height = ")))
print()
print("Rectangle 2:")
rect2 = Rectangle(int(input("x = ")), int(input("y = ")), int(input("width = ")), int(input("height = ")))
print()
print('а) Принадлежат ли все точки первого прямоугольника второму:')
print(rect2.is_involved(rect1))
print('б) Принадлежат ли все точки одного из прямоугольников другому:')
print(rect2.is_involved(rect1) or rect1.is_involved(rect2))
print('в)* Пересекаются ли эти прямоугольники:')
print(rect2.is_intersected(rect1) or rect1.is_intersected(rect2))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question