Given two rectangles, find if the given two rectangles overlap or not. A rectangle is denoted by providing the x and y coordinates of two points: the left top corner and the right bottom corner of the rectangle. Two rectangles sharing a side are considered overlapping. (L1 and R1 are the extreme points of the first rectangle and L2 and R2 are the extreme points of the second rectangle).
Note: It may be assumed that the rectangles are parallel to the coordinate axis.
Example 1:
Input:
L1=(0,10)
R1=(10,0)
L2=(5,5)
R2=(15,0)
Output:
1
Explanation:
The rectangles overlap.
Example 2:
Input:
L1=(0,2)
R1=(1,1)
L2=(-2,0)
R2=(0,-3)
Output:
0
Explanation:
The rectangles do not overlap.
Your Task:
You don't need to read input or print anything. Your task is to complete the function doOverlap() which takes the points L1, R1, L2, and R2 as input parameters and returns 1 if the rectangles overlap. Otherwise, it returns 0.
Expected Time Complexity:O(1)
Expected Auxillary Space:O(1)
Constraints:
-10^{9}<=x-coordinate,y-coordinate<=10^{9}

Source: geeksforgeeks | Difficulty: EASY
Tags: Geometry

Mathematics

Starter code: #User function Template for python3

class Solution:
    
    def doOverlap(self, L1, R1, L2, R2):
        #code here

Solutions:
class Solution:

	def doOverlap(self, l1, r1, l2, r2):
		r1tl = l1
		r1tr = [r1[0], l1[1]]
		r1bl = [l1[0], r1[1]]
		r1br = r1
		r2tl = l2
		r2tr = [r2[0], l2[1]]
		r2bl = [l2[0], r2[1]]
		r2br = r2
		if l2[0] > r1[0] or r1[1] > l2[1]:
			return 0
		if r2[1] > l1[1] or l1[0] > r2[0]:
			return 0
		return 1


class Solution:

	def doOverlap(self, L1, R1, L2, R2):
		if R1[1] > L2[1] or R2[1] > L1[1]:
			return 0
		elif R1[0] < L2[0] or R2[0] < L1[0]:
			return 0
		return 1


class Solution:

	def doOverlap(self, L1, R1, L2, R2):
		if L1[0] <= R2[0] and L2[0] <= R1[0]:
			if L1[1] >= R2[1] and L2[1] >= R1[1]:
				return 1
		return 0


class Solution:

	def doOverlap(self, l1, r1, l2, r2):
		if l1[0] > r2[0] or r1[0] < l2[0] or l1[1] < r2[1] or (r1[1] > l2[1]):
			return 0
		return 1


class Solution:

	def doOverlap(self, L1, R1, L2, R2):
		if L1[0] > L2[0]:
			(L1, R1, L2, R2) = (L2, R2, L1, R1)
		a1 = (L1[0], R1[1])
		b1 = R1
		c1 = (R1[0], L1[1])
		d1 = L1
		a2 = (L2[0], R2[1])
		b2 = R2
		c2 = (R2[0], L2[1])
		d2 = L2
		if b1[0] < a2[0] or d1[1] < a2[1] or d2[1] < a1[1]:
			return 0
		return 1


class Solution:

	def doOverlap(self, L1, R1, L2, R2):
		if L1[0] > L2[0]:
			(L1, R1, L2, R2) = (L2, R2, L1, R1)
		A1LT = L1
		A1RB = R1
		A1LB = [L1[0], R1[1]]
		A1RT = [R1[0], L1[1]]
		A2LT = L2
		A2RB = R2
		A2LB = [L2[0], R2[1]]
		A2RT = [R2[0], L2[1]]
		if A1RT[0] < A2LT[0] or A1RT[1] < A2LB[1] or A1RB[1] > A2RT[1]:
			return 0
		return 1


class Solution:

	def doOverlap(self, L1, R1, L2, R2):
		if max(L1[0], R1[0]) < min(L2[0], R2[0]) or min(L1[0], R1[0]) > max(L2[0], R2[0]) or min(L1[1], R1[1]) > max(L2[1], R2[1]) or (max(L1[1], R1[1]) < min(L2[1], R2[1])):
			return 0
		return 1


class Solution:

	def doOverlap(self, L1, R1, L2, R2):
		vertical_range = max(L1[1], L2[1]) - min(R1[1], R2[1])
		condition1 = L1[1] - R1[1] + L2[1] - R2[1] < vertical_range
		horizontal_range = max(R1[0], R2[0]) - min(L1[0], L2[0])
		condition2 = R1[0] - L1[0] + R2[0] - L2[0] < horizontal_range
		if condition1 or condition2:
			return 0
		elif L1[1] - R1[1] + L2[1] - R2[1] == vertical_range and R1[0] - L1[0] + R2[0] - L2[0] == horizontal_range:
			return 0
		else:
			return 1


class Solution:

	def doOverlap(self, L1, R1, L2, R2):
		a1 = L1[0]
		b1 = L1[1]
		a2 = R1[0]
		b2 = R1[1]
		x1 = L2[0]
		y1 = L2[1]
		x2 = R2[0]
		y2 = R2[1]
		if b1 < y2 or a2 < x1 or x2 < a1 or (y1 < b2):
			return 0
		return 1


class Solution:

	def doOverlap(self, l1, R1, l2, R2):
		if l1[0] > R2[0] or l2[0] > R1[0]:
			return 0
		if l1[1] < R2[1] or l2[1] < R1[1]:
			return 0
		return 1


