From eb78fc66930abc70383a511c2ac8c5585f60a447 Mon Sep 17 00:00:00 2001 From: ladyada Date: Thu, 15 Mar 2012 16:23:38 -0400 Subject: [PATCH] triangles! --- Adafruit_GFX.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) diff --git a/Adafruit_GFX.cpp b/Adafruit_GFX.cpp index fef2211..40a5123 100644 --- a/Adafruit_GFX.cpp +++ b/Adafruit_GFX.cpp @@ -226,7 +226,7 @@ uint16_t h, uint16_t r, uint16_t color) { // fill a rounded rectangle! void Adafruit_GFX::fillRoundRect(uint16_t x, uint16_t y, uint16_t w, -uint16_t h, uint16_t r, uint16_t color) { + uint16_t h, uint16_t r, uint16_t color) { // smarter version fillRect(x+r, y, w-2*r, h, color); @@ -235,6 +235,92 @@ uint16_t h, uint16_t r, uint16_t color) { fillCircleHelper(x+r , y+r, r, 2, h-2*r-1, color); } +// draw a triangle! +void Adafruit_GFX::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); +} + +// fill a triangle! +void Adafruit_GFX::fillTriangle ( uint16_t x0, uint16_t y0, + uint16_t x1, uint16_t y1, + uint16_t x2, uint16_t y2, uint16_t color) { + + int16_t a, b, y, last; + + // Sort coordinates by Y order (y2 >= y1 >= y0) + 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); + } + + if(y0 == y2) { // Handle awkward all-on-same-line case as its own thing + a = b = x0; + if(x1 < a) a = x1; + else if(x1 > b) b = x1; + if(x2 < a) a = x2; + else if(x2 > b) b = x2; + drawFastHLine(a, y0, b-a+1, color); + return; + } + + int16_t + dx01 = x1 - x0, + dy01 = y1 - y0, + dx02 = x2 - x0, + dy02 = y2 - y0, + dx12 = x2 - x1, + dy12 = y2 - y1, + sa = 0, + sb = 0; + + // For upper part of triangle, find scanline crossings for segments + // 0-1 and 0-2. If y1=y2 (flat-bottomed triangle), the scanline y1 + // is included here (and second loop will be skipped, avoiding a /0 + // error there), otherwise scanline y1 is skipped here and handled + // in the second loop...which also avoids a /0 error here if y0=y1 + // (flat-topped triangle). + if(y1 == y2) last = y1; // Include y1 scanline + else last = y1-1; // Skip it + + for(y=y0; y<=last; y++) { + a = x0 + sa / dy01; + b = x0 + sb / dy02; + sa += dx01; + sb += dx02; + /* longhand: + a = x0 + (x1 - x0) * (y - y0) / (y1 - y0); + b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); + */ + if(a > b) swap(a,b); + drawFastHLine(a, y, b-a+1, color); + } + + // For lower part of triangle, find scanline crossings for segments + // 0-2 and 1-2. This loop is skipped if y1=y2. + sa = dx12 * (y - y1); + sb = dx02 * (y - y0); + for(; y<=y2; y++) { + a = x1 + sa / dy12; + b = x0 + sb / dy02; + sa += dx12; + sb += dx02; + /* longhand: + a = x1 + (x2 - x1) * (y - y1) / (y2 - y1); + b = x0 + (x2 - x0) * (y - y0) / (y2 - y0); + */ + if(a > b) swap(a,b); + drawFastHLine(a, y, b-a+1, color); + } +} void Adafruit_GFX::drawBitmap(uint16_t x, uint16_t y, const uint8_t *bitmap, uint16_t w, uint16_t h,