Skip to content

Commit

Permalink
triangles, filled and outlined
Browse files Browse the repository at this point in the history
  • Loading branch information
ladyada committed Jul 13, 2011
1 parent 3090da8 commit c97a399
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
68 changes: 68 additions & 0 deletions TFTLCD.cpp
Expand Up @@ -117,6 +117,74 @@ void TFTLCD::drawChar(uint16_t x, uint16_t y, char c,
}
}

// draw a triangle!
void TFTLCD::drawTriangle(uint16_t x0, uint16_t y0,
uint16_t x1, uint16_t y1,
uint16_t x2, uint16_t y2, uint16_t color)
{
drawLine(x0, y0, x1, y1, color);
drawLine(x1, y1, x2, y2, color);
drawLine(x2, y2, x0, y0, color);
}

void TFTLCD::fillTriangle ( int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint16_t color)
{
if (y0 > y1) {
swap(y0, y1); swap(x0, x1);
}
if (y1 > y2) {
swap(y2, y1); swap(x2, x1);
}
if (y0 > y1) {
swap(y0, y1); swap(x0, x1);
}

int32_t dx1, dx2, dx3; // Interpolation deltas
int32_t sx1, sx2, sy; // Scanline co-ordinates

sx2=(int32_t)x0 * (int32_t)1000; // Use fixed point math for x axis values
sx1 = sx2;
sy=y0;

// Calculate interpolation deltas
if (y1-y0 > 0) dx1=((x1-x0)*1000)/(y1-y0);
else dx1=0;
if (y2-y0 > 0) dx2=((x2-x0)*1000)/(y2-y0);
else dx2=0;
if (y2-y1 > 0) dx3=((x2-x1)*1000)/(y2-y1);
else dx3=0;

// Render scanlines (horizontal lines are the fastest rendering method)
if (dx1 > dx2)
{
for(; sy<=y1; sy++, sx1+=dx2, sx2+=dx1)
{
drawHorizontalLine(sx1/1000, sy, (sx2-sx1)/1000, color);
}
sx2 = x1*1000;
sy = y1;
for(; sy<=y2; sy++, sx1+=dx2, sx2+=dx3)
{
drawHorizontalLine(sx1/1000, sy, (sx2-sx1)/1000, color);
}
}
else
{
for(; sy<=y1; sy++, sx1+=dx1, sx2+=dx2)
{
drawHorizontalLine(sx1/1000, sy, (sx2-sx1)/1000, color);
}
sx1 = x1*1000;
sy = y1;
for(; sy<=y2; sy++, sx1+=dx3, sx2+=dx2)
{
drawHorizontalLine(sx1/1000, sy, (sx2-sx1)/1000, color);
}
}
}



// draw a rectangle
void TFTLCD::drawRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h,
uint16_t color) {
Expand Down
9 changes: 8 additions & 1 deletion TFTLCD.h
Expand Up @@ -4,7 +4,7 @@
#include <WProgram.h>

// comment or uncomment the next line for special pinout!
//#define USE_ADAFRUIT_SHIELD_PINOUT
#define USE_ADAFRUIT_SHIELD_PINOUT


// register names from Peter Barrett's Microtouch code
Expand Down Expand Up @@ -75,6 +75,13 @@ class TFTLCD : public Print {
void drawFastLine(uint16_t x0, uint16_t y0, uint16_t l, uint16_t color, uint8_t flag);
void drawVerticalLine(uint16_t x0, uint16_t y0, uint16_t length, uint16_t color);
void drawHorizontalLine(uint16_t x0, uint16_t y0, uint16_t length, uint16_t color);
void drawTriangle(uint16_t x0, uint16_t y0,
uint16_t x1, uint16_t y1,
uint16_t x2, uint16_t y2, uint16_t color);
void fillTriangle(int32_t x0, int32_t y0,
int32_t x1, int32_t y1,
int32_t x2, int32_t y2,
uint16_t color);
void drawRect(uint16_t x0, uint16_t y0, uint16_t w, uint16_t h, uint16_t color);
void fillRect(uint16_t x0, uint16_t y0, uint16_t w, uint16_t h, uint16_t color);
void drawRoundRect(uint16_t x0, uint16_t y0, uint16_t w, uint16_t h, uint16_t radius, uint16_t color);
Expand Down

0 comments on commit c97a399

Please sign in to comment.