LukacikPavel

bresenham

Oct 5th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1. void Besenham(int x1, int y1, int x2, int y2) {
  2.         boolean zapamataj = false;
  3.         boolean bolaVymena1 = false;
  4.         boolean bolaVymena2 = false;
  5.        
  6.         if (Math.abs(x2 - x1) < Math.abs(y2 - y1)) {
  7.             int pom;
  8.             pom = x1; x1 = y1; y1 = pom;
  9.             pom = x2; x2 = y2; y2 = pom;
  10.             zapamataj = true;
  11.             glColor3d(255, 0, 0);
  12.             bolaVymena1 = true;
  13.         }
  14.        
  15.         if (x2 < x1) {
  16.             int pom;
  17.             pom = x1; x1 = x2; x2 = pom;
  18.             pom = y1; y1 = y2; y2 = pom;
  19.             glColor3d(0, 0, 255);
  20.             bolaVymena2 = true;
  21.         }
  22.        
  23.         if (bolaVymena1 && bolaVymena2) {
  24.             glColor3d(0, 255, 0);
  25.         }
  26.        
  27.         if (!bolaVymena1 && !bolaVymena2) {
  28.             glColor3d(255, 255, 255);
  29.         }
  30.        
  31.         int krok = y2 < y1 ? -1 : 1;
  32.         int dx = x2 - x1;
  33.         int dy = Math.abs(y2 - y1);
  34.         int y = y1;
  35.         int k1 = 2 * dy;
  36.         int k2 = 2 * dy - 2 * dx;
  37.         int d = 2 * dy - dx;
  38.        
  39.         for (int x = x1; x <= x2; x++) {
  40.             if (zapamataj) {
  41.                 Bod(y, x);
  42.             } else {
  43.                 Bod(x, y);
  44.             }
  45.             if (d < 0) {
  46.                 d = d + k1;
  47.             } else {
  48.                 d = d + k2;
  49.                 y = y + krok;
  50.             }
  51.         }
  52.     }
Add Comment
Please, Sign In to add comment