Advertisement
999ms

Computer Graphics lab1 v.1.1

Oct 17th, 2019
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.01 KB | None | 0 0
  1. #include "Graphics.h"
  2. #include "StdAfx.h"
  3. #include "GF.h"
  4. #include <functional>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <bitset>
  8. #include <cstdlib>
  9. #include <cstring>
  10. #include <string>
  11. #include <string.h>
  12.  
  13. #define all(x) (x).begin(),(x).end()
  14.  
  15. #ifndef M_PI
  16. const double M_PI = 3.1415926535897932384626433832795;
  17. #endif
  18.  
  19. class TPoint {
  20. public:
  21.     int x, y;
  22.     TPoint(int x = 0, int y = 0)
  23.         : x(x)
  24.         , y(y)
  25.     {
  26.     }
  27.  
  28.     TPoint(const TPoint& a, const TPoint& b)
  29.         : x(b.x - a.x)
  30.         , y(b.y - a.y)
  31.     {
  32.     }
  33.    
  34.     double Length() {
  35.         return hypot(x, y);
  36.     }
  37. private:
  38.    
  39. };
  40.  
  41. using TPolygon = std::vector<TPoint>;
  42.  
  43. inline void DrawLine(int x0, int y0, int x1, int y1, const RGBPIXEL& color) {
  44.     const int deltaX = abs(x0 - x1);
  45.     const int deltaY = abs(y0 - y1);
  46.     const int signX = x0 < x1 ? 1 : -1;
  47.     const int signY = y0 < y1 ? 1 : -1;
  48.     int d = deltaX - deltaY;
  49.     gfSetPixel(x1, y1, color);
  50.     while (x0 != x1 || y0 != y1) {
  51.         gfSetPixel(x0, y0, color);
  52.         const int d2 = d << 1;
  53.         if (d2 > -deltaY) { // d > -dy / 2
  54.             d -= deltaY;
  55.             x0 += signX;
  56.         }
  57.         if (d2 < deltaX) {
  58.             d += deltaX;
  59.             y0 += signY;
  60.         }
  61.     }
  62. }
  63. inline void DrawLine(TPoint a, TPoint b, const RGBPIXEL& color) {
  64.     DrawLine(a.x, a.y, b.x, b.y, color);
  65. }
  66.  
  67. void DrawWithRule(TPoint minPoint, TPoint maxPoint, const RGBPIXEL& color, const std::function<bool(TPoint)>& func) {
  68.     for (int x = minPoint.x; x <= maxPoint.x; x++) {
  69.         for (int y = minPoint.y; y <= maxPoint.y; y++) {
  70.             if (func({x, y})) {
  71.                 gfSetPixelNoCheck(x, y, color);
  72.             }
  73.         }
  74.     }
  75. }
  76.  
  77. void DrawPolygon(const TPolygon& polygon, const RGBPIXEL& color) {
  78.     int n = polygon.size();
  79.     for (int i = 0; i < n; i++) {
  80.         DrawLine(polygon[i].x, polygon[i].y, polygon[(i + 1) % n].x, polygon[(i + 1) % n].y, color);
  81.     }
  82. }
  83.  
  84. TPolygon GetRandomPolygon(int n, const int& WINDOW_SIZE) {
  85.     TPolygon result(n);
  86.     for (int i = 0; i < n; i++) {
  87.         result[i].x = rand() % WINDOW_SIZE;
  88.         result[i].y = rand() % WINDOW_SIZE;
  89.     }
  90.     return result;
  91. }
  92.  
  93. inline bool cw (const TPoint& a, const TPoint& b, const TPoint& c) { // clockwise
  94.     return a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y) < 0;
  95. }
  96.  
  97. inline bool ccw (const TPoint& a, const TPoint& b, const TPoint& c) { // counter clockwise
  98.     return a.x * (b.y - c.y) + b.x * (c.y - a.y) + c.x * (a.y - b.y) > 0;
  99. }
  100.  
  101. inline int Area (const TPoint& a, const TPoint& b, const TPoint& c) {
  102.     return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
  103. }
  104.  
  105.  
  106. inline int Area (const TPoint& a, const TPoint& b) {
  107.     return a.x * b.y - a.y * b.x;
  108. }
  109.  
  110. inline bool Intersect_1 (int a, int b, int c, int d) {
  111.     if (a > b)  std::swap (a, b);
  112.     if (c > d)  std::swap (c, d);
  113.     return max(a,c) <= min(b,d);
  114. }
  115.  
  116. bool Intersect (const TPoint& a, const TPoint& b, const TPoint& c, const TPoint& d) {
  117.     return Intersect_1 (a.x, b.x, c.x, d.x)
  118.            && Intersect_1 (a.y, b.y, c.y, d.y)
  119.            && Area(a,b,c) * 1ll * Area(a,b,d) <= 0
  120.            && Area(c,d,a) * 1ll * Area(c,d,b) <= 0;
  121. }
  122.  
  123. inline bool ContainsInTriangle(const TPoint& a, const TPoint& b, const TPoint& c, const TPoint& x) {
  124.     return max(max(cw(b, a, x), cw(x, a, c)), max(cw(a, c, x), max(cw(x, c, b), max(cw(c, b, x), cw(x, b, a))))) <= 0;
  125. }
  126.  
  127. inline bool PointInConvexPolygon(const TPolygon& p, TPoint x) {
  128.     for (int i = 1; i + 1 < p.size(); i++) {
  129.         if (ContainsInTriangle(p[0], p[i], p[i + 1], x)) {
  130.             return true;
  131.         }
  132.     }
  133.     return false;
  134. }
  135.  
  136. inline RGBPIXEL RandColor() {
  137.     return RGBPIXEL(rand() % 256, rand() % 256, rand() % 256);
  138. }
  139.  
  140. bool PointInPolygonWithEvenOddRule(const TPolygon& p, const TPoint& a, const int WINDOW_SIZE) {
  141.     char counter = 0;
  142.     TPoint b = {a.x, a.y - WINDOW_SIZE};
  143.     bool pre = false;
  144.     bool cur = false;
  145.     const int n = p.size();
  146.     for (int i = 0; i < n; i++) {
  147.         cur = Intersect(p[i], p[(i + 1) % n], a, b);
  148.         if (cur && pre && p[i].x == a.x) {
  149.             if ((p[(i - 1 + n) % n].x > a.x) ^ (p[(i + 1) % n].x > a.x)) {
  150.                 continue;
  151.             }
  152.         }
  153.         counter ^= cur;
  154.         pre = cur;
  155.     }
  156.     return counter;
  157. }
  158.  
  159. bool PointInPolygonWithNonZeroWindingRule(const TPolygon& p, const TPoint& a, const int WINDOW_SIZE) {
  160.     int counter = 0;
  161.     TPoint b = {a.x, a.y - WINDOW_SIZE};
  162.     bool pref = false;
  163.     bool cur = false;
  164.     const int n = p.size();
  165.     for (int i = 0; i < n; i++) {
  166.         if (cur = Intersect(p[i], p[(i + 1) % n], a, b)) {
  167.             if (cur && pref && p[i].x == a.x) {
  168.                 if ((p[(i - 1 + n) % n].x > a.x) ^ (p[(i + 1) % n].x > a.x)) {
  169.                     continue;
  170.                 }
  171.             }
  172.             if (Area(p[i], p[(i + 1) % n], a) >= 0) {
  173.                 counter++;
  174.             } else {
  175.                 counter--;
  176.             }
  177.         }
  178.         pref = cur;
  179.     }
  180.     return counter;
  181. }
  182.  
  183. const TCHAR* GetTextForDraw(const std::string s) {
  184.     char* answer = new char[s.size() + 1];
  185.     for (int i = 0; i < s.size(); i++) {
  186.         answer[i] = s[i];
  187.     }
  188.     answer[s.size()] = '\0';
  189.     return answer;
  190. }
  191.  
  192. template<typename T>
  193. const TCHAR* GetTextForDraw(const T& value) {
  194.     return GetTextForDraw(std::to_string(value));
  195. }
  196.  
  197. bool IsComplex(const TPolygon& polygon) {
  198.     const int n = polygon.size();
  199.     if (n <= 3) {
  200.         return false;
  201.     }
  202.     for (int i = 0; i < n; i++) {
  203.         for (int j = i + 2; j < n; j++) {
  204.             if (j == n - 1 && i == 0) continue;
  205.             if (Intersect(polygon[i], polygon[(i + 1) % n], polygon[j], polygon[(j + 1) % n])) {
  206.                 return true;
  207.             }
  208.         }
  209.     }
  210.     return false;
  211. }
  212.  
  213. bool IsConvex(const TPolygon& polygon) {
  214.     const int n = polygon.size();
  215.     if (n <= 3) {
  216.         return true;
  217.     }
  218.     if (IsComplex(polygon)) {
  219.         return false;
  220.     }
  221.     double pos = 0;
  222.     double neg = 0;
  223.     for (int i = 0; i < n; i++) {
  224.         const double s = Area(polygon[(i - 1 + n) % n], polygon[i], polygon[(i + 1) % n]);
  225.         if (s > 0) {
  226.             pos++;
  227.         }
  228.         if (s < 0) {
  229.             neg++;
  230.         }
  231.         if (pos > 0 && neg > 0) {
  232.             return false;
  233.         }
  234.     }
  235.     if (pos > 0 && neg > 0) {
  236.         return false;
  237.     }
  238.     return true;
  239. }
  240.  
  241. const char CONVEX = 1;
  242. const char COMPLEX = 2;
  243. const char NONCONVEX = 3;
  244.  
  245. char Classify(const TPolygon& polygon) {
  246.     const int n = polygon.size();
  247.     if (IsConvex(polygon)) {
  248.         return CONVEX;
  249.     } else if (IsComplex(polygon)) {
  250.         return COMPLEX;
  251.     } else {
  252.         return NONCONVEX;
  253.     }
  254. }
  255.  
  256. std::pair<TPoint, TPoint> MinMaxBorders(const TPolygon& polygon) {
  257.     const int inf = 1 << 30;
  258.     TPoint minPoint = {inf, inf};
  259.     TPoint maxPoint = {-inf, -inf};
  260.     for (const auto& p : polygon) {
  261.         minPoint.x = min(minPoint.x, p.x);
  262.         minPoint.y = min(minPoint.y, p.y);
  263.         maxPoint.x = max(maxPoint.x, p.x);
  264.         maxPoint.y = max(maxPoint.y, p.y);
  265.     }
  266.     return {minPoint, maxPoint};
  267. }
  268.  
  269.  
  270. bool gfInitScene() {
  271.     try {
  272.         const int WINDOW_SIZE = 600;
  273.         gfSetWindowSize(WINDOW_SIZE, WINDOW_SIZE);
  274.         auto p = GetRandomPolygon(10000, WINDOW_SIZE - 1);
  275.         DrawPolygon(p, RGBPIXEL::White());
  276.     } catch (...) {
  277.         return false;
  278.     }
  279.     return true;
  280. }
  281.  
  282. void gfDrawScene()
  283. {
  284.     //gfClearScreen(RGBPIXEL::Black());
  285.     //int x = gfGetMouseX(), y = gfGetMouseY();
  286.     //gfDrawRectangle(x - 10, y - 10, x + 10, y + 10, RGBPIXEL::Green());
  287. }
  288.  
  289. void gfCleanupScene()
  290. {
  291.  
  292. }
  293.  
  294. void gfOnLMouseClick( int x, int y )
  295. {
  296.  
  297. }
  298.  
  299. void gfOnRMouseClick( int x, int y )
  300. {
  301. }
  302.  
  303. void gfOnKeyDown( UINT key ) {
  304.    
  305. }
  306.  
  307. void gfOnKeyUp( UINT key )
  308. {
  309. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement