Advertisement
Guest User

rectangle task, edit 2

a guest
Sep 24th, 2011
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cstdlib>
  4.  
  5. int main (int argc, char* argv[]) {
  6.     if (argc != 9) {
  7.         std::cerr << "Необходимо задать 8 параметров" << std::endl;
  8.         return 1;
  9.     }
  10.     int
  11.     // результирующая площать S, пересечение по x, y
  12.         S, Ix, Iy,
  13.     // Пр1
  14.         x1, y1, x2, y2,
  15.     // Пр2
  16.         X1, Y1, X2, Y2;
  17.     // какой-нибудь ввод для [xyXY][12]... или парсить argv
  18.     // ...
  19.     x1 = atoi(argv[1]); y1 = atoi(argv[2]); x2 = atoi(argv[3]); y2 = atoi(argv[4]);
  20.     X1 = atoi(argv[5]); Y1 = atoi(argv[6]); X2 = atoi(argv[7]); Y2 = atoi(argv[8]);
  21.     S = abs(x2 - x1) * abs(y2 - y1);
  22.     std::cout << "Заданы прямоугольники:\nПр1: (" << x1 << "; " << y1 << "), (" << x2 << "; " << y2 << "); S = " << S <<
  23.                        "\nПр2: (" << X1 << "; " << Y1 << "), (" << X2 << "; " << Y2 << ")" << std::endl;
  24.     Ix = abs(x2 - x1) + abs(X2 - X1) - abs(std::max(x2, std::max(X2, std::max(x1, X1))) - std::min(x1, std::min(X1, std::min(x2, X2))));
  25.     if (Ix > 0) {
  26.         Iy = abs(y2 - y1) + abs(Y2 - Y1) - abs(std::max(y2, std::max(Y2, std::max(y1, Y1))) - std::min(y1, std::min(Y1, std::min(y2, Y2))));
  27.         if (Iy > 0) { // вычитаем площадь пересечения
  28.             std::cout << "Площадь пересечения = " << Ix * Iy << std::endl;
  29.             S -= Ix * Iy;
  30.         }
  31.     }
  32.     std::cout << "Площадь той части Пр1, которая не входит в Пр2 = " << S << std::endl;
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement