SHARE
TWEET

Untitled

a guest Apr 18th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2.  
  3. #include <vector>
  4. #include <array>
  5.  
  6. using namespace std;
  7.  
  8. template <typename T, size_t M>
  9. class Polygon
  10. {
  11. public:
  12.     double perimeter();
  13.     void add_vertex(const array<T, M> &point);
  14.     double min_square();
  15.     double min_surround_area();
  16.  
  17. private:
  18.     vector<array<T, M>> coordinates_;
  19. };
  20.  
  21. template <typename T, size_t M>
  22. void Polygon<T, M>::add_vertex(const array<T, M> &point)
  23. {
  24.     coordinates_.push_back(point);
  25. }
  26.  
  27. template <typename T, size_t M>
  28. double Polygon<T, M>::perimeter()
  29. {
  30.     double p = 0; //хранение результата
  31.     double len; //длина текущего отрезка
  32.     double sum; //текущая координата
  33.                 //a_len = sqrt((x ^ 2) + (y ^ 2))
  34.  
  35.     for (size_t i = 0; i < coordinates_.back() - 1; i++) //по точкам
  36.     {
  37.         sum = 0;
  38.  
  39.         for (size_t j = 0; j < M; j++) //по координатам
  40.             sum += (abs(coordinates_[i][j] - coordinates_[i + 1][j]))
  41.             * (abs(coordinates_[i][j] - coordinates_[i + 1][j]));
  42.  
  43.  
  44.         len = sqrt(sum);
  45.         p += len;
  46.     }
  47.  
  48.     for (size_t j = 0; j < M; j++)
  49.         sum += (abs(coordinates_[0][j] - coordinates_[coordinates_.back()][j]))
  50.         * (abs(coordinates_[0][j] - coordinates_[coordinates_.back()][j]));
  51.  
  52.     len = sqrt(sum);
  53.     p += len;
  54.  
  55.     return p;
  56. }
  57.  
  58. template <typename T, size_t M>
  59. double Polygon<T, M>::min_square()
  60. {
  61.     double s = 0;
  62.     T min_x, min_y, max_x, max_y;
  63.  
  64.     min_x = coordinates_[0][0];
  65.     max_x = coordinates_[0][0];
  66.     min_y = coordinates_[0][1];
  67.     max_y = coordinates_[0][1];
  68.  
  69.     for (size_t i = 0; i < coordinates_.back() - 1; i++) //по точкам
  70.     {
  71.             if (coordinates_[i][0] > max_x) max_x = coordinates_[i][0];
  72.             if (coordinates_[i][0] < min_x) min_x = coordinates_[i][0];
  73.  
  74.             if (coordinates_[i][1] > max_y) max_y = coordinates_[i][1];
  75.             if (coordinates_[i][1] > min_y) min_y = coordinates_[i][1];
  76.     }
  77.  
  78.     s = (max_x - min_x) * (max_y - min_y);
  79.  
  80.     return s;
  81. }
  82.  
  83. template<typename T, size_t M>
  84. double Polygon<T, M>::min_surround_area()
  85. {
  86.     static_assert(M == 2, "Реализовано только для случая M = 2");
  87.     double s = min_square();
  88.  
  89.     return s;
  90. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top