Algorithm to draw filled triangle

by ganzziani on Mon Sep 30, 2013 9:08 pm

I was looking for an algorithm to draw filled triangles, I found one from Adafruit's Graphics library, but I kept on looking for something more efficient. I found some code from http://www.sunshine2k.de/, it is based on the Bresenham's line algorithm and I optimized a bit further.

I have found that the Bresenham's algorithm, besides being more efficient (at least on a microcontroller), draws the triangles more accurately, particularly when drawing sharp edges.

Attached is the code for anyone interested.

Perhaps the Adafruit team can test the algorithm and check if it is any good and consider adding it to the library...
Attachments
triangles.c
(5.84 KiB) Downloaded 22 times
Last edited by ganzziani on Tue Oct 01, 2013 11:16 pm, edited 1 time in total.
User avatar
ganzziani
 
Posts: 13
Joined: Thu Oct 07, 2010 1:18 pm
Location: Florida

Re: Algorithm to draw filled triangle

by adafruit_support_bill on Tue Oct 01, 2013 4:38 am

Thanks for posting. I'll forward this to the team.
User avatar
adafruit_support_bill
 
Posts: 28903
Joined: Sat Feb 07, 2009 9:11 am

Re: Algorithm to draw filled triangle

by ganzziani on Tue Oct 01, 2013 11:26 pm

I should have posted a day later...
I 've been using the triangle function and found that sometimes there were some tiny gaps. I have fixed it, and updated the file.

I've tested it on a standard PC compiler, and it draws the triangles very well.

I've ran some benchmarks on an AVR and it still more efficient than the slope method (although not by much).

Below a comparison:
Image
User avatar
ganzziani
 
Posts: 13
Joined: Thu Oct 07, 2010 1:18 pm
Location: Florida