Advertisement
Rafpast

successful line-making according to the Bresenham method

Aug 13th, 2021
2,944
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.70 KB | None | 0 0
  1. void lineDrawing()
  2. {
  3.   int xIncrease = mouseOldCordX < mouseNewCordX ? 1 : -1;  
  4.   int yIncrease = mouseOldCordY < mouseNewCordY ? 1 : -1;
  5.   int deltaX =  abs(mouseNewCordX - mouseOldCordX);
  6.   int deltaY = -abs(mouseNewCordY - mouseOldCordY);
  7.   int error = deltaX + deltaY;
  8.  
  9.   while (true)
  10.   {
  11.     world[mouseOldCordX][mouseOldCordY].state = selectedState;
  12.  
  13.     if ((mouseOldCordX == mouseNewCordX) && (mouseOldCordY == mouseNewCordY)) break;
  14.  
  15.     int doubleError = 2 * error;
  16.  
  17.     if (doubleError >= deltaY)
  18.     {
  19.       error += deltaY;
  20.       mouseOldCordX += xIncrease;
  21.     }
  22.  
  23.     if (doubleError <= deltaX)
  24.     {
  25.       error += deltaX;
  26.       mouseOldCordY += yIncrease;
  27.     }
  28.   }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement