Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <vector>
  4.  
  5. std::vector<std::string> split(const std::string &s, char delim) {
  6. std::vector<std::string> elems;
  7. std::stringstream ss;
  8. ss.str(s);
  9. std::string item;
  10. while (std::getline(ss, item, delim)) {
  11. elems.push_back(item);
  12. }
  13. return elems;
  14. }
  15.  
  16. struct Rect {
  17. double x1, x2, y1, y2;
  18. void operator << (std::string line) {
  19. auto rectcoods = split(line, ' ');
  20. this->x1 = std::stod(split(rectcoods.at(0), ',').at(0));
  21. this->y1 = std::stod(split(rectcoods.at(0), ',').at(1));
  22. this->x2 = std::stod(split(rectcoods.at(1), ',').at(0));
  23. this->y2 = std::stod(split(rectcoods.at(1), ',').at(1));
  24. }
  25.  
  26. double area() {
  27. return ((std::max(x1,x2)- std::min(x1,x2))*(std::max(y1,y2) - std::min(y1,y2)));
  28. }
  29. };
  30.  
  31. Rect getIntersectionRect(Rect rect1, Rect rect2) {
  32. Rect result;
  33. result.x1 = std::max(std::min(rect1.x1, rect1.x2), std::min(rect2.x1, rect2.x2));
  34. result.x2 = std::min(std::max(rect1.x1, rect1.x2), std::max(rect2.x1, rect2.x2));
  35. result.y1 = std::min(std::max(rect1.y1, rect1.y2), std::max(rect2.y1, rect2.y2));
  36. result.y2 = std::max(std::min(rect1.y1, rect1.y2), std::min(rect2.y1, rect2.y2));
  37.  
  38. return result;
  39. }
  40.  
  41. double getIntersectionArea(Rect rect1, Rect rect2) {
  42. 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));
  43. 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));
  44.  
  45. return(std::max(height, double(0)) * std::max(width, double(0)));
  46. }
  47.  
  48. int main() {
  49. std::vector<Rect> rects;
  50.  
  51. std::string line;
  52. while(std::getline(std::cin, line) && !line.empty()) {
  53. Rect rect;
  54. rect << line;
  55. rects.push_back(rect);
  56. }
  57.  
  58. Rect currentIntersectionRect = rects.at(0);
  59. bool done = false;
  60. double solution = 0;
  61. for (auto rect : rects){
  62. if (getIntersectionArea(currentIntersectionRect, rect) == 0) {
  63. done = true; break;
  64. }
  65. currentIntersectionRect = getIntersectionRect(currentIntersectionRect,rect);
  66. }
  67.  
  68. if(!done) {solution = currentIntersectionRect.area();}
  69.  
  70. std::cout << solution << std::endl;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement