I would like some feedback on a rectangle class I created.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Rect{
ll x1,y1,x2,y2,area(){return max(0LL,x2-x1)*max(0LL,y2-y1);}; // Rect with x2<x1 && y2<y1 --> product = positive
void input(){cin>>x1>>y1>>x2>>y2;};
Rect(int x1=0,int y1=0,int x2=0,int y2=0): x1(x1),x2(x2),y1(y1),y2(y2) {}
Rect intersect(Rect a){Rect t(max(x1,a.x1),max(y1,a.y1),min(x2,a.x2),min(y2,a.y2));
return t.x2>=t.x1&&t.y2>=t.y1?t:Rect();};
ll unite2(Rect a){return area()+a.area()-intersect(a).area();};
ll unite3(Rect a,Rect b){return area()+a.area()+b.area()
-intersect(a).area()-intersect(b).area()-a.intersect(b).area()
+a.intersect(*this).intersect(b).area();};
};
I appreciate any feedback on my class above. I have an area function, an intersect function (returning the rectangle itself), a unite function for 2 rectangles, and a unite function for 3 rectangles.
I think this can help shorten the code for several problems (for example, White Sheet):
int main(){
Rect w, b1, b2; w.input(); b1.input(); b2.input();
cout << (b1.intersect(w).unite2(b2.intersect(w)) < w.area() ? "YES" : "NO") << '\n';
}
Are there any more features I can add that usually come in problems? In terms of how concisely or directly I coded each attribute, can you please point out any suggestions for me to improve?
Also, is there an easy way to write unite3()
in terms of unite2()
, rather than rewriting all terms and expressions? Is there an easy way to code PIE with n variables in terms of PIE with n-1 variables?
Thank you!