From 001f35ce87d064f9df7253aac9d03ea64e93c3ed Mon Sep 17 00:00:00 2001 From: Leonardo Cavagnis Date: Fri, 14 Apr 2023 15:47:33 +0200 Subject: [PATCH] adding circle and ellipse drawing functions --- docs/api.md | 77 +++++++++++++++++++++++++++++++++++++++++ keywords.txt | 2 ++ src/ArduinoGraphics.cpp | 47 +++++++++++++++++++++++++ src/ArduinoGraphics.h | 4 ++- 4 files changed, 129 insertions(+), 1 deletion(-) diff --git a/docs/api.md b/docs/api.md index 491bb9c..a870920 100644 --- a/docs/api.md +++ b/docs/api.md @@ -539,6 +539,83 @@ YourScreen.endDraw(); ``` +### `circle()` + +#### Description + + +Stroke and fill a circle, uses the stroke color set in stroke() and the fill color set in fill(). + +#### Syntax + +``` +YourScreen.circle(x, y, diameter) + +``` + + +#### Parameters + + +- x: x center position of the circle +- y: y center position of the circle +- diameter: diameter of the circle + +#### Returns + +Nothing + +#### Example + +``` +YourScreen.beginDraw(); +YourScreen.clear(); +YourScreen.noStroke(); +YourScreen.fill(255, 255, 0); +YourScreen.circle(YourScreen.width()/2, YourScreen.height()/2, YourScreen.height()); +YourScreen.endDraw(); +``` + + +### `ellipse()` + +#### Description + + +Stroke and fill an ellipse, uses the stroke color set in stroke() and the fill color set in fill(). + +#### Syntax + +``` +YourScreen.ellipse(x, y, width, height) + +``` + + +#### Parameters + + +- x: x center position of the ellipse +- y: y center position of the ellipse +- width: width of the ellipse +- height: height of the ellipse + +#### Returns + +Nothing + +#### Example + +``` +YourScreen.beginDraw(); +YourScreen.clear(); +YourScreen.noStroke(); +YourScreen.fill(255, 255, 0); +YourScreen.ellipse(YourScreen.width()/2, YourScreen.height()/2, YourScreen.width(), YourScreen.height()); +YourScreen.endDraw(); +``` + + ### `text()` #### Description diff --git a/keywords.txt b/keywords.txt index 5c7d60b..ce07cc7 100644 --- a/keywords.txt +++ b/keywords.txt @@ -33,6 +33,8 @@ line KEYWORD2 point KEYWORD2 quad KEYWORD2 rect KEYWORD2 +circle KEYWORD2 +ellipse KEYWORD2 text KEYWORD2 textFont KEYWORD2 diff --git a/src/ArduinoGraphics.cpp b/src/ArduinoGraphics.cpp index 5aeff55..15fe1ad 100644 --- a/src/ArduinoGraphics.cpp +++ b/src/ArduinoGraphics.cpp @@ -59,6 +59,12 @@ int ArduinoGraphics::height() return _height; } +uint32_t ArduinoGraphics::background() +{ + uint32_t bg = (uint32_t)((uint32_t)(_backgroundR << 16) | (uint32_t)(_backgroundG << 8) | (uint32_t)(_backgroundB << 0)); + return bg; +} + void ArduinoGraphics::beginDraw() { } @@ -124,6 +130,47 @@ void ArduinoGraphics::noStroke() _stroke = false; } +void ArduinoGraphics::circle(int x, int y, int diameter) +{ + ellipse(x, y, diameter, diameter); +} + +void ArduinoGraphics::ellipse(int x, int y, int width, int height) +{ + if (!_stroke && !_fill) { + return; + } + + int32_t a = width / 2; + int32_t b = height / 2; + int64_t a2 = a * a; + int64_t b2 = b * b; + int64_t i, j; + + if (_fill) { + for (j = -b; j <= b; j++) { + for (i = -a; i <= a; i++) { + if (i*i*b2 + j*j*a2 <= a2*b2) { + set(x + i, y + j, _fillR, _fillG, _fillB); + } + } + } + } + if (_stroke) { + int x_val, y_val; + for (i = -a; i <= a; i++) { + y_val = b * sqrt(1 - (double)i*i / a2); + set(x + i, y + y_val, _strokeR, _strokeG, _strokeB); + set(x + i, y - y_val, _strokeR, _strokeG, _strokeB); + } + for (j = -b; j <= b; j++) { + x_val = a * sqrt(1 - (double)j*j / b2); + set(x + x_val, y + j, _strokeR, _strokeG, _strokeB); + set(x - x_val, y + j, _strokeR, _strokeG, _strokeB); + } + } +} + void ArduinoGraphics::line(int x1, int y1, int x2, int y2) { if (!_stroke) { diff --git a/src/ArduinoGraphics.h b/src/ArduinoGraphics.h index f5468b0..1f1c914 100644 --- a/src/ArduinoGraphics.h +++ b/src/ArduinoGraphics.h @@ -43,6 +43,7 @@ class ArduinoGraphics : public Print { int width(); int height(); + uint32_t background(); virtual void beginDraw(); virtual void endDraw(); @@ -58,7 +59,8 @@ 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 circle(int x, int y, int diameter); + 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);