Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #include <GLFW/glfw3.h>
  2. #include <iostream>
  3. #include <cmath>
  4.  
  5. void setPixel(int x, int y)
  6. {
  7.     float pixel_x = float(x)/1000.0;
  8.     float pixel_y = float(y)/1000.0;
  9.  
  10.     glBegin(GL_POINTS);
  11.     glVertex2f(pixel_x, pixel_y);
  12.     glEnd();
  13.     glFlush();
  14.  
  15. }
  16.  
  17. void drawBresenhamLine(int x1, int y1, int x2, int y2)
  18. {
  19.     /*
  20.     В переменных x и y хранятся текущие значения координаты точки
  21.     dx , dy - разница по иксу и по игрику
  22.     d = (dy * 2) - dx
  23.     d1 = dy * 2
  24.     d2 = (dy - dx) * 2
  25.     */
  26.     int x, y, dx = x2 - x1, dy = y2 - y1;
  27.     int d = (dy * 2) - dx, d1 = dy * 2, d2 = (dy - dx) * 2;
  28.     x = x1, y = y1;
  29.     setPixel(x1, y1);
  30.     setPixel(x2, y2);
  31.     x2--;
  32.     while (x < x2)
  33.     {
  34.         x++;
  35.         if (d < 0) d += d1;
  36.         else
  37.         {
  38.             y++;
  39.             d += d2;
  40.         }
  41.         setPixel(x, -y);
  42.  
  43.     }
  44. }
  45.  
  46. void drawLine(int x1, int y1, int x2, int y2) {
  47.     const int deltaX = abs(x2 - x1);
  48.     const int deltaY = abs(y2 - y1);
  49.     const int signX = x1 < x2 ? 1 : -1;
  50.     const int signY = y1 < y2 ? 1 : -1;
  51.     //
  52.     int error = deltaX - deltaY;
  53.     //
  54.     setPixel(x2, y2);
  55.     while (x1 != x2 || y1 != y2)
  56.     {
  57.         setPixel(x1, y1);
  58.         const int error2 = error * 2;
  59.         //
  60.         if (error2 > -deltaY)
  61.         {
  62.             error -= deltaY;
  63.             x1 += signX;
  64.         }
  65.         if (error2 < deltaX)
  66.         {
  67.             error += deltaX;
  68.             y1 += signY;
  69.         }
  70.     }
  71.  
  72. }
  73.  
  74.  
  75. void drawCircle(int x0, int y0, int radius) {
  76.     int x = 0;
  77.     int y = radius;
  78.     int delta = 1 - 2 * radius;
  79.     int error = 0;
  80.     while (y >= 0) {
  81.         //setPixel(x0 + x, y0 + y); //1-2
  82.         setPixel(x0 + x, y0 - y); // 7-8
  83.         //setPixel(x0 - x, y0 + y); // 3-4
  84.         //setPixel(x0 - x, y0 - y); // 5-6
  85.         error = 2 * (delta + y) - 1;
  86.         if (delta < 0 && error <= 0) {
  87.             ++x;
  88.             delta += 2 * x + 1;
  89.             continue;
  90.         }
  91.         error = 2 * (delta - x) - 1;
  92.         if (delta > 0 && error > 0) {
  93.             --y;
  94.             delta += 1 - 2 * y;
  95.             continue;
  96.         }
  97.         ++x;
  98.         delta += 2 * (x - y);
  99.         --y;
  100.     }
  101. }
  102.  
  103.  
  104. int main(void)
  105. {
  106.     GLFWwindow* window;
  107.  
  108.     if (!glfwInit())
  109.         return -1;
  110.  
  111.     window = glfwCreateWindow(1000, 1000, "Hello World", NULL, NULL);
  112.     if (!window)
  113.     {
  114.         glfwTerminate();
  115.         return -1;
  116.     }
  117.  
  118.     glfwMakeContextCurrent(window);
  119.  
  120.     while (!glfwWindowShouldClose(window))
  121.     {
  122.         //drawBresenhamLine(0, 0, 1000, 1000);
  123.         drawLine(0, 0, 500, -1000);
  124.  
  125.         drawCircle(0, 0, 500);
  126.         //glClear(GL_COLOR_BUFFER_BIT);
  127.  
  128.         glfwSwapBuffers(window);
  129.  
  130.         glfwPollEvents();
  131.     }
  132.  
  133.     glfwTerminate();
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement