Skip to content

Adding circle and ellipse drawing functions #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ line KEYWORD2
point KEYWORD2
quad KEYWORD2
rect KEYWORD2
circle KEYWORD2
ellipse KEYWORD2

text KEYWORD2
textFont KEYWORD2
Expand Down
47 changes: 47 additions & 0 deletions src/ArduinoGraphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
}
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/ArduinoGraphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class ArduinoGraphics : public Print {

int width();
int height();
uint32_t background();

virtual void beginDraw();
virtual void endDraw();
Expand All @@ -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);
Expand Down