UniGraphics Library Fork to support mbed os 6.3 Release for ILI9341

Dependents:   TFT_ILI9341_UniGraphic TFT_ILI9341_os6

Files at this revision

API Documentation at this revision

Comitter:
amouroug
Date:
Thu Oct 08 18:11:03 2020 -0500
Parent:
1:6f267dbbafec
Commit message:
Added GraphicsDisplay GFX API to draw triangle.

Changed in this revision

Graphics/GraphicsDisplay.cpp Show annotated file Show diff for this revision Revisions of this file
Graphics/GraphicsDisplay.h Show annotated file Show diff for this revision Revisions of this file
diff -r 6f267dbbafec -r 59188908eb60 Graphics/GraphicsDisplay.cpp
--- a/Graphics/GraphicsDisplay.cpp	Thu Oct 08 17:58:07 2020 -0500
+++ b/Graphics/GraphicsDisplay.cpp	Thu Oct 08 18:11:03 2020 -0500
@@ -473,4 +473,88 @@
     return (auto_up);
 }
 
-
+void GraphicsDisplay::triangle(int x0, int y0,
+                int x1, int y1, 
+                int x2, int y2, unsigned short color) {
+                    
+    line(x0, y0, x1, y1, color);
+    line(x1, y1, x2, y2, color);
+    line(x2, y2, x0, y0, color);
+}
+ 
+// fill a triangle!
+void GraphicsDisplay::fillTriangle(int x0, int y0, int x1, int y1, int x2, int y2, unsigned short color) 
+{ 
+  int 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;
+    hline(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);
+    hline(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);
+    hline(a, y, b-a+1, color);
+  }
+}
+ 
\ No newline at end of file
diff -r 6f267dbbafec -r 59188908eb60 Graphics/GraphicsDisplay.h
--- a/Graphics/GraphicsDisplay.h	Thu Oct 08 17:58:07 2020 -0500
+++ b/Graphics/GraphicsDisplay.h	Thu Oct 08 18:11:03 2020 -0500
@@ -186,6 +186,26 @@
    */    
   virtual void fillrect(int x0, int y0, int x1, int y1, unsigned short color);
   
+    /** draw a empty triangle
+   *
+   * @param x0,y0 1st vertice of triangle
+   * @param x1,y1 2nd vertice of triangle
+   * @param x2,y2 3rd vertice of triangle
+   * @param color 16 bit color
+   *
+   */    
+  virtual void triangle(int x0, int y0,int x1, int y1, int x2, int y2, unsigned short color);
+
+   /** draw a filled triangle
+   *
+   * @param x0,y0 1st vertice of triangle
+   * @param x1,y1 2nd vertice of triangle
+   * @param x2,y2 3rd vertice of triangle
+   * @param color 16 bit color
+   *
+   */  
+  virtual void fillTriangle(int x0, int y0, int x1, int y1, int x2, int y2, unsigned short color);
+
     /** setup cursor position for text
    *
    * @param x x-position (top left)