Advertisement
kalabukdima

Bad code riddle

Sep 21st, 2021
936
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.57 KB | None | 0 0
  1. #include <vector>
  2. #include <iostream>
  3. #include <cmath>
  4.  
  5. const double kEps = 1e-7;
  6.  
  7. int main() {
  8.     double x1, y1, x2, y2, x3, y3;
  9.     std::cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
  10.     const int minX = std::floor(std::min(x1, std::min(x2, x3)));
  11.     const int minY = std::floor(std::min(y1, std::min(y2, y3)));
  12.     const int maxX = std::ceil(std::max(x1, std::max(x2, x3)));
  13.     const int maxY = std::ceil(std::max(y1, std::max(y2, y3)));
  14.  
  15.     double a = std::sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
  16.     double b = std::sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3));
  17.     double c = std::sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));
  18.     double p = (a + b + c) / 2;
  19.     double s = std::sqrt(p * (p - a) * (p - b) * (p - c));
  20.     int cnt = 0;
  21.     for (int x = minX; x <= maxX; ++x) {
  22.         for (int y = minY; y <= maxY; ++y) {
  23.             double a1 = std::sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
  24.             double a2 = std::sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
  25.             double a3 = std::sqrt((x - x3) * (x - x3) + (y - y3) * (y - y3));
  26.  
  27.             double p1 = (a + a1 + a2) / 2;
  28.             double s1 = std::sqrt(p1 * (p1 - a) * (p1 - a1) * (p1 - a2));
  29.             double p2 = (b + a2 + a3) / 2;
  30.             double s2 = std::sqrt(p2 * (p2 - b) * (p2 - a2) * (p2 - a3));
  31.             double p3 = (c + a1 + a3) / 2;
  32.             double s3 = std::sqrt(p3 * (p3 - c) * (p3 - a1) * (p3 - a3));
  33.  
  34.             if (std::abs(s1 + s2 + s3 - s) < kEps) {
  35.                 ++cnt;
  36.             }
  37.         }
  38.     }
  39.     std::cout << cnt;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement