From 70c352c34f448b4c4c3bb4250d1e477e28393037 Mon Sep 17 00:00:00 2001 From: Ashutosh Date: Mon, 16 Mar 2020 14:52:00 +0530 Subject: [PATCH] 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); }