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 | |||
---|---|---|---|
IUSACO | module 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 , we can simply go through each of the 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 syssys.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 .
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 heightRectangle 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 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 classimport 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!).
#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 sysclass 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
Status | Source | Problem Name | Difficulty | Tags | Solution |
---|---|---|---|---|---|
Bronze | Very Easy | Show Tagsrect | External Sol | ||
Bronze | Easy | Show Tagsrect | External Sol | ||
CF | Normal | Show Tagsrect | Check CF |