Skip to content

Commit bed59f2

Browse files
Fix issue #6. Added Ellipse, Circle and Elliptical Arc as mentioned.
1 parent 70c352c commit bed59f2

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

keywords.txt

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ point KEYWORD2
3434
quad KEYWORD2
3535
rect KEYWORD2
3636
ellipse KEYWORD2
37+
circle KEYWORD2
38+
arc KEYWORD2
3739

3840
text KEYWORD2
3941
textFont KEYWORD2

src/ArduinoGraphics.cpp

+62-5
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,13 @@ void ArduinoGraphics::ellipse(int x, int y, int width, int height)
192192

193193
int x1 = x;
194194
int y1 = y;
195-
int r1 = (width/2);
196-
int r2 = (height/2);
195+
int r1 = (int)(width/2);
196+
int r2 = (int)(height/2);
197197
int x2 = x1 + r1 - 1;
198198

199-
for(x = x1; x <= x2; x++) {
200-
y2 = y1 + sqrt((1 - ((x * x)/(r1 * r1)))) * r2;
201-
for(y = y1; y <= y2; y++) {
199+
for (x = x1; x <= x2; x++) {
200+
y2 = (int) (y1 + sqrt((1 - ((x * x)/(r1 * r1)))) * r2);
201+
for (y = y1; y <= y2; y++) {
202202
if ((y == y2) && _stroke) {
203203
// stroke
204204
set(x, y, _strokeR, _strokeG, _strokeB); // current point
@@ -216,6 +216,63 @@ void ArduinoGraphics::ellipse(int x, int y, int width, int height)
216216
}
217217
}
218218

219+
void ArduinoGraphics::circle(int x, int y, int radius)
220+
{
221+
if (!_stroke && !_fill) {
222+
return;
223+
}
224+
225+
int x1 = x;
226+
int y1 = y;
227+
int x2 = x1 + radius;
228+
229+
for (x = x1; x <= x2; x++) {
230+
y2 = (int) y1 + sqrt((radius*radius)-(x*x)) ;
231+
for (y = y1; y <= y2; y++) {
232+
if ((y == y2) && _stroke) {
233+
// stroke
234+
set(x, y, _strokeR, _strokeG, _strokeB); // current point
235+
set(x - (((x - x1) * 2)), y, _strokeR, _strokeG, _strokeB); // second reflection
236+
set(x, y - (((y - y1) * 2)), _strokeR, _strokeG, _strokeB); // third reflection
237+
set(x - (((x - x1) * 2)), y - (((y - y1) * 2)), _strokeR, _strokeG, _strokeB); // fourth reflection
238+
} else if (_fill) {
239+
// fill
240+
set(x, y, _fillR, _fillG, _fillB); // current point
241+
set(x - (((x - x1) * 2)), y, _fillR, _fillG, _fillB); // second reflection
242+
set(x, y - (((y - y1) * 2)), _fillR, _fillG, _fillB); // third reflection
243+
set(x - (((x - x1) * 2)), y - (((y - y1) * 2)), _fillR, _fillG, _fillB); // fourth reflection
244+
}
245+
}
246+
}
247+
}
248+
249+
void ArduinoGraphics::arc(int x, int y, int radiusX, int radiusY, int start, int stop);
250+
{
251+
if (!_stroke && !_fill) {
252+
return;
253+
}
254+
255+
int x1 = x;
256+
int y1 = y;
257+
258+
for(int a = start; a <= stop; a++) {
259+
260+
int x2 = (int)(x1 + (radiusX * cos(a)));
261+
int y2 = (int)(y1 + (radiusY * sin(a)));
262+
263+
if (_stroke) {
264+
// stroke
265+
set(x2, y2, _strokeR, _strokeG, _strokeB);
266+
}
267+
268+
if (_fill) {
269+
for (int r = 0; r < a; r++) {
270+
set((int)(x1 + (radiusX * cos(r))), (int)(y1 + (radiusY * sin(r))), _fillR, _fillG, _fillB);
271+
}
272+
}
273+
}
274+
}
275+
219276
void ArduinoGraphics::text(const char* str, int x, int y)
220277
{
221278
if (!_font || !_stroke) {

src/ArduinoGraphics.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
#include <Arduino.h>
2424

25+
#include <math.h> // added for sqrt, cos and sin functions
26+
2527
#include "Font.h"
2628
#include "Image.h"
2729

@@ -57,13 +59,14 @@ class ArduinoGraphics : public Print {
5759
void stroke(uint32_t color);
5860
void noStroke();
5961

60-
//virtual void arc(int x, int y, int width, int height, int start, int stop);
6162
virtual void line(int x1, int y1, int x2, int y2);
6263
virtual void point(int x, int y);
6364
//virtual void quad(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4);
6465
//virtual void triangle(int x1, int y1, int x2, int y2, int x3, int y3);
6566
virtual void rect(int x, int y, int width, int height);
6667
virtual void ellipse(int x, int y, int width, int height);
68+
virtual void circle(int x, int y, int radius);
69+
virtual void arc(int x, int y, int radiusX, int radiusY, int start, int stop);
6770

6871
virtual void text(const char* str, int x = 0, int y = 0);
6972
virtual void text(const String& str, int x = 0, int y = 0) { text(str.c_str(), x, y); }

0 commit comments

Comments
 (0)