Click here to Skip to main content
13,832,323 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 8 Jun 2010
Licenced CPOL

draw triangle algorithm (2D)

, 8 Jun 2010
Rate this:
Please Sign up or sign in to vote.
short and efficient algorithm for pixel plotting a 2D triangle on a bitmap
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;
    // sort the points vertically
    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; // if y0 == y1, special case
    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;
            xt += dx_low;

ps. fixed small bug


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Herre Kuijpers
Architect Rubicon
Netherlands Netherlands
Currently Herre Kuijpers is employed at Rubicon. During his career he developed skills with all kinds of technologies, methodologies and programming languages such as c#, ASP.Net, .Net Core, VC++, Javascript, SQL, Agile, Scrum, DevOps, ALM. Currently he fulfills the role of software architect in various projects.

Herre Kuijpers is a very experienced software architect with deep knowledge of software design and development on the Microsoft .Net platform. He has a broad knowledge of Microsoft products and knows how these, in combination with custom software, can be optimally implemented in the often complex environment of the customer.

You may also be interested in...

Comments and Discussions

BugDivide By Zero Pin
Member 253099719-Nov-14 23:20
memberMember 253099719-Nov-14 23:20 
GeneralRe: Divide By Zero Pin
Herre Kuijpers19-Nov-14 23:46
memberHerre Kuijpers19-Nov-14 23:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.190114.1 | Last Updated 8 Jun 2010
Article Copyright 2010 by Herre Kuijpers
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid