This repository has been archived on 2022-06-22. You can view files and clone it, but cannot push or open issues or pull requests.
usaco-guide/content/3_Bronze/Rect_Geo.md

136 lines
4.5 KiB
Markdown
Raw Normal View History

2020-06-04 01:42:57 +00:00
---
2020-06-16 01:06:49 +00:00
id: rect-geo
2020-06-04 05:39:49 +00:00
title: "Rectangle Geometry"
author: Darren Yao, Michael Cao
2020-06-04 01:42:57 +00:00
---
2020-06-17 22:18:07 +00:00
<module-excerpt>
2020-06-16 18:05:15 +00:00
"Geometry" problems on USACO Bronze are usually quite simple and limited to intersections and unions of squares or rectangles.
2020-06-03 14:17:07 +00:00
2020-06-17 22:18:07 +00:00
</module-excerpt>
2020-06-16 18:05:15 +00:00
- Some 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.
- Also, the coordinates typically only go up to $1000$, so a program that performs $\approx 1000^2$ operations (ex. with a nested loop) should pass.
## Rectangle Class (Java)
2020-06-17 18:05:19 +00:00
A useful class in `Java` for dealing with rectangle geometry problems is the built-in [`Rectangle`](https://docs.oracle.com/javase/8/docs/api/java/awt/Rectangle.html) class. To create a new rectangle, use the following constructor:
```java
//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.
2020-06-17 01:32:29 +00:00
2020-06-17 18:42:42 +00:00
`firstRect.intersects(secondRect)` checks if two rectangles intersect.
2020-06-17 01:32:29 +00:00
2020-06-17 18:42:42 +00:00
`firstRect.union(secondRect)` returns a rectangle representing the union of two rectangles.
2020-06-17 01:32:29 +00:00
2020-06-17 18:42:42 +00:00
`firstRect.contains(x, y)` checks whether the integer point (x,y) exists in firstRect.
2020-06-17 01:32:29 +00:00
2020-06-17 18:42:42 +00:00
`firstRect.intersection(secondRect)` returns a rectangle representing the intersection of two rectangles.
2020-06-17 18:05:19 +00:00
This class can often lessen the implementation needed in a lot of bronze problems and CodeForces problems.
2020-06-17 18:05:19 +00:00
For example, here is a nice implementation of the problem [Blocked Billboard](http://usaco.org/index.php?page=viewproblem2&cpid=759) ([editorial](http://www.usaco.org/current/data/sol_billboard_bronze_dec17.html)).
2020-06-17 23:10:45 +00:00
<spoiler title="Java Solution">
```java
import java.awt.Rectangle; //needed to use Rectangle class
2020-06-17 18:42:42 +00:00
import java.io.*;
import java.util.*;
2020-06-17 18:42:42 +00:00
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"));
2020-06-17 18:42:42 +00:00
int x1, y1, x2, y2;
//the top left point is (0,0), so you need to do -y2
x1 = sc.nextInt(); y1 = sc.nextInt(); x2 = sc.nextInt(); y2 = sc.nextInt();
Rectangle firstRect = new Rectangle(x1, -y2, x2-x1, y2-y1);
x1 = sc.nextInt(); y1 = sc.nextInt(); x2 = sc.nextInt(); y2 = sc.nextInt();
Rectangle secondRect = new Rectangle(x1, -y2, x2-x1, y2-y1);
x1 = sc.nextInt(); y1 = sc.nextInt(); x2 = sc.nextInt(); y2 = sc.nextInt();
Rectangle truck = new Rectangle(x1, -y2, x2-x1, y2-y1);
long firstIntersect = getArea(firstRect.intersection(truck));
long secondIntersect = getArea(secondRect.intersection(truck));
pw.println(getArea(firstRect) + getArea(secondRect)
2020-06-17 18:42:42 +00:00
- firstIntersect - secondIntersect);
pw.close();
}
public static long getArea(Rectangle r){
2020-06-17 18:42:42 +00:00
if(r.getWidth() <= 0 || r.getHeight() <= 0){
return 0;
}
return (long)r.getHeight() * (long)r.getWidth();
}
}
2020-06-17 18:42:42 +00:00
```
2020-06-17 23:10:45 +00:00
</spoiler>
## Rectangle Class (C++)
Unfortunately, C++ doesn't have a built in rectangle class, so you need to write the functions yourself. Here is the solution to Blocked Billboard written in C++ (thanks, Brian Dean!).
2020-06-17 23:10:45 +00:00
<spoiler title="C++ Solution">
2020-06-17 18:05:19 +00:00
```cpp
2020-06-17 18:42:42 +00:00
#include <iostream>
#include <fstream>
using namespace std;
struct Rect{
int x1, y1, x2, y2;
};
2020-06-17 18:42:42 +00:00
int area(Rect r){
return (r.y2 - r.y1) * (r.x2 - r.x1);
}
int intersect(Rect p, Rect q){
int xOverlap = max(0, min(p.x2, q.x2) - max(p.x1, q.x1));
int yOverlap = max(0, min(p.y2, q.y2) - max(p.y1, q.y1));
return xOverlap * yOverlap;
}
int main(){
ifstream cin ("billboard.in");
ofstream cout ("billboard.out");
Rect a, b, t; // billboards a, b, and the truck
cin >> a.x1 >> a.y1 >> a.x2 >> a.y2;
cin >> b.x1 >> b.y1 >> b.x2 >> b.y2;
cin >> t.x1 >> t.y1 >> t.x2 >> t.y2;
cout << area(a) + area(b) - intersect(a, t) - intersect(b, t);
}
2020-06-17 18:42:42 +00:00
```
2020-06-17 23:10:45 +00:00
</spoiler>
## Problems
2020-06-17 18:05:19 +00:00
- USACO Bronze
2020-06-16 18:05:15 +00:00
- [Fence Painting](http://usaco.org/index.php?page=viewproblem2&cpid=567)
- 1D geometry!!
- [Square Pasture](http://usaco.org/index.php?page=viewproblem2&cpid=663)
- [Blocked Billboard](http://usaco.org/index.php?page=viewproblem2&cpid=759)
- Rectangles
- [Blocked Billboard II](http://usaco.org/index.php?page=viewproblem2&cpid=783)
- Also rectangles
- Other
- [CF 587 (Div. 3) C: White Sheet](https://codeforces.com/contest/1216/problem/C)
2020-06-17 18:05:19 +00:00
- See this code (TODO; codeforces is down) for a nice implementation using the Java Rectangle class.