CPP   110
screen init
Guest on 7th February 2023 02:13:03 AM


  1. #include <iostream>
  2. #include <cmath>
  3. #include <graphic.h>
  4. using namespace std;
  5.  
  6. enum color { black='*', white=' ' };
  7.  
  8. char screen[XMAX][YMAX];
  9.  
  10. void screen_init()
  11. {
  12.     for (int y=0; y<YMAX; y++)
  13.         for (int x=0; x<XMAX; x++)
  14.             screen[x][y] = white;
  15. }
  16.  
  17. void screen_destroy() {}
  18.  
  19. inline int on_screen(int a, int b)   // clipping
  20. {
  21.     return 0<=a && a<XMAX && 0<=b && b<YMAX;
  22. }
  23.  
  24. void put_point(int a, int b)
  25. {
  26.     if (on_screen(a, b)) screen[a][b] = black;
  27. }
  28.  
  29. void put_line(int x0, int y0, int x1, int y1)
  30. /*
  31.    Plot the line (x0, y0) to (x1, y1).
  32.    The line being plotted is b(x-x0) +a(y-y0) = 0.
  33.    Minimize abs(eps) where eps = 2*(b(x-x0) + a(y-y0).
  34.    See Newman and Sproull:
  35.    ``Principles of Interactive Computer Graphics''
  36.    McGraw-Hill, New York, 1979.  pp. 33-44.
  37. */
  38. {
  39.    register int dx = 1;
  40.    int a = x1 - x0;
  41.    if (a < 0) dx = -1, a = -a;
  42.  
  43.    register int dy = 1;
  44.    int b = y1 - y0;
  45.    if (b < 0) dy = -1, b = -b;
  46.  
  47.    int two_a = 2*a;
  48.    int two_b = 2*b;
  49.    int xcrit = -b + two_a;
  50.    register int eps = 0;
  51.  
  52.    for(;;) {
  53.       put_point(x0,y0);
  54.       if (x0==x1 && y0 == y1) break;
  55.       if (eps <= xcrit) x0 += dx, eps += two_b;
  56.       if (eps>=a || a<=b) y0 += dy, eps -= two_a;
  57.    }
  58. }
  59.  
  60. void screen_clear() { screen_init(); }
  61.  
  62. void screen_refresh()
  63. {
  64.     for (int y=YMAX-1; 0<=y; y--) {    // top to bottom
  65.         for (int x=0; x<XMAX; x++)      // left to right
  66.             cout << screen[x][y];
  67.         cout << '\n';
  68.     }
  69. }

Raw Paste

Login or Register to edit or fork this paste. It's free.