Skip to content

Commit 001f35c

Browse files
adding circle and ellipse drawing functions
1 parent bc3c523 commit 001f35c

File tree

4 files changed

+129
-1
lines changed

4 files changed

+129
-1
lines changed

docs/api.md

+77
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,83 @@ YourScreen.endDraw();
539539
```
540540

541541

542+
### `circle()`
543+
544+
#### Description
545+
546+
547+
Stroke and fill a circle, uses the stroke color set in stroke() and the fill color set in fill().
548+
549+
#### Syntax
550+
551+
```
552+
YourScreen.circle(x, y, diameter)
553+
554+
```
555+
556+
557+
#### Parameters
558+
559+
560+
- x: x center position of the circle
561+
- y: y center position of the circle
562+
- diameter: diameter of the circle
563+
564+
#### Returns
565+
566+
Nothing
567+
568+
#### Example
569+
570+
```
571+
YourScreen.beginDraw();
572+
YourScreen.clear();
573+
YourScreen.noStroke();
574+
YourScreen.fill(255, 255, 0);
575+
YourScreen.circle(YourScreen.width()/2, YourScreen.height()/2, YourScreen.height());
576+
YourScreen.endDraw();
577+
```
578+
579+
580+
### `ellipse()`
581+
582+
#### Description
583+
584+
585+
Stroke and fill an ellipse, uses the stroke color set in stroke() and the fill color set in fill().
586+
587+
#### Syntax
588+
589+
```
590+
YourScreen.ellipse(x, y, width, height)
591+
592+
```
593+
594+
595+
#### Parameters
596+
597+
598+
- x: x center position of the ellipse
599+
- y: y center position of the ellipse
600+
- width: width of the ellipse
601+
- height: height of the ellipse
602+
603+
#### Returns
604+
605+
Nothing
606+
607+
#### Example
608+
609+
```
610+
YourScreen.beginDraw();
611+
YourScreen.clear();
612+
YourScreen.noStroke();
613+
YourScreen.fill(255, 255, 0);
614+
YourScreen.ellipse(YourScreen.width()/2, YourScreen.height()/2, YourScreen.width(), YourScreen.height());
615+
YourScreen.endDraw();
616+
```
617+
618+
542619
### `text()`
543620

544621
#### Description

keywords.txt

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ line KEYWORD2
3333
point KEYWORD2
3434
quad KEYWORD2
3535
rect KEYWORD2
36+
circle KEYWORD2
37+
ellipse KEYWORD2
3638

3739
text KEYWORD2
3840
textFont KEYWORD2

src/ArduinoGraphics.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ int ArduinoGraphics::height()
5959
return _height;
6060
}
6161

62+
uint32_t ArduinoGraphics::background()
63+
{
64+
uint32_t bg = (uint32_t)((uint32_t)(_backgroundR << 16) | (uint32_t)(_backgroundG << 8) | (uint32_t)(_backgroundB << 0));
65+
return bg;
66+
}
67+
6268
void ArduinoGraphics::beginDraw()
6369
{
6470
}
@@ -124,6 +130,47 @@ void ArduinoGraphics::noStroke()
124130
_stroke = false;
125131
}
126132

133+
void ArduinoGraphics::circle(int x, int y, int diameter)
134+
{
135+
ellipse(x, y, diameter, diameter);
136+
}
137+
138+
void ArduinoGraphics::ellipse(int x, int y, int width, int height)
139+
{
140+
if (!_stroke && !_fill) {
141+
return;
142+
}
143+
144+
int32_t a = width / 2;
145+
int32_t b = height / 2;
146+
int64_t a2 = a * a;
147+
int64_t b2 = b * b;
148+
int64_t i, j;
149+
150+
if (_fill) {
151+
for (j = -b; j <= b; j++) {
152+
for (i = -a; i <= a; i++) {
153+
if (i*i*b2 + j*j*a2 <= a2*b2) {
154+
set(x + i, y + j, _fillR, _fillG, _fillB);
155+
}
156+
}
157+
}
158+
}
159+
if (_stroke) {
160+
int x_val, y_val;
161+
for (i = -a; i <= a; i++) {
162+
y_val = b * sqrt(1 - (double)i*i / a2);
163+
set(x + i, y + y_val, _strokeR, _strokeG, _strokeB);
164+
set(x + i, y - y_val, _strokeR, _strokeG, _strokeB);
165+
}
166+
for (j = -b; j <= b; j++) {
167+
x_val = a * sqrt(1 - (double)j*j / b2);
168+
set(x + x_val, y + j, _strokeR, _strokeG, _strokeB);
169+
set(x - x_val, y + j, _strokeR, _strokeG, _strokeB);
170+
}
171+
}
172+
}
173+
127174
void ArduinoGraphics::line(int x1, int y1, int x2, int y2)
128175
{
129176
if (!_stroke) {

src/ArduinoGraphics.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class ArduinoGraphics : public Print {
4343

4444
int width();
4545
int height();
46+
uint32_t background();
4647

4748
virtual void beginDraw();
4849
virtual void endDraw();
@@ -58,7 +59,8 @@ class ArduinoGraphics : public Print {
5859
void noStroke();
5960

6061
//virtual void arc(int x, int y, int width, int height, int start, int stop);
61-
//virtual void ellipse(int x, int y, int width, int height);
62+
virtual void circle(int x, int y, int diameter);
63+
virtual void ellipse(int x, int y, int width, int height);
6264
virtual void line(int x1, int y1, int x2, int y2);
6365
virtual void point(int x, int y);
6466
//virtual void quad(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4);

0 commit comments

Comments
 (0)