PrevNext
Not Frequent
 0/4

Rectangle Geometry

Authors: Darren Yao, Michael Cao, Benjamin Qi, Allen Li, Andrew Wang

Problems involving rectangles whose sides are parallel to the coordinate axes.

Resources
IUSACOmodule is based off this

Most only include two or three squares or rectangles, in which case you can simply draw out cases on paper. This should logically lead to a solution.

Example: Blocked Billboard

Focus Problem – read through this problem before continuing!

Naive Solution

Since all coordinates are in the range [−1000,1000][-1000,1000], we can simply go through each of the 200022000^2 possible visible squares and check which ones are visible using nested for loops.

C++

#include <bits/stdc++.h>
using namespace std;
bool ok[2000][2000];
int main() {
freopen("billboard.in","r",stdin);
freopen("billboard.out","w",stdout);
for (int i = 0; i < 3; ++i) {
int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2;

Java

import java.io.*;
import java.util.*;
public class billboard
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new FileReader("billboard.in"));
PrintWriter out = new PrintWriter(new FileWriter("billboard.out"));
int ok[][] = new int[2000][2000];

Python

Unfortunately, the naive python solution will only get 9/10 test cases.

import sys
sys.stdin = open("billboard.in", "r")
sys.stdout = open("billboard.out", "w")
ok = [[False for i in range(2000)] for j in range(2000)]
for i in range(3):
x1, y1, x2, y2 = map(int, input().split())
x1 += 1000; y1 += 1000; x2 += 1000; y2 += 1000

Of course, this wouldn't suffice if the coordinates were changed to be up to 10910^9.

Rectangle Class

A useful class in Java for dealing with rectangle geometry problems (though definitely overkill) is the built-in Rectangle class.

Java

To create a new rectangle, use the following constructor:

// creates a rectangle with upper-left corner at (x,y) with a specified width and height
Rectangle newRect = new Rectangle(x, y, width, height);

The Rectangle class supports numerous useful methods, including the following:

  • firstRect.intersects(secondRect) checks if two rectangles intersect.
  • firstRect.union(secondRect) returns a rectangle representing the union of two rectangles.
  • firstRect.contains(x, y) checks whether the integer point (x,y)(x,y) exists in firstRect.
  • firstRect.intersection(secondRect) returns a rectangle representing the intersection of two rectangles.
  • what happens when intersection is empty?

This class can often lessen the implementation needed in a lot of bronze problems and CodeForces problems.

With Built-in Rectangle class:

import java.awt.Rectangle; //needed to use Rectangle class
import java.io.*;
import java.util.*;
public class blockedBillboard {
public static void main(String[] args) throws IOException{
Scanner sc = new Scanner(new File("billboard.in"));
PrintWriter pw = new PrintWriter(new FileWriter("billboard.out"));
int x1, y1, x2, y2;

Without Built-in Rectangle class:

import java.io.*;
import java.util.*;
class Rect {
int x1, y1, x2, y2;
Rect() {}
int area() { return (y2-y1)*(x2-x1); }
}
public class blockedBillboard {

C++

Unfortunately, C++ doesn't have a built in rectangle struct or class, so you need to write the functions yourself. Here is the solution to Blocked Billboard written in C++ (thanks, Brian Dean!).

What's a struct?

A C++ struct is the same as a C++ class but all members are public rather than private by default. Check LCPP for more information.

#include <bits/stdc++.h>
using namespace std;
struct Rect {
int x1,y1,x2,y2;
int area() { return (y2-y1)*(x2-x1); }
};
int intersect(Rect p, Rect q){
int xOverlap = max(0,min(p.x2,q.x2)-max(p.x1,q.x1));

Python

Unfortunately, Python doesn't have a built in rectangle class, so you need to write the class yourself.

import sys
class Rect:
def __init__(self):
self.x1, self.y1, self.x2, self.y2 = map(int, input().split())
def area(self):
return (self.y2 - self.y1) * (self.x2 - self.x1)
def intersect(p, q):

Problems

StatusSourceProblem NameDifficultyTagsSolution
BronzeVery Easy
Show Tags

rect

External Sol
BronzeEasy
Show Tags

rect

External Sol
CFNormal
Show Tags

rect

Check CF

Give Us Feedback on Rectangle Geometry!

Module Progress:

PrevNext