Advertisement
aed1oN

LOL

Jun 5th, 2021
1,168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.18 KB | None | 0 0
  1. //TP 2018/2019: Zadaća 4, Zadatak 1
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <iostream>
  5. #include <memory>
  6. #include <stdexcept>
  7. #include <utility>
  8. #include <vector>
  9.  
  10. typedef std::pair<double, double> Tacka;
  11.  
  12. double TestOrijentacije(const Tacka &t1, const Tacka &t2, const Tacka &t3) {
  13.     return t1.first * (t2.second - t3.second) - t2.first * (t1.second - t3.second) + t3.first * (t1.second - t2.second);
  14. }
  15.  
  16. double Udaljenost(double x1, double y1, double x2, double y2) {
  17.     return std::sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
  18. }
  19.  
  20. class Trougao {
  21.     Tacka A, B, C;
  22.     friend double TestOrijentacije(const Tacka &t1, const Tacka &t2, const Tacka &t3);
  23.     friend double Udaljenost(const Tacka &t1, const Tacka &t2);
  24. public:
  25.     Trougao(const Tacka &t1, const Tacka &t2, const Tacka &t3) {
  26.         if (!Orijetacija(t1, t2, t3))
  27.             throw std::domain_error("Nekorektne pozicije tjemena");
  28.         A = t1; B = t2; C = t3;    
  29.     }
  30.     void Postavi(const Tacka &t1, const Tacka &t2, const Tacka &t3) {
  31.         if (!Orijetacija(t1, t2, t3))
  32.             throw std::domain_error("Nekorektne pozicije tjemena");
  33.         A = t1; B = t2; C = t3;
  34.     }
  35.     void Postavi(int indeks, const Tacka &t) {
  36.         if      (indeks == 1) A = t;
  37.         else if (indeks == 2) B = t;
  38.         else if (indeks == 3) C = t;
  39.         else throw std::range_error("Nekorektan indeks");
  40.     }
  41.     static int Orijetacija(const Tacka &t1, const Tacka &t2, const Tacka &t3) {
  42.         double test = TestOrijentacije(t1, t2, t3);
  43.         if      (test > 0) return  1;
  44.         else if (test < 0) return -1;
  45.         else               return  0;
  46.     }
  47.     Tacka DajTjeme(int indeks) const {
  48.         if      (indeks == 1) return A;
  49.         else if (indeks == 2) return B;
  50.         else if (indeks == 3) return C;
  51.         else throw std::range_error("Nekorektan indeks");
  52.     }
  53.     double DajStranicu(int indeks) const {
  54.         if      (indeks == 1) return Udaljenost(B.first, B.second, C.first, C.second);  
  55.         else if (indeks == 2) return Udaljenost(A.first, A.second, C.first, C.second);
  56.         else if (indeks == 3) return Udaljenost(A.first, A.second, B.first, B.second);
  57.         else throw std::range_error("Nekorektan indeks");
  58.     }
  59.     double DajUgao(int indeks) const {
  60.         if      (indeks == 1) return std::acos((DajStranicu(2) * DajStranicu(2) + DajStranicu(3) * DajStranicu(3) - DajStranicu(1) * DajStranicu(1)) / (2 * DajStranicu(2) * DajStranicu(3)));
  61.         else if (indeks == 2) return std::acos((DajStranicu(1) * DajStranicu(1) + DajStranicu(3) * DajStranicu(3) - DajStranicu(2) * DajStranicu(2)) / (2 * DajStranicu(1) * DajStranicu(3)));
  62.         else if (indeks == 3) return std::acos((DajStranicu(1) * DajStranicu(1) + DajStranicu(2) * DajStranicu(2) - DajStranicu(3) * DajStranicu(3)) / (2 * DajStranicu(1) * DajStranicu(2)));
  63.         else throw std::range_error("Nekorektan indeks");
  64.     }
  65.     Tacka DajCentar() const { return {(A.first + B.first + C.first) / 3, (A.second + B.second + C.second) / 3}; }
  66.     double DajObim() const { return DajStranicu(1) + DajStranicu(2) + DajStranicu(3); }
  67.     double DajPovrsinu() const { return std::fabs(TestOrijentacije(A, B, C) / 2); }
  68.     bool DaLiJePozitivnoOrijentiran() const {
  69.         if (Orijetacija(A, B, C) >= 0) return true;
  70.         else return false;
  71.     }
  72.     bool DaLiJeUnutra(const Tacka &t) const {
  73.         if ((TestOrijentacije(A, B, C) >= 0 && TestOrijentacije(A, B, t) >= 0 && TestOrijentacije(B, C, t) >= 0 && TestOrijentacije(C, A, t) >= 0) ||
  74.             (TestOrijentacije(A, B, C) <  0 && TestOrijentacije(A, B, t) <  0 && TestOrijentacije(B, C, t) <  0 && TestOrijentacije(C, A, t) <  0))
  75.                 return true;
  76.         else return false;
  77.     }
  78.     void Ispisi() const {
  79.         std::cout << "((" << A.first << "," << A.second << "),(" << B.first << "," << B.second << "),(" << C.first << "," << C.second << "))";    
  80.     }
  81.     void Transliraj(double deltaX, double deltaY) {
  82.         A.first += deltaX; B.first += deltaX; C.first += deltaX;
  83.         A.second += deltaY; B.second += deltaY; C.second += deltaY;
  84.     }
  85.     void Centriraj(const Tacka &t);
  86.     void Rotiraj(const Tacka &t, double ugao);
  87.     void Skaliraj(const Tacka &t, double faktor);
  88.     void Rotiraj(double ugao) {
  89.         Rotiraj(DajCentar(), ugao);
  90.     }
  91.     void Skaliraj(double faktor) {
  92.         Skaliraj(DajCentar(), faktor);
  93.     }
  94.     friend bool DaLiSuIdenticni(const Trougao &t1, const Trougao &t2);
  95.     friend bool DaLiSuPodudarni(const Trougao &t1, const Trougao &t2);
  96.     friend bool DaLiSuSlicni(const Trougao &t1, const Trougao &t2);
  97. };
  98.  
  99. void Trougao::Centriraj(const Tacka &t) {
  100.     double deltaX = Udaljenost(DajCentar().first, DajCentar().second, t.first, DajCentar().second);
  101.     double deltaY = Udaljenost(DajCentar().first, DajCentar().second, DajCentar().first, t.second);
  102.     if (t.first >= DajCentar().first && t.second >= DajCentar().second) {
  103.         A.first += deltaX; B.first += deltaX; C.first += deltaX;
  104.         A.second += deltaY; B.second += deltaY; C.second += deltaY;
  105.     } else if (t.first >= DajCentar().first && t.second <= DajCentar().second) {
  106.         A.first += deltaX; B.first += deltaX; C.first += deltaX;
  107.         A.second -= deltaY; B.second -= deltaY; C.second -= deltaY;
  108.     } else if (t.first <= DajCentar().first && t.second <= DajCentar().second) {
  109.         A.first -= deltaX; B.first -= deltaX; C.first -= deltaX;
  110.         A.second -= deltaY; B.second -= deltaY; C.second -= deltaY;
  111.     } else {
  112.         A.first -= deltaX; B.first -= deltaX; C.first -= deltaX;
  113.         A.second += deltaY; B.second += deltaY; C.second += deltaY;
  114.     }
  115. }
  116.  
  117. void Trougao::Rotiraj(const Tacka &t, double ugao)  {
  118.     double aX = t.first  + (A.first - t.first) * std::cos(ugao) - (A.second - t.second) * std::sin(ugao);
  119.     double aY = t.second + (A.first - t.first) * std::sin(ugao) + (A.second - t.second) * std::cos(ugao);
  120.     double bX = t.first  + (B.first - t.first) * std::cos(ugao) - (B.second - t.second) * std::sin(ugao);
  121.     double bY = t.second + (B.first - t.first) * std::sin(ugao) + (B.second - t.second) * std::cos(ugao);
  122.     double cX = t.first  + (C.first - t.first) * std::cos(ugao) - (C.second - t.second) * std::sin(ugao);
  123.     double cY = t.second + (C.first - t.first) * std::sin(ugao) + (C.second - t.second) * std::cos(ugao);
  124.     A.first  = aX; A.second = aY;   B.first  = bX; B.second = bY;   C.first  = cX; C.second = cY;
  125. }
  126.  
  127. void Trougao::Skaliraj(const Tacka &t, double faktor)  {
  128.     if (!faktor) throw std::domain_error("Nekorektan faktor skaliranja");
  129.     double aX = t.first  + faktor * (A.first - t.first);
  130.     double aY = t.second + faktor * (A.second - t.second);
  131.     double bX = t.first  + faktor * (B.first - t.first);
  132.     double bY = t.second + faktor * (B.second - t.second);
  133.     double cX = t.first  + faktor * (C.first - t.first);
  134.     double cY = t.second + faktor * (C.second - t.second);
  135.         A.first = aX; A.second = aY;   B.first = bX; B.second = bY;   C.first = cX; C.second = cY;
  136. }
  137.  
  138. bool DaLiSuIdenticni(const Trougao &t1, const Trougao &t2) {
  139.     double stranice1[]{t1.DajStranicu(1), t1.DajStranicu(2), t1.DajStranicu(3)};
  140.     double stranice2[]{t2.DajStranicu(1), t2.DajStranicu(2), t2.DajStranicu(3)};
  141.     for (int i = 0; i < 3; i++) {
  142.         bool ima = false;
  143.         for (int j = 0; j < 3; j++)
  144.             if (stranice1[i] == stranice2[j]) {
  145.                 ima = true;
  146.                 break;
  147.             }
  148.         if (!ima)
  149.             return false;
  150.     }
  151.     double uglovi1[]{t1.DajUgao(1), t1.DajUgao(2), t1.DajUgao(3)};
  152.     double uglovi2[]{t2.DajUgao(1), t2.DajUgao(2), t2.DajUgao(3)};
  153.     for (int i = 0; i < 3; i++) {
  154.         bool ima = false;
  155.         for (int j = 0; j < 3; j++)
  156.             if (uglovi1[i] == uglovi2[j]) {
  157.                 ima = true;
  158.                 break;
  159.             }
  160.         if (!ima)
  161.             return false;
  162.     }
  163.     return true;
  164. }
  165.  
  166. bool DaLiSuPodudarni(const Trougao &t1, const Trougao &t2) {
  167.     double stranice1[]{t1.DajStranicu(1), t1.DajStranicu(2), t1.DajStranicu(3)};
  168.     double stranice2[]{t2.DajStranicu(1), t2.DajStranicu(2), t2.DajStranicu(3)};
  169.     for (int i = 0; i < 3; i++)
  170.         if (stranice1[i] != stranice2[i])
  171.             return false;
  172.    
  173.     return true;    
  174. }
  175.  
  176. int main () {
  177.     int brTrouglova;
  178.     std::cout << "Koliko zelite kreirati trouglova: ";
  179.     std::cin >> brTrouglova;
  180.     std::vector<std::shared_ptr<Trougao>> trouglovi;
  181.     for (int i = 0; i < brTrouglova; i++) {
  182.         std::cout << "Unesite podatke za " << i + 1 << ". trougao (x1 y1 x2 y2 x3 y3): ";
  183.         Tacka a, b, c;
  184.         double x, y;
  185.         std::cin >> x >> y;
  186.         a = std::make_pair(x, y);
  187.         std::cin >> x >> y;
  188.         b = std::make_pair(x, y);
  189.         std::cin >> x >> y;
  190.         c = std::make_pair(x, y);
  191.         try {
  192.             trouglovi.push_back(std::make_shared<Trougao>(a, b, c));
  193.         } catch (std::domain_error exc) {
  194.             std::cout << exc.what() << ", ponovite unos!" << std::endl;
  195.             i--;
  196.         }
  197.     }
  198.     double dx, dy;
  199.     std::cout << "Unesite vektor translacije (dx dy): ";
  200.     std::cin >> dx >> dy;
  201.     std::transform(trouglovi.begin(), trouglovi.end(), trouglovi.begin(), [dx, dy](std::shared_ptr<Trougao> t) { t->Transliraj(dx, dy); return t; });
  202.     double ugao;
  203.     std::cout << "Unesite ugao rotracije: ";
  204.     std::cin >> ugao;
  205.     std::transform(trouglovi.begin(), trouglovi.end(), trouglovi.begin(), [ugao](std::shared_ptr<Trougao> t) { t->Rotiraj(ugao); return t; });
  206.     double faktorSkaliranja;
  207.     std::cout << "Unesite faktor skaliranja: ";
  208.     std::cin >> faktorSkaliranja;
  209.     std::transform(trouglovi.begin(), trouglovi.end(), trouglovi.begin(), [faktorSkaliranja](std::shared_ptr<Trougao> t) { t->Skaliraj(t->DajTjeme(1), faktorSkaliranja); return t; });
  210.     std::sort(trouglovi.begin(), trouglovi.end(), [](std::shared_ptr<Trougao> t1, std::shared_ptr<Trougao> t2) { return t1->DajPovrsinu() < t2 -> DajPovrsinu(); });
  211.     std::cout << "Trouglovi nakon obavljenih transformacija: " << std::endl;
  212.     std::for_each(trouglovi.begin(), trouglovi.end(), [](std::shared_ptr<Trougao> t) { t->Ispisi(); std::cout << std::endl; });
  213.     auto it = std::min_element(trouglovi.begin(), trouglovi.end(), [](std::shared_ptr<Trougao> t1, std::shared_ptr<Trougao> t2) { return t1->DajObim() < t2->DajObim(); });
  214.     std::cout << "Trougao sa najmanjim obimom: ";
  215.     (*it)->Ispisi();
  216.     // Trougao t({7, 5}, {3, 3}, {3, 9});
  217.     // std::cout << t.DajPovrsinu();
  218.     std::vector<std::pair<Trougao, Trougao>> identicni;
  219.     for (int i = 0; i < trouglovi.size(); i++)
  220.         for (int j = 0; j < trouglovi.size(); j++)
  221.             if (i != j && DaLiSuIdenticni(*trouglovi.at(i), *trouglovi.at(j)))
  222.                 identicni.push_back({*trouglovi.at(i), *trouglovi.at(j)});
  223.     std::cout << std::endl << "Parovi identicnih trouglova: " << std::endl;
  224.     for (int i = 0; i < identicni.size(); i++) {
  225.         identicni.at(i).first.Ispisi();
  226.         std::cout << " i ";
  227.         identicni.at(i).second.Ispisi();
  228.         std::cout << std::endl;
  229.     }
  230.                
  231.     return 0;
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement