Last active
January 23, 2017 15:07
-
-
Save esmitt/a5ec509d085b0a32c163392d95a61f59 to your computer and use it in GitHub Desktop.
Algorithm of the middle point (2D Bresenham) to draw a line, where points are printing in console.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Algorithm of the middle point, where points are printing in console | |
/// Works independently of the position of both points. Also is more easy using Point from Drawing. | |
/// </summary> | |
/// <param name="x1">X value of the 1st point</param> | |
/// <param name="y1">Y value of the 1st point</param> | |
/// <param name="x2">X value of the 2nd point</param> | |
/// <param name="y2">Y value of the 2nd point</param> | |
static void Bresenham(int x1, int y1, int x2, int y2) | |
{ | |
int incCase1 = 0, incCase2 = 0; | |
int x, y, addX1 = 0, addY1 = 0, addX2 = 0, addY2 = 0, dx, dy; | |
int d = 0; | |
if (x1 > x2) //swapping in case of 1st is bigger than 2nd | |
{ | |
int tx, ty; | |
tx = x1; //x1 ^= x2; x2 ^= x1; x1 ^= x2; | |
x1 = x2; | |
x2 = tx; | |
ty = y1; //y1 ^= y2; y2 ^= y1; y1 ^= y2; | |
y1 = y2; | |
y2 = ty; | |
} | |
dx = x1 - x2; dy = y1 - y2; | |
x = x1; y = y1; | |
if (dy == 0) //horizontal line | |
{ | |
for (int i = x; i <= x2; i++) | |
Console.WriteLine("("+i+","+y+")"); | |
return; | |
} | |
if (dx == 0) //vertical line | |
{ | |
for (int i = Math.Min(y1, y2); i <= Math.Max(y1, y2); i++) | |
Console.WriteLine("(" + x + "," + i + ")"); | |
return; | |
} | |
//slope between [0 - 1] | |
if ((dy >= dx) && (dy <= 0)) | |
{ | |
incCase1 = dy * 2; // dy << 1; | |
incCase2 = incCase1 - (dx * 2); //(dx << 1); | |
d = incCase1 - dx; | |
addX1 = 1; | |
addX2 = 1; | |
addY2 = 1; | |
} | |
//slope 1 - +infi | |
else if (dx > dy) | |
{ | |
incCase2 = -dx * 2;// dx << 1; | |
incCase1 = dy * 2 + incCase2; // (dy << 1) + incCaso2; | |
d = dy * 2 - dx; //(dy << 1) - dx; | |
addX1 = 1; | |
addY1 = 1; | |
addY2 = 1; | |
} | |
//slope 0 - -1 | |
else if ((dy > 0) && (-dx >= dy)) | |
{ | |
incCase2 = dy * 2; // dy << 1; | |
incCase1 = incCase2 + dx * 2; // (dx << 1); | |
d = incCase2 + dx; | |
addX1 = 1; | |
addY1--; | |
addX2 = 1; | |
} | |
//slope between -inf - -1 | |
else if (-dx < dy) | |
{ | |
incCase1 = dx * 2; //dx << 1; | |
incCase2 = (dy * 2) + incCase1; ; // (dy << 1) + incCase1; | |
d = (dy * 2) + dx; // (dy << 1) + dx; | |
addY1--; | |
addX2 = 1; | |
addY2--; | |
} | |
//any line | |
while (x < x2) | |
{ | |
Console.WriteLine("(" + x + "," + y + ")"); | |
if (d >= 0) | |
{ | |
d += incCase1; | |
x += addX1; | |
y += addY1; | |
} | |
else | |
{ | |
d += incCase2; | |
x += addX2; | |
y += addY2; | |
} | |
} | |
Console.WriteLine("(" + x2 + "," + y2 + ")"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment