Advertisement
AdrianMadajewski

Untitled

Feb 5th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <cmath>
  5. #include <algorithm>
  6.  
  7. struct Punkt { double x, y; };
  8. struct Okrag { int x, y, r; };
  9.  
  10. bool styczny(Okrag o)
  11. {
  12.     return abs(o.y) == o.r;
  13. }
  14.  
  15. void sortowanie(std::vector<Okrag> &v)
  16. {
  17.     std::sort(v.begin(), v.end(), [](Okrag o1, Okrag o2) { if (o1.x == o2.x) return o2.y <= o1.y;
  18.     else return o1.x <= o2.x; });
  19. }
  20.  
  21.  
  22. void sortowanie_p(std::vector<Punkt> &v)
  23. {
  24.     std::sort(v.begin(), v.end(), [](Punkt o1, Punkt o2) { if (o1.x == o2.x) return o2.y <= o1.y;
  25.     else return o1.x <= o2.x; });
  26. }
  27.  
  28. double pole(Punkt a, Punkt b, Punkt c)
  29. {
  30.     // P = 1/2 * |xA*yB + xB*yC + xC*yA - xC*yB - xA*yC - xB*yA
  31.     return 1.0f / 2.0f * abs(a.x * b.y + b.x * c.y + c.x * a.y - c.x * b.y - a.x * c.y - b.x * a.y);
  32. }
  33.  
  34. int main()
  35. {
  36.     std::vector<Punkt> punkty;
  37.     std::vector<Okrag> okregi;
  38.    
  39.     std::ifstream dane1("punkty.txt");
  40.     std::ifstream dane2("okregi.txt");
  41.  
  42.     while (!dane1.eof())
  43.     {
  44.         Punkt p;
  45.         dane1 >> p.x >> p.y;
  46.  
  47.         punkty.emplace_back(p);
  48.     }
  49.  
  50.     while (!dane2.eof())
  51.     {
  52.         Okrag o;
  53.         dane2 >> o.x >> o.y >> o.r;
  54.  
  55.         okregi.emplace_back(o);
  56.     }
  57.  
  58.     // 4.1
  59.     int c1, c2, c3, c4;
  60.     c1 = c2 = c3 = c4 = 0;
  61.     for (auto &punkty : punkty)
  62.     {
  63.         if (punkty.x > 0 && punkty.y > 0) c1++;
  64.         if (punkty.x < 0 && punkty.y > 0) c2++;
  65.         if (punkty.x < 0 && punkty.y < 0) c3++;
  66.         if (punkty.x > 0 && punkty.y < 0) c4++;
  67.     }
  68.  
  69.     std::ofstream w1("wynik1.txt");
  70.     w1 << c1 << " " << c2 << " " << c3 << " " << c4;
  71.  
  72.     // 4.2
  73.     std::vector<Okrag> styczne;
  74.    
  75.     for (auto& okrag : okregi)
  76.         if (styczny(okrag))
  77.             styczne.emplace_back(okrag);
  78.  
  79.     sortowanie(styczne);
  80.  
  81.     std::ofstream w2("wynik2.txt");
  82.     for (auto &okrag : styczne)
  83.         w2 << okrag.x << " " << okrag.y << " " << okrag.r << std::endl;
  84.     w2 << styczne.size();
  85.  
  86.     // 4.3
  87.     // Sortowanie zeby moc obliczyc pole
  88.     sortowanie_p(punkty);
  89.  
  90.     double polec = 0.0f;
  91.     for (int i = 0; i < punkty.size() - 3; i++)
  92.         polec += pole(punkty[i], punkty[i + 1], punkty[i + 2]);
  93.  
  94.     std::ofstream w3("wynik3.txt");
  95.     w3 << (int) polec;
  96.  
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement