In this tip, you will see code that shows a C# pseudo code implementation of the triangle drawing routine.
Introduction
I've been looking for a quick way to draw a triangle on a bitmap by pixel plotting. This is especially interesting in combination with the WriteableBitmap
in Silverlight.
The following code shows a C# pseudo code implementation of the triangle drawing routine. Given a bitmap, the array of pixels (integers), then the following short piece of code will draw the bitmap efficiently. It also makes use of a swap
function that swaps 2 variables (integers, not included).
Note that the color
parameter is assumed ARGB encoded and should be represented as an integer.
public static void FillTriangleSimple
(this WriteableBitmap bitmap, int x0, int y0, int x1, int y1, int x2, int y2, int color)
{
int[] pixels = bitmap.Pixels;
int width = bitmap.PixelWidth;
int height = bitmap.PixelHeight;
if (y1 > y2)
{
swap(ref x1, ref x2);
swap(ref y1, ref y2);
}
if (y0 > y1)
{
swap(ref x0, ref x1);
swap(ref y0, ref y1);
}
if (y1 > y2)
{
swap(ref x1, ref x2);
swap(ref y1, ref y2);
}
double dx_far = Convert.ToDouble(x2 - x0) / (y2 - y0 + 1);
double dx_upper = Convert.ToDouble(x1 - x0) / (y1 - y0 + 1);
double dx_low = Convert.ToDouble(x2 - x1) / (y2 - y1 + 1);
double xf = x0;
double xt = x0 + dx_upper;
for (int y = y0; y <= (y2 > height-1 ? height-1 : y2); y++)
{
if (y >= 0)
{
for (int x = (xf > 0 ? Convert.ToInt32(xf) : 0);
x <= (xt < width ? xt : width-1) ; x++)
pixels[Convert.ToInt32(x + y * width)] = color;
for (int x = (xf < width ? Convert.ToInt32(xf) : width-1);
x >= (xt > 0 ? xt : 0); x--)
pixels[Convert.ToInt32(x + y * width)] = color;
}
xf += dx_far;
if (y < y1)
xt += dx_upper;
else
xt += dx_low;
}
}
P.S.: Fixed small bug
History
- 8th June, 2010: Initial version