Facebook
From ff, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 209
  1.         setPixel(firstX, firstY, R, G, B); /// rysujemy punkt startowy (firstX, firstY)
  2.         setPixel(lastX, lastY, R, G, B); /// rysujemy punkt koncowy (firstX, firstY)
  3.  
  4.         const int dx = abs(lastX - firstX);
  5.         const int dy = abs(lastY - firstY);
  6.  
  7.  
  8.         const int stepX = (lastX > firstX) ? 1 : -1;
  9.         const int stepY = (lastY > firstY) ? 1 : -1;
  10.  
  11.         if (abs(dx) > abs(dy)) {
  12.                 int d1 = 2 * dy - dx;
  13.                 int di = d1;
  14.  
  15.                 const int di_ge0_step = 2 * (dy - dx);
  16.                 const int di_lt0_step = 2 * dy;
  17.  
  18.                 for (int xi = firstX, yi = firstY; xi != lastX; xi += stepX) {
  19.                         if (di >= 0) {
  20.                                 di += di_ge0_step;
  21.                                 yi += stepY;
  22.                                 const int Ti[] = { xi, yi };
  23.                                 setPixel(Ti[0], Ti[1], R, G, B);
  24.                         }
  25.                         else {
  26.                                 di += di_lt0_step;
  27.                                 const int Si[] = { xi, yi };
  28.                                 setPixel(Si[0], Si[1], R, G, B);
  29.                         }
  30.                 }
  31.         }
  32.         else {
  33.                 const int d1 = 2 * dx - dy;
  34.                 int di = d1;
  35.  
  36.                 const int di_ge0_step = 2 * (dx - dy);
  37.                 const int di_lt0_step = 2 * dx;
  38.  
  39.                 for (int xi = firstX, yi = firstY; yi != lastY; yi += stepY) {
  40.                         if (di >= 0) {
  41.                                 di += di_ge0_step;
  42.                                 xi += stepX;
  43.                                 const int Ti[] = { xi, yi };
  44.                                 setPixel(Ti[0], Ti[1], R, G, B);
  45.                         }
  46.                         else {
  47.                                 di += di_lt0_step;
  48.                                 const int Si[] = { xi, yi };
  49.                                 setPixel(Si[0], Si[1], R, G, B);
  50.                         }
  51.                 }
  52.         }