Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <sstream>
- #include <vector>
- std::vector<std::string> split(const std::string &s, char delim) {
- std::vector<std::string> elems;
- std::stringstream ss;
- ss.str(s);
- std::string item;
- while (std::getline(ss, item, delim)) {
- elems.push_back(item);
- }
- return elems;
- }
- struct Rect {
- double x1, x2, y1, y2;
- void operator << (std::string line) {
- auto rectcoods = split(line, ' ');
- this->x1 = std::stod(split(rectcoods.at(0), ',').at(0));
- this->y1 = std::stod(split(rectcoods.at(0), ',').at(1));
- this->x2 = std::stod(split(rectcoods.at(1), ',').at(0));
- this->y2 = std::stod(split(rectcoods.at(1), ',').at(1));
- }
- double area() {
- return ((std::max(x1,x2)- std::min(x1,x2))*(std::max(y1,y2) - std::min(y1,y2)));
- }
- };
- Rect getIntersectionRect(Rect rect1, Rect rect2) {
- Rect result;
- result.x1 = std::max(std::min(rect1.x1, rect1.x2), std::min(rect2.x1, rect2.x2));
- result.x2 = std::min(std::max(rect1.x1, rect1.x2), std::max(rect2.x1, rect2.x2));
- result.y1 = std::min(std::max(rect1.y1, rect1.y2), std::max(rect2.y1, rect2.y2));
- result.y2 = std::max(std::min(rect1.y1, rect1.y2), std::min(rect2.y1, rect2.y2));
- return result;
- }
- double getIntersectionArea(Rect rect1, Rect rect2) {
- double height = std::min(std::max(rect1.y1, rect1.y2), std::max(rect2.y1, rect2.y2)) - std::max(std::min(rect1.y1, rect1.y2), std::min(rect2.y1, rect2.y2));
- double width = std::min(std::max(rect1.x1, rect1.x2), std::max(rect2.x1, rect2.x2)) - std::max(std::min(rect1.x1, rect1.x2), std::min(rect2.x1, rect2.x2));
- return(std::max(height, double(0)) * std::max(width, double(0)));
- }
- int main() {
- std::vector<Rect> rects;
- std::string line;
- while(std::getline(std::cin, line) && !line.empty()) {
- Rect rect;
- rect << line;
- rects.push_back(rect);
- }
- Rect currentIntersectionRect = rects.at(0);
- bool done = false;
- double solution = 0;
- for (auto rect : rects){
- if (getIntersectionArea(currentIntersectionRect, rect) == 0) {
- done = true; break;
- }
- currentIntersectionRect = getIntersectionRect(currentIntersectionRect,rect);
- }
- if(!done) {solution = currentIntersectionRect.area();}
- std::cout << solution << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement