Advertisement
kalabukdima

Functions riddle

Oct 2nd, 2022 (edited)
876
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 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.  
  11.     double a = std::sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
  12.     double b = std::sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2 - y3));
  13.     double c = std::sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3));
  14.     double p = (a + b + c) / 2;
  15.     double s = std::sqrt(p * (p - a) * (p - b) * (p - c));
  16.     int cnt = 0;
  17.     for (int x = std::floor(std::min(x1, std::min(x2, x3))); x <= std::ceil(std::max(x1, std::max(x2, x3))); ++x) {
  18.         for (int y = std::floor(std::min(y1, std::min(y2, y3))); y <= std::ceil(std::max(y1, std::max(y2, y3))); ++y) {
  19.             double a1 = std::sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
  20.             double a2 = std::sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
  21.             double a3 = std::sqrt((x - x3) * (x - x3) + (y - y3) * (y - y3));
  22.  
  23.             double p1 = (a + a1 + a2) / 2;
  24.             double s1 = std::sqrt(p1 * (p1 - a) * (p1 - a1) * (p1 - a2));
  25.             double p2 = (b + a2 + a3) / 2;
  26.             double s2 = std::sqrt(p2 * (p2 - b) * (p2 - a2) * (p2 - a3));
  27.             double p3 = (c + a1 + a3) / 2;
  28.             double s3 = std::sqrt(p3 * (p3 - c) * (p3 - a1) * (p3 - a3));
  29.  
  30.             if (std::abs(s1 + s2 + s3 - s) < kEps) {
  31.                 ++cnt;
  32.             }
  33.         }
  34.     }
  35.     std::cout << cnt;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement