Xom9ik

SRS #3/15var (IV semester) CG

Jun 1st, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.90 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <iostream>
  5. #include <vector>
  6. #include <algorithm>
  7. #include "glut.h"
  8.  
  9. class Vector2
  10. {
  11. public:
  12.     int x;
  13.     int y;
  14.     Vector2() { }
  15.     Vector2(int _x, int _y)
  16.     {
  17.         x = _x;
  18.         y = _y;
  19.     }
  20. };
  21.  
  22. Vector2 CollisionPoint(Vector2 a, Vector2 b, Vector2 c, Vector2 d)
  23. {
  24.     if (((a.y - b.y)*(d.x - c.x) - (c.y - d.y)*(b.x - a.x)) == 0)
  25.     {
  26.         return Vector2(0, 0);
  27.     }
  28.  
  29.     if (d.y > a.y)
  30.     {
  31.         return Vector2(0, 0);
  32.     }
  33.  
  34.     Vector2 T;
  35.     T.x = -((a.x*b.y - b.x*a.y)*(d.x - c.x) - (c.x*d.y - d.x*c.y)*(b.x - a.x)) / ((a.y - b.y)*(d.x - c.x) - (c.y - d.y)*(b.x - a.x));
  36.  
  37.     if ((d.x - c.x) == 0)
  38.     {
  39.         T.y = a.y;
  40.     }
  41.     else
  42.     {
  43.         T.y = ((c.y - d.y)*(-T.x) - (c.x*d.y - d.x*c.y)) / (d.x - c.x);
  44.     }
  45.  
  46.     return T;
  47. }
  48.  
  49. int width = 390, height = 320;
  50. void PutPixel(int x, int y, int* Color) {
  51.     glBegin(GL_POINTS);
  52.     glColor3ub(Color[0], Color[1], Color[2]);
  53.     glVertex2i(x, y);
  54.     glEnd();
  55. }
  56. void DrawLine(int x1, int y1, int x2, int y2, int* Color) {
  57.     int dx, dy, i, e;
  58.     int incx, incy, inc1, inc2;
  59.     int x, y;
  60.     dx = x2 - x1;
  61.     dy = y2 - y1;
  62.     if (dx < 0) dx = -dx;
  63.     if (dy < 0) dy = -dy;
  64.     incx = 1;
  65.     if (x2 < x1) incx = -1;
  66.     incy = 1;
  67.     if (y2 < y1) incy = -1;
  68.     x = x1; y = y1;
  69.     if (dx > dy) {
  70.         PutPixel(x, y, Color);
  71.         e = 2 * dy - dx;
  72.         inc1 = 2 * (dy - dx);
  73.         inc2 = 2 * dy;
  74.         for (i = 0; i<dx; i++) {
  75.             if (e >= 0) {
  76.                 y += incy;
  77.                 e += inc1;
  78.             }
  79.             else
  80.                 e += inc2;
  81.             x += incx;
  82.             PutPixel(x, y, Color);
  83.         }
  84.     }
  85.     else {
  86.         PutPixel(x, y, Color);
  87.         e = 2 * dx - dy;
  88.         inc1 = 2 * (dx - dy);
  89.         inc2 = 2 * dx;
  90.         for (i = 0; i<dy; i++) {
  91.             if (e >= 0) {
  92.                 x += incx;
  93.                 e += inc1;
  94.             }
  95.             else
  96.                 e += inc2;
  97.             y += incy;
  98.             PutPixel(x, y, Color);
  99.         }
  100.     }
  101.  
  102. }
  103. int Color[3] = { 239,202,193 };
  104. int BorderColor[3] = { 209,172,163 };
  105. void Display(void){
  106.     glClear(GL_COLOR_BUFFER_BIT);
  107.     std::vector<Vector2> points;
  108.     points.push_back(Vector2(100, 100));
  109.     points.push_back(Vector2(100, 120));
  110.     points.push_back(Vector2(80, 120));
  111.     points.push_back(Vector2(80, 200));
  112.     points.push_back(Vector2(100, 200));
  113.     points.push_back(Vector2(100, 220));
  114.     points.push_back(Vector2(280, 220));
  115.     points.push_back(Vector2(280, 200));
  116.     points.push_back(Vector2(300, 200));
  117.     points.push_back(Vector2(300, 120));
  118.     points.push_back(Vector2(280, 120));
  119.     points.push_back(Vector2(280, 100));
  120.     points.push_back(Vector2(100, 100));
  121.  
  122.     for (int i = 0; i < points.size() - 1; i++)
  123.     {
  124.         DrawLine(points[i].x, points[i].y, points[i + 1].x, points[i + 1].y, BorderColor);
  125.     }
  126.  
  127.     //1.0 min && max
  128.     int ymin = INT_MAX;
  129.     int ymax = INT_MIN;
  130.     int xmin = INT_MAX;
  131.     int xmax = INT_MIN;
  132.  
  133.     for (int i = 0; i < points.size() - 1; i++)
  134.     {
  135.         if (points[i].y < ymin)
  136.         {
  137.             ymin = points[i].y;
  138.         }
  139.         if (points[i].y > ymax)
  140.         {
  141.             ymax = points[i].y;
  142.         }
  143.         if (points[i].x < xmin)
  144.         {
  145.             xmin = points[i].x;
  146.         }
  147.         if (points[i].x > xmax)
  148.         {
  149.             xmax = points[i].x;
  150.         }
  151.     }
  152.  
  153.     Vector2 collision;
  154.     Vector2 sourcePoint;
  155.  
  156.     for (int y = ymin + 1; y < ymax; y++)
  157.     {
  158.         for (int i = 0; i < points.size() - 1; i++)
  159.         {
  160.             if (points[i].y >= y)
  161.             {
  162.                 continue;
  163.             }
  164.  
  165.             if (points[i + 1].x > points[i].x)
  166.             {
  167.                 continue;
  168.             }
  169.  
  170.             for (int j = i; j < points.size() - 1; j++)
  171.             {
  172.                 sourcePoint = CollisionPoint(Vector2(-1000, y), Vector2(1000, y), points[i+1], points[i]);
  173.  
  174.                 if (sourcePoint.x == 0 && sourcePoint.y == 0 || sourcePoint.x > xmax || sourcePoint.x < xmin || sourcePoint.x < points[i].x)
  175.                 {
  176.                     continue;
  177.                 }
  178.  
  179.                 collision = CollisionPoint(sourcePoint, Vector2(sourcePoint.x + 1000, y), points[j], points[j + 1]);
  180.  
  181.                 if (collision.x == 0 && collision.y == 0
  182. || collision.x > xmax || collision.x < xmin
  183. || collision.x < points[i].x)
  184.                 {
  185.                     continue;
  186.                 }
  187.  
  188. /*if ((collision.x == 0 && collision.y == 0
  189. || !(collision.x > xmax || collision.x < xmin
  190. || collision.x < points[i].x)))
  191.                 {
  192.                     continue;
  193.                 }*/
  194.                
  195.                 DrawLine(points[i].x + 1, y, collision.x - 1, y, Color);
  196.             }
  197.         }
  198.  
  199.         glFlush();
  200.     }
  201. }
  202. void Display2(void){
  203.     glClear(GL_COLOR_BUFFER_BIT);
  204.     std::vector<Vector2> points;
  205.     points.push_back(Vector2(100, 100));
  206.     points.push_back(Vector2(100, 120));
  207.     points.push_back(Vector2(80, 120));
  208.     points.push_back(Vector2(80, 200));
  209.     points.push_back(Vector2(100, 200));
  210.     points.push_back(Vector2(100, 220));
  211.     points.push_back(Vector2(280, 220));
  212.     points.push_back(Vector2(280, 200));
  213.     points.push_back(Vector2(300, 200));
  214.     points.push_back(Vector2(300, 120));
  215.     points.push_back(Vector2(280, 120));
  216.     points.push_back(Vector2(280, 100));
  217.     points.push_back(Vector2(100, 100));
  218.  
  219.     for (int i = 0; i < points.size() - 1; i++)
  220.     {
  221.         DrawLine(points[i].x, points[i].y, points[i + 1].x, points[i + 1].y, BorderColor);
  222.     }
  223.    
  224.     //1.0 min && max
  225.     int ymin = INT_MAX;
  226.     int ymax = INT_MIN;
  227.     for (int i = 0; i < points.size(); i++)
  228.     {
  229.         if (points[i].y < ymin)
  230.             ymin = points[i].y;
  231.         if (points[i].y > ymax)
  232.             ymax = points[i].y;
  233.     }
  234.  
  235.     //2.0 for ymin->ymax
  236.     for (int y = ymin+1; y < ymax; y+=1)
  237.     {
  238.         //2.1 calc
  239.         std::vector<int> xk;
  240.         for (int i = 0; i < points.size() - 3; i++)
  241.         {
  242.             int xn = points[i].x;
  243.             int one = (points[i + 1].x - points[i].x);
  244.             int two = (y - points[i].y);
  245.    
  246.             int dividend = abs(one*two);
  247.             int divider = points[i + 1].y - points[i].y;
  248.  
  249.             if (divider != 0)
  250.             {
  251.                 xk.push_back(xn + (dividend / divider));
  252.             }
  253.         }
  254.  
  255.         //2.2 sort
  256.         std::sort(xk.begin(), xk.end());
  257.  
  258.         //2.3 draw
  259.         for (int i = 0; i < xk.size()-1; i++)
  260.         {
  261.             DrawLine(xk[i], y, xk[i + 1], y, Color);   
  262.         }
  263.     }
  264.     glFlush();
  265. }
  266. void Init(){
  267.     glClearColor(1.0f, 0.941f, 0.913f, 1.0f);
  268.     gluOrtho2D(0, width, 0, height);
  269. }
  270. void main(int argc, char **argv){
  271.     glutInit(&argc, argv);
  272.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  273.     glutInitWindowPosition(0, 0);
  274.     glutInitWindowSize(width, height);
  275.     glutCreateWindow("SRS#3 Fill");
  276.     Init();
  277.     glutDisplayFunc(Display);
  278.     glutMainLoop();
  279. }
Advertisement
Add Comment
Please, Sign In to add comment