class Solution:

	def doOverlap(self, L1, R1, L2, R2):
		(l1x, l1y) = (L1[0], L1[1])
		(l2x, l2y) = (L2[0], L2[1])
		(r1x, r1y) = (R1[0], R1[1])
		(r2x, r2y) = (R2[0], R2[1])
		w = min(r1x, r2x) - max(l1x, l2x)
		h = min(l1y, l2y) - max(r1y, r2y)
		if w >= 0 and h >= 0:
			return 1
		else:
			return 0


class Solution:

	def doOverlap(self, L1, R1, L2, R2):
		A1 = (L1[0], R1[1])
		B1 = R1
		C1 = (R1[0], L1[1])
		D1 = L1
		A2 = (L2[0], R2[1])
		B2 = R2
		C2 = (R2[0], L2[1])
		D2 = L2
		check1 = False
		check2 = False
		for x in range(A1[0], B1[0] + 1):
			if x >= A2[0] and x <= B2[0]:
				check1 = True
		for y in range(B1[1], D1[1] + 1):
			if y >= B2[1] and y <= D2[1]:
				check2 = True
		if check1 and check2:
			return 1
		else:
			return 0


class Solution:

	def doOverlap(self, L1, R1, L2, R2):
		(r1x1, r1y1) = L1
		(r1x2, r1y2) = R1
		(r2x1, r2y1) = L2
		(r2x2, r2y2) = R2
		o1 = r1x1 <= r2x1 <= r1x2 or r1x1 <= r2x2 <= r1x2 or (r1x1 >= r2x1 and r1x2 <= r2x2)
		o2 = r1y1 >= r2y1 >= r1y2 or r1y1 >= r2y2 >= r1y2 or (r1y1 <= r2y1 and r1y2 >= r2y2)
		return o1 * o2


class Solution:

	def doOverlap(self, L1, R1, L2, R2):
		(x1, y1, x2, y2, p1, q1, p2, q2) = (L1[0], L1[1], R1[0], R1[1], L2[0], L2[1], R2[0], R2[1])
		if q2 > y1 or p1 > x2 or y2 > q1 or (x1 > p2):
			return 0
		else:
			return 1


class Solution:

	def doOverlap(self, L1, R1, L2, R2):
		mid1_x = (R1[0] - L1[0]) / 2
		mid1_y = (L1[1] - R1[1]) / 2
		mid2_x = (R2[0] - L2[0]) / 2
		mid2_y = (L2[1] - R2[1]) / 2
		max_x = mid1_x + mid2_x
		max_y = mid1_y + mid2_y
		mid1_act_x = L1[0] + mid1_x
		mid1_act_y = R1[1] + mid1_y
		mid2_act_x = L2[0] + mid2_x
		mid2_act_y = R2[1] + mid2_y
		if abs(mid2_act_x - mid1_act_x) > max_x or abs(mid2_act_y - mid1_act_y) > max_y:
			return 0
		else:
			return 1


class Solution:

	def doOverlap(self, L1, R1, L2, R2):

		def doSingleDimentionalOverlap(L1, P1, L2, P2):
			return L1 >= L2 >= P1 or L1 >= P2 >= P1 or L2 >= L1 >= P1 >= P2
		(P1, P2) = ((L1[0], R1[1]), (L2[0], R2[1]))
		return 1 if doSingleDimentionalOverlap(L1[1], P1[1], L2[1], P2[1]) and doSingleDimentionalOverlap(R1[0], P1[0], R2[0], P2[0]) else 0


class Solution:

	def doOverlap(self, L1, R1, L2, R2):
		r1x1 = L1[0]
		r1y1 = L1[1]
		r1x2 = R1[0]
		r1y2 = R1[1]
		r2x1 = L2[0]
		r2y1 = L2[1]
		r2x2 = R2[0]
		r2y2 = R2[1]
		if r1y1 < r2y2 or r1x2 < r2x1 or r2x2 < r1x1 or (r2y1 < r1y2):
			return 0
		return 1


class Solution:

	def doOverlap(self, l1, r1, l2, r2):
		if r2[1] > l1[1] or r2[0] < l1[0] or r1[0] < l2[0] or (r1[1] > l2[1]):
			return 0
		return 1
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		p = [0] * 2
		q = [0] * 2
		r = [0] * 2
		s = [0] * 2
		(p[0], p[1], q[0], q[1], r[0], r[1], s[0], s[1]) = map(int, input().strip().split(' '))
		ob = Solution()
		ans = ob.doOverlap(p, q, r, s)
		print(ans)


class Solution:

	def doOverlap(self, L1, R1, L2, R2):
		if L1[1] < R2[1] or R1[0] < L2[0] or L1[0] > R2[0] or (R1[1] > L2[1]):
			return 0
		else:
			return 1
import math
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		p = [0] * 2
		q = [0] * 2
		r = [0] * 2
		s = [0] * 2
		(p[0], p[1], q[0], q[1], r[0], r[1], s[0], s[1]) = map(int, input().strip().split(' '))
		ob = Solution()
		ans = ob.doOverlap(p, q, r, s)
		print(ans)


class Solution:

	def doOverlap(self, L1, R1, L2, R2):
		a = L1[0]
		b = L1[1]
		c = R1[0]
		d = R1[1]
		e = L2[0]
		f = L2[1]
		g = R2[0]
		h = R2[1]
		if a > g or e > c or d > f or (h > b):
			return 0
		else:
			return 1


class Point:

	def __init__(self, x, y):
		self.x = x
		self.y = y

class Solution:

	def doOverlap(self, L1, R1, L2, R2):
		l1 = Point(L1[0], L1[1])
		r1 = Point(R1[0], R1[1])
		l2 = Point(L2[0], L2[1])
		r2 = Point(R2[0], R2[1])
		if l1.x > r2.x or l2.x > r1.x:
			return 0
		if r1.y > l2.y or r2.y > l1.y:
			return 0
		return 1