From 70c352c34f448b4c4c3bb4250d1e477e28393037 Mon Sep 17 00:00:00 2001 From: Ashutosh Date: Mon, 16 Mar 2020 14:52:00 +0530 Subject: [PATCH 1/3] Ellipse Function Added. Need to test. --- keywords.txt | 5 +++-- src/ArduinoGraphics.cpp | 36 ++++++++++++++++++++++++++++++++++-- src/ArduinoGraphics.h | 2 +- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/keywords.txt b/keywords.txt index 5c7d60b..dcdec65 100644 --- a/keywords.txt +++ b/keywords.txt @@ -10,8 +10,8 @@ Font KEYWORD1 Image KEYWORD1 ########################################## -# Methods and Functions -########################################## +# Methods and Functions +########################################## begin KEYWORD2 end KEYWORD2 @@ -33,6 +33,7 @@ line KEYWORD2 point KEYWORD2 quad KEYWORD2 rect KEYWORD2 +ellipse KEYWORD2 text KEYWORD2 textFont KEYWORD2 diff --git a/src/ArduinoGraphics.cpp b/src/ArduinoGraphics.cpp index bedd1a7..75960af 100644 --- a/src/ArduinoGraphics.cpp +++ b/src/ArduinoGraphics.cpp @@ -184,6 +184,38 @@ void ArduinoGraphics::rect(int x, int y, int width, int height) } } +void ArduinoGraphics::ellipse(int x, int y, int width, int height) +{ + if (!_stroke && !_fill) { + return; + } + + int x1 = x; + int y1 = y; + int r1 = (width/2); + int r2 = (height/2); + int x2 = x1 + r1 - 1; + + for(x = x1; x <= x2; x++) { + y2 = y1 + sqrt((1 - ((x * x)/(r1 * r1)))) * r2; + for(y = y1; y <= y2; y++) { + if ((y == y2) && _stroke) { + // stroke + set(x, y, _strokeR, _strokeG, _strokeB); // current point + set(x - (((x - x1) * 2)), y, _strokeR, _strokeG, _strokeB); // second reflection + set(x, y - (((y - y1) * 2)), _strokeR, _strokeG, _strokeB); // third reflection + set(x - (((x - x1) * 2)), y - (((y - y1) * 2)), _strokeR, _strokeG, _strokeB); // fourth reflection + } else if (_fill) { + // fill + set(x, y, _fillR, _fillG, _fillB); // current point + set(x - (((x - x1) * 2)), y, _fillR, _fillG, _fillB); // second reflection + set(x, y - (((y - y1) * 2)), _fillR, _fillG, _fillB); // third reflection + set(x - (((x - x1) * 2)), y - (((y - y1) * 2)), _fillR, _fillG, _fillB); // fourth reflection + } + } + } +} + void ArduinoGraphics::text(const char* str, int x, int y) { if (!_font || !_stroke) { @@ -365,7 +397,7 @@ void ArduinoGraphics::beginText(int x, int y, uint8_t r, uint8_t g, uint8_t b) _textR = r; _textG = g; - _textB = b; + _textB = b; } void ArduinoGraphics::beginText(int x, int y, uint32_t color) @@ -482,7 +514,7 @@ void ArduinoGraphics::lineHigh(int x1, int y1, int x2, int y2) xi = -1; dx = -dx; } - + int D = 2 * dx - dy; int x = x1; diff --git a/src/ArduinoGraphics.h b/src/ArduinoGraphics.h index f5468b0..6df5790 100644 --- a/src/ArduinoGraphics.h +++ b/src/ArduinoGraphics.h @@ -58,12 +58,12 @@ class ArduinoGraphics : public Print { void noStroke(); //virtual void arc(int x, int y, int width, int height, int start, int stop); - //virtual void ellipse(int x, int y, int width, int height); virtual void line(int x1, int y1, int x2, int y2); virtual void point(int x, int y); //virtual void quad(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4); //virtual void triangle(int x1, int y1, int x2, int y2, int x3, int y3); virtual void rect(int x, int y, int width, int height); + virtual void ellipse(int x, int y, int width, int height); virtual void text(const char* str, int x = 0, int y = 0); virtual void text(const String& str, int x = 0, int y = 0) { text(str.c_str(), x, y); } From bed59f26345dbd53bf95c1f9c91de80735c52300 Mon Sep 17 00:00:00 2001 From: Ashutosh Date: Tue, 17 Mar 2020 00:08:51 +0530 Subject: [PATCH 2/3] Fix issue #6. Added Ellipse, Circle and Elliptical Arc as mentioned. --- keywords.txt | 2 ++ src/ArduinoGraphics.cpp | 67 ++++++++++++++++++++++++++++++++++++++--- src/ArduinoGraphics.h | 5 ++- 3 files changed, 68 insertions(+), 6 deletions(-) diff --git a/keywords.txt b/keywords.txt index dcdec65..1855ffd 100644 --- a/keywords.txt +++ b/keywords.txt @@ -34,6 +34,8 @@ point KEYWORD2 quad KEYWORD2 rect KEYWORD2 ellipse KEYWORD2 +circle KEYWORD2 +arc KEYWORD2 text KEYWORD2 textFont KEYWORD2 diff --git a/src/ArduinoGraphics.cpp b/src/ArduinoGraphics.cpp index 75960af..1f099fe 100644 --- a/src/ArduinoGraphics.cpp +++ b/src/ArduinoGraphics.cpp @@ -192,13 +192,13 @@ void ArduinoGraphics::ellipse(int x, int y, int width, int height) int x1 = x; int y1 = y; - int r1 = (width/2); - int r2 = (height/2); + int r1 = (int)(width/2); + int r2 = (int)(height/2); int x2 = x1 + r1 - 1; - for(x = x1; x <= x2; x++) { - y2 = y1 + sqrt((1 - ((x * x)/(r1 * r1)))) * r2; - for(y = y1; y <= y2; y++) { + for (x = x1; x <= x2; x++) { + y2 = (int) (y1 + sqrt((1 - ((x * x)/(r1 * r1)))) * r2); + for (y = y1; y <= y2; y++) { if ((y == y2) && _stroke) { // stroke set(x, y, _strokeR, _strokeG, _strokeB); // current point @@ -216,6 +216,63 @@ void ArduinoGraphics::ellipse(int x, int y, int width, int height) } } +void ArduinoGraphics::circle(int x, int y, int radius) +{ + if (!_stroke && !_fill) { + return; + } + + int x1 = x; + int y1 = y; + int x2 = x1 + radius; + + for (x = x1; x <= x2; x++) { + y2 = (int) y1 + sqrt((radius*radius)-(x*x)) ; + for (y = y1; y <= y2; y++) { + if ((y == y2) && _stroke) { + // stroke + set(x, y, _strokeR, _strokeG, _strokeB); // current point + set(x - (((x - x1) * 2)), y, _strokeR, _strokeG, _strokeB); // second reflection + set(x, y - (((y - y1) * 2)), _strokeR, _strokeG, _strokeB); // third reflection + set(x - (((x - x1) * 2)), y - (((y - y1) * 2)), _strokeR, _strokeG, _strokeB); // fourth reflection + } else if (_fill) { + // fill + set(x, y, _fillR, _fillG, _fillB); // current point + set(x - (((x - x1) * 2)), y, _fillR, _fillG, _fillB); // second reflection + set(x, y - (((y - y1) * 2)), _fillR, _fillG, _fillB); // third reflection + set(x - (((x - x1) * 2)), y - (((y - y1) * 2)), _fillR, _fillG, _fillB); // fourth reflection + } + } + } +} + +void ArduinoGraphics::arc(int x, int y, int radiusX, int radiusY, int start, int stop); +{ + if (!_stroke && !_fill) { + return; + } + + int x1 = x; + int y1 = y; + + for(int a = start; a <= stop; a++) { + + int x2 = (int)(x1 + (radiusX * cos(a))); + int y2 = (int)(y1 + (radiusY * sin(a))); + + if (_stroke) { + // stroke + set(x2, y2, _strokeR, _strokeG, _strokeB); + } + + if (_fill) { + for (int r = 0; r < a; r++) { + set((int)(x1 + (radiusX * cos(r))), (int)(y1 + (radiusY * sin(r))), _fillR, _fillG, _fillB); + } + } + } +} + void ArduinoGraphics::text(const char* str, int x, int y) { if (!_font || !_stroke) { diff --git a/src/ArduinoGraphics.h b/src/ArduinoGraphics.h index 6df5790..c2af9ec 100644 --- a/src/ArduinoGraphics.h +++ b/src/ArduinoGraphics.h @@ -22,6 +22,8 @@ #include +#include // added for sqrt, cos and sin functions + #include "Font.h" #include "Image.h" @@ -57,13 +59,14 @@ class ArduinoGraphics : public Print { void stroke(uint32_t color); void noStroke(); - //virtual void arc(int x, int y, int width, int height, int start, int stop); virtual void line(int x1, int y1, int x2, int y2); virtual void point(int x, int y); //virtual void quad(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4); //virtual void triangle(int x1, int y1, int x2, int y2, int x3, int y3); virtual void rect(int x, int y, int width, int height); virtual void ellipse(int x, int y, int width, int height); + virtual void circle(int x, int y, int radius); + virtual void arc(int x, int y, int radiusX, int radiusY, int start, int stop); virtual void text(const char* str, int x = 0, int y = 0); virtual void text(const String& str, int x = 0, int y = 0) { text(str.c_str(), x, y); } From a58d8988c10b0d4c6f3c665bc92b44eb8586d19a Mon Sep 17 00:00:00 2001 From: Ashutosh Sharma Date: Mon, 23 Mar 2020 09:33:56 +0530 Subject: [PATCH 3/3] Changed and fixed as suggested Co-Authored-By: per1234 Changed ellipse function. Removed arc function for now. --- keywords.txt | 5 +- src/ArduinoGraphics.cpp | 169 +++++++++++++++++++++------------------- src/ArduinoGraphics.h | 2 +- 3 files changed, 93 insertions(+), 83 deletions(-) diff --git a/keywords.txt b/keywords.txt index 1855ffd..7c93dfc 100644 --- a/keywords.txt +++ b/keywords.txt @@ -33,9 +33,8 @@ line KEYWORD2 point KEYWORD2 quad KEYWORD2 rect KEYWORD2 -ellipse KEYWORD2 -circle KEYWORD2 -arc KEYWORD2 +ellipse KEYWORD2 +circle KEYWORD2 text KEYWORD2 textFont KEYWORD2 diff --git a/src/ArduinoGraphics.cpp b/src/ArduinoGraphics.cpp index 1f099fe..f59bfc1 100644 --- a/src/ArduinoGraphics.cpp +++ b/src/ArduinoGraphics.cpp @@ -186,91 +186,102 @@ void ArduinoGraphics::rect(int x, int y, int width, int height) void ArduinoGraphics::ellipse(int x, int y, int width, int height) { - if (!_stroke && !_fill) { - return; - } + if (!_stroke && !_fill) { + return; + } - int x1 = x; - int y1 = y; - int r1 = (int)(width/2); - int r2 = (int)(height/2); - int x2 = x1 + r1 - 1; + int r1 = (int)(width/2); + int r2 = (int)(height/2); + + x--; + y--; + + for(int i = 0; i < r1; i++) + { + int j = ceil(sqrt(1 - ((float)(i*i)/(r1*r1))) * r2); + + int x1 = x-i; + int x2 = x+i; + int y1 = y-j; + int y2 = y+j; + + if(width%2 == 0) + { + x2--; + } + + if(height%2 == 0) + { + y2--; + } + + if(_stroke) + { + set(x1, y1, _strokeR, _strokeG, _strokeB); + set(x1, y2, _strokeR, _strokeG, _strokeB); + set(x2, y1, _strokeR, _strokeG, _strokeB); + set(x2, y2, _strokeR, _strokeG, _strokeB); + } + + if(_fill) + { + for(int a = 0; a < j; a++) + { + int x1 = x-i; + int x2 = x+i; + int y1 = y-a; + int y2 = y+a; + + if(width%2 == 0) + { + x2--; + } + + if(height%2 == 0) + { + y2--; + } + + set(x1, y1, _fillR, _fillG, _fillB); + set(x1, y2, _fillR, _fillG, _fillB); + set(x2, y1, _fillR, _fillG, _fillB); + set(x2, y2, _fillR, _fillG, _fillB); + } + } + } - for (x = x1; x <= x2; x++) { - y2 = (int) (y1 + sqrt((1 - ((x * x)/(r1 * r1)))) * r2); - for (y = y1; y <= y2; y++) { - if ((y == y2) && _stroke) { - // stroke - set(x, y, _strokeR, _strokeG, _strokeB); // current point - set(x - (((x - x1) * 2)), y, _strokeR, _strokeG, _strokeB); // second reflection - set(x, y - (((y - y1) * 2)), _strokeR, _strokeG, _strokeB); // third reflection - set(x - (((x - x1) * 2)), y - (((y - y1) * 2)), _strokeR, _strokeG, _strokeB); // fourth reflection - } else if (_fill) { - // fill - set(x, y, _fillR, _fillG, _fillB); // current point - set(x - (((x - x1) * 2)), y, _fillR, _fillG, _fillB); // second reflection - set(x, y - (((y - y1) * 2)), _fillR, _fillG, _fillB); // third reflection - set(x - (((x - x1) * 2)), y - (((y - y1) * 2)), _fillR, _fillG, _fillB); // fourth reflection - } - } - } + if(_stroke) + { + for(int j = 0; j < r2; j++) + { + int i = ceil(sqrt(1 - ((float)(j*j)/(r2*r2))) * r1); + + int x1 = x-i; + int x2 = x+i; + int y1 = y-j; + int y2 = y+j; + + if(width%2 == 0) + { + x2--; + } + + if(height%2 == 0) + { + y2--; + } + + set(x1, y1, _strokeR, _strokeG, _strokeB); + set(x1, y2, _strokeR, _strokeG, _strokeB); + set(x2, y1, _strokeR, _strokeG, _strokeB); + set(x2, y2, _strokeR, _strokeG, _strokeB); + } + } } void ArduinoGraphics::circle(int x, int y, int radius) { - if (!_stroke && !_fill) { - return; - } - - int x1 = x; - int y1 = y; - int x2 = x1 + radius; - - for (x = x1; x <= x2; x++) { - y2 = (int) y1 + sqrt((radius*radius)-(x*x)) ; - for (y = y1; y <= y2; y++) { - if ((y == y2) && _stroke) { - // stroke - set(x, y, _strokeR, _strokeG, _strokeB); // current point - set(x - (((x - x1) * 2)), y, _strokeR, _strokeG, _strokeB); // second reflection - set(x, y - (((y - y1) * 2)), _strokeR, _strokeG, _strokeB); // third reflection - set(x - (((x - x1) * 2)), y - (((y - y1) * 2)), _strokeR, _strokeG, _strokeB); // fourth reflection - } else if (_fill) { - // fill - set(x, y, _fillR, _fillG, _fillB); // current point - set(x - (((x - x1) * 2)), y, _fillR, _fillG, _fillB); // second reflection - set(x, y - (((y - y1) * 2)), _fillR, _fillG, _fillB); // third reflection - set(x - (((x - x1) * 2)), y - (((y - y1) * 2)), _fillR, _fillG, _fillB); // fourth reflection - } - } - } -} - -void ArduinoGraphics::arc(int x, int y, int radiusX, int radiusY, int start, int stop); -{ - if (!_stroke && !_fill) { - return; - } - - int x1 = x; - int y1 = y; - - for(int a = start; a <= stop; a++) { - - int x2 = (int)(x1 + (radiusX * cos(a))); - int y2 = (int)(y1 + (radiusY * sin(a))); - - if (_stroke) { - // stroke - set(x2, y2, _strokeR, _strokeG, _strokeB); - } - - if (_fill) { - for (int r = 0; r < a; r++) { - set((int)(x1 + (radiusX * cos(r))), (int)(y1 + (radiusY * sin(r))), _fillR, _fillG, _fillB); - } - } - } + ellipse(x, y, ((radius*2)-1), ((radius*2)-1)); } void ArduinoGraphics::text(const char* str, int x, int y) diff --git a/src/ArduinoGraphics.h b/src/ArduinoGraphics.h index c2af9ec..1ff88df 100644 --- a/src/ArduinoGraphics.h +++ b/src/ArduinoGraphics.h @@ -66,7 +66,7 @@ class ArduinoGraphics : public Print { virtual void rect(int x, int y, int width, int height); virtual void ellipse(int x, int y, int width, int height); virtual void circle(int x, int y, int radius); - virtual void arc(int x, int y, int radiusX, int radiusY, int start, int stop); + // virtual void arc(int x, int y, int radiusX, int radiusY, int start, int stop); virtual void text(const char* str, int x = 0, int y = 0); virtual void text(const String& str, int x = 0, int y = 0) { text(str.c_str(), x, y); }