Bitmap/Bresenham's line algorithm: Difference between revisions

Content added Content deleted
No edit summary
(→‎{{header|C}}: Adding C# Version)
Line 602: Line 602:
if (e2 < dy) { err += dx; y0 += sy; }
if (e2 < dy) { err += dx; y0 += sy; }
}
}
}</lang>

=={{header|C sharp|C#}}==
Port of the C version.
<lang csharp>static class Program
{
static void Main()
{
new Bitmap(200, 200)
.DrawLine(0, 0, 199, 199, Color.Black).DrawLine(199,0,0,199,Color.Black)
.DrawLine(50, 75, 150, 125, Color.Blue).DrawLine(150, 75, 50, 125, Color.Blue)
.Save("line.png", ImageFormat.Png);
}
static Bitmap DrawLine(this Bitmap screen, int x0, int y0, int x1, int y1, Color color)
{
int dx = Math.Abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
int dy = Math.Abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
int err = (dx > dy ? dx : -dy) / 2;
do {
screen.SetPixel(x0, y0, color);
var e2 = err;
if (e2 > -dx) { err -= dy; x0 += sx; }
if (e2 < dy) { err += dx; y0 += sy; }
} while (x0 != x1 || y0 != y1);
screen.SetPixel(x1, y1, color);
return screen;
}
}</lang>
}</lang>