Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //TP 2018/2019: Zadaća 4, Zadatak 1
- #include <algorithm>
- #include <cmath>
- #include <iostream>
- #include <memory>
- #include <stdexcept>
- #include <utility>
- #include <vector>
- typedef std::pair<double, double> Tacka;
- double TestOrijentacije(const Tacka &t1, const Tacka &t2, const Tacka &t3) {
- return t1.first * (t2.second - t3.second) - t2.first * (t1.second - t3.second) + t3.first * (t1.second - t2.second);
- }
- double Udaljenost(double x1, double y1, double x2, double y2) {
- return std::sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
- }
- class Trougao {
- Tacka A, B, C;
- friend double TestOrijentacije(const Tacka &t1, const Tacka &t2, const Tacka &t3);
- friend double Udaljenost(const Tacka &t1, const Tacka &t2);
- public:
- Trougao(const Tacka &t1, const Tacka &t2, const Tacka &t3) {
- if (!Orijetacija(t1, t2, t3))
- throw std::domain_error("Nekorektne pozicije tjemena");
- A = t1; B = t2; C = t3;
- }
- void Postavi(const Tacka &t1, const Tacka &t2, const Tacka &t3) {
- if (!Orijetacija(t1, t2, t3))
- throw std::domain_error("Nekorektne pozicije tjemena");
- A = t1; B = t2; C = t3;
- }
- void Postavi(int indeks, const Tacka &t) {
- if (indeks == 1) A = t;
- else if (indeks == 2) B = t;
- else if (indeks == 3) C = t;
- else throw std::range_error("Nekorektan indeks");
- }
- static int Orijetacija(const Tacka &t1, const Tacka &t2, const Tacka &t3) {
- double test = TestOrijentacije(t1, t2, t3);
- if (test > 0) return 1;
- else if (test < 0) return -1;
- else return 0;
- }
- Tacka DajTjeme(int indeks) const {
- if (indeks == 1) return A;
- else if (indeks == 2) return B;
- else if (indeks == 3) return C;
- else throw std::range_error("Nekorektan indeks");
- }
- double DajStranicu(int indeks) const {
- if (indeks == 1) return Udaljenost(B.first, B.second, C.first, C.second);
- else if (indeks == 2) return Udaljenost(A.first, A.second, C.first, C.second);
- else if (indeks == 3) return Udaljenost(A.first, A.second, B.first, B.second);
- else throw std::range_error("Nekorektan indeks");
- }
- double DajUgao(int indeks) const {
- if (indeks == 1) return std::acos((DajStranicu(2) * DajStranicu(2) + DajStranicu(3) * DajStranicu(3) - DajStranicu(1) * DajStranicu(1)) / (2 * DajStranicu(2) * DajStranicu(3)));
- else if (indeks == 2) return std::acos((DajStranicu(1) * DajStranicu(1) + DajStranicu(3) * DajStranicu(3) - DajStranicu(2) * DajStranicu(2)) / (2 * DajStranicu(1) * DajStranicu(3)));
- else if (indeks == 3) return std::acos((DajStranicu(1) * DajStranicu(1) + DajStranicu(2) * DajStranicu(2) - DajStranicu(3) * DajStranicu(3)) / (2 * DajStranicu(1) * DajStranicu(2)));
- else throw std::range_error("Nekorektan indeks");
- }
- Tacka DajCentar() const { return {(A.first + B.first + C.first) / 3, (A.second + B.second + C.second) / 3}; }
- double DajObim() const { return DajStranicu(1) + DajStranicu(2) + DajStranicu(3); }
- double DajPovrsinu() const { return std::fabs(TestOrijentacije(A, B, C) / 2); }
- bool DaLiJePozitivnoOrijentiran() const {
- if (Orijetacija(A, B, C) >= 0) return true;
- else return false;
- }
- bool DaLiJeUnutra(const Tacka &t) const {
- if ((TestOrijentacije(A, B, C) >= 0 && TestOrijentacije(A, B, t) >= 0 && TestOrijentacije(B, C, t) >= 0 && TestOrijentacije(C, A, t) >= 0) ||
- (TestOrijentacije(A, B, C) < 0 && TestOrijentacije(A, B, t) < 0 && TestOrijentacije(B, C, t) < 0 && TestOrijentacije(C, A, t) < 0))
- return true;
- else return false;
- }
- void Ispisi() const {
- std::cout << "((" << A.first << "," << A.second << "),(" << B.first << "," << B.second << "),(" << C.first << "," << C.second << "))";
- }
- void Transliraj(double deltaX, double deltaY) {
- A.first += deltaX; B.first += deltaX; C.first += deltaX;
- A.second += deltaY; B.second += deltaY; C.second += deltaY;
- }
- void Centriraj(const Tacka &t);
- void Rotiraj(const Tacka &t, double ugao);
- void Skaliraj(const Tacka &t, double faktor);
- void Rotiraj(double ugao) {
- Rotiraj(DajCentar(), ugao);
- }
- void Skaliraj(double faktor) {
- Skaliraj(DajCentar(), faktor);
- }
- friend bool DaLiSuIdenticni(const Trougao &t1, const Trougao &t2);
- friend bool DaLiSuPodudarni(const Trougao &t1, const Trougao &t2);
- friend bool DaLiSuSlicni(const Trougao &t1, const Trougao &t2);
- };
- void Trougao::Centriraj(const Tacka &t) {
- double deltaX = Udaljenost(DajCentar().first, DajCentar().second, t.first, DajCentar().second);
- double deltaY = Udaljenost(DajCentar().first, DajCentar().second, DajCentar().first, t.second);
- if (t.first >= DajCentar().first && t.second >= DajCentar().second) {
- A.first += deltaX; B.first += deltaX; C.first += deltaX;
- A.second += deltaY; B.second += deltaY; C.second += deltaY;
- } else if (t.first >= DajCentar().first && t.second <= DajCentar().second) {
- A.first += deltaX; B.first += deltaX; C.first += deltaX;
- A.second -= deltaY; B.second -= deltaY; C.second -= deltaY;
- } else if (t.first <= DajCentar().first && t.second <= DajCentar().second) {
- A.first -= deltaX; B.first -= deltaX; C.first -= deltaX;
- A.second -= deltaY; B.second -= deltaY; C.second -= deltaY;
- } else {
- A.first -= deltaX; B.first -= deltaX; C.first -= deltaX;
- A.second += deltaY; B.second += deltaY; C.second += deltaY;
- }
- }
- void Trougao::Rotiraj(const Tacka &t, double ugao) {
- double aX = t.first + (A.first - t.first) * std::cos(ugao) - (A.second - t.second) * std::sin(ugao);
- double aY = t.second + (A.first - t.first) * std::sin(ugao) + (A.second - t.second) * std::cos(ugao);
- double bX = t.first + (B.first - t.first) * std::cos(ugao) - (B.second - t.second) * std::sin(ugao);
- double bY = t.second + (B.first - t.first) * std::sin(ugao) + (B.second - t.second) * std::cos(ugao);
- double cX = t.first + (C.first - t.first) * std::cos(ugao) - (C.second - t.second) * std::sin(ugao);
- double cY = t.second + (C.first - t.first) * std::sin(ugao) + (C.second - t.second) * std::cos(ugao);
- A.first = aX; A.second = aY; B.first = bX; B.second = bY; C.first = cX; C.second = cY;
- }
- void Trougao::Skaliraj(const Tacka &t, double faktor) {
- if (!faktor) throw std::domain_error("Nekorektan faktor skaliranja");
- double aX = t.first + faktor * (A.first - t.first);
- double aY = t.second + faktor * (A.second - t.second);
- double bX = t.first + faktor * (B.first - t.first);
- double bY = t.second + faktor * (B.second - t.second);
- double cX = t.first + faktor * (C.first - t.first);
- double cY = t.second + faktor * (C.second - t.second);
- A.first = aX; A.second = aY; B.first = bX; B.second = bY; C.first = cX; C.second = cY;
- }
- bool DaLiSuIdenticni(const Trougao &t1, const Trougao &t2) {
- double stranice1[]{t1.DajStranicu(1), t1.DajStranicu(2), t1.DajStranicu(3)};
- double stranice2[]{t2.DajStranicu(1), t2.DajStranicu(2), t2.DajStranicu(3)};
- for (int i = 0; i < 3; i++) {
- bool ima = false;
- for (int j = 0; j < 3; j++)
- if (stranice1[i] == stranice2[j]) {
- ima = true;
- break;
- }
- if (!ima)
- return false;
- }
- double uglovi1[]{t1.DajUgao(1), t1.DajUgao(2), t1.DajUgao(3)};
- double uglovi2[]{t2.DajUgao(1), t2.DajUgao(2), t2.DajUgao(3)};
- for (int i = 0; i < 3; i++) {
- bool ima = false;
- for (int j = 0; j < 3; j++)
- if (uglovi1[i] == uglovi2[j]) {
- ima = true;
- break;
- }
- if (!ima)
- return false;
- }
- return true;
- }
- bool DaLiSuPodudarni(const Trougao &t1, const Trougao &t2) {
- double stranice1[]{t1.DajStranicu(1), t1.DajStranicu(2), t1.DajStranicu(3)};
- double stranice2[]{t2.DajStranicu(1), t2.DajStranicu(2), t2.DajStranicu(3)};
- for (int i = 0; i < 3; i++)
- if (stranice1[i] != stranice2[i])
- return false;
- return true;
- }
- int main () {
- int brTrouglova;
- std::cout << "Koliko zelite kreirati trouglova: ";
- std::cin >> brTrouglova;
- std::vector<std::shared_ptr<Trougao>> trouglovi;
- for (int i = 0; i < brTrouglova; i++) {
- std::cout << "Unesite podatke za " << i + 1 << ". trougao (x1 y1 x2 y2 x3 y3): ";
- Tacka a, b, c;
- double x, y;
- std::cin >> x >> y;
- a = std::make_pair(x, y);
- std::cin >> x >> y;
- b = std::make_pair(x, y);
- std::cin >> x >> y;
- c = std::make_pair(x, y);
- try {
- trouglovi.push_back(std::make_shared<Trougao>(a, b, c));
- } catch (std::domain_error exc) {
- std::cout << exc.what() << ", ponovite unos!" << std::endl;
- i--;
- }
- }
- double dx, dy;
- std::cout << "Unesite vektor translacije (dx dy): ";
- std::cin >> dx >> dy;
- std::transform(trouglovi.begin(), trouglovi.end(), trouglovi.begin(), [dx, dy](std::shared_ptr<Trougao> t) { t->Transliraj(dx, dy); return t; });
- double ugao;
- std::cout << "Unesite ugao rotracije: ";
- std::cin >> ugao;
- std::transform(trouglovi.begin(), trouglovi.end(), trouglovi.begin(), [ugao](std::shared_ptr<Trougao> t) { t->Rotiraj(ugao); return t; });
- double faktorSkaliranja;
- std::cout << "Unesite faktor skaliranja: ";
- std::cin >> faktorSkaliranja;
- std::transform(trouglovi.begin(), trouglovi.end(), trouglovi.begin(), [faktorSkaliranja](std::shared_ptr<Trougao> t) { t->Skaliraj(t->DajTjeme(1), faktorSkaliranja); return t; });
- std::sort(trouglovi.begin(), trouglovi.end(), [](std::shared_ptr<Trougao> t1, std::shared_ptr<Trougao> t2) { return t1->DajPovrsinu() < t2 -> DajPovrsinu(); });
- std::cout << "Trouglovi nakon obavljenih transformacija: " << std::endl;
- std::for_each(trouglovi.begin(), trouglovi.end(), [](std::shared_ptr<Trougao> t) { t->Ispisi(); std::cout << std::endl; });
- auto it = std::min_element(trouglovi.begin(), trouglovi.end(), [](std::shared_ptr<Trougao> t1, std::shared_ptr<Trougao> t2) { return t1->DajObim() < t2->DajObim(); });
- std::cout << "Trougao sa najmanjim obimom: ";
- (*it)->Ispisi();
- // Trougao t({7, 5}, {3, 3}, {3, 9});
- // std::cout << t.DajPovrsinu();
- std::vector<std::pair<Trougao, Trougao>> identicni;
- for (int i = 0; i < trouglovi.size(); i++)
- for (int j = 0; j < trouglovi.size(); j++)
- if (i != j && DaLiSuIdenticni(*trouglovi.at(i), *trouglovi.at(j)))
- identicni.push_back({*trouglovi.at(i), *trouglovi.at(j)});
- std::cout << std::endl << "Parovi identicnih trouglova: " << std::endl;
- for (int i = 0; i < identicni.size(); i++) {
- identicni.at(i).first.Ispisi();
- std::cout << " i ";
- identicni.at(i).second.Ispisi();
- std::cout << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement