Skip to content

Commit 32da3d2

Browse files
Added drawEllipse and fillEllipse functions (#477)
* Added drawEllipse and fillEllipse functions * Formatting fixes
1 parent 21648a8 commit 32da3d2

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

Adafruit_GFX.cpp

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,122 @@ void Adafruit_GFX::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
345345
}
346346
}
347347

348+
/**************************************************************************/
349+
/*!
350+
@brief Draw an ellipse outline
351+
@param x0 Center-point x coordinate
352+
@param y0 Center-point y coordinate
353+
@param rw Horizontal radius of ellipse
354+
@param rh Vertical radius of ellipse
355+
@param color 16-bit 5-6-5 Color to draw with
356+
*/
357+
/**************************************************************************/
358+
void Adafruit_GFX::drawEllipse(int16_t x0, int16_t y0, int16_t rw, int16_t rh,
359+
uint16_t color) {
360+
#if defined(ESP8266)
361+
yield();
362+
#endif
363+
// Bresenham's ellipse algorithm
364+
int16_t x = 0, y = rh;
365+
int32_t rw2 = rw * rw, rh2 = rh * rh;
366+
int32_t twoRw2 = 2 * rw2, twoRh2 = 2 * rh2;
367+
368+
int32_t decision = rh2 - (rw2 * rh) + (rw2 / 4);
369+
370+
startWrite();
371+
372+
// region 1
373+
while ((twoRh2 * x) < (twoRw2 * y)) {
374+
writePixel(x0 + x, y0 + y, color);
375+
writePixel(x0 - x, y0 + y, color);
376+
writePixel(x0 + x, y0 - y, color);
377+
writePixel(x0 - x, y0 - y, color);
378+
x++;
379+
if (decision < 0) {
380+
decision += rh2 + (twoRh2 * x);
381+
} else {
382+
decision += rh2 + (twoRh2 * x) - (twoRw2 * y);
383+
y--;
384+
}
385+
}
386+
387+
// region 2
388+
decision = ((rh2 * (2 * x + 1) * (2 * x + 1)) >> 2) +
389+
(rw2 * (y - 1) * (y - 1)) - (rw2 * rh2);
390+
while (y >= 0) {
391+
writePixel(x0 + x, y0 + y, color);
392+
writePixel(x0 - x, y0 + y, color);
393+
writePixel(x0 + x, y0 - y, color);
394+
writePixel(x0 - x, y0 - y, color);
395+
y--;
396+
if (decision > 0) {
397+
decision += rw2 - (twoRw2 * y);
398+
} else {
399+
decision += rw2 + (twoRh2 * x) - (twoRw2 * y);
400+
x++;
401+
}
402+
}
403+
404+
endWrite();
405+
}
406+
407+
/**************************************************************************/
408+
/*!
409+
@brief Draw an ellipse with filled colour
410+
@param x0 Center-point x coordinate
411+
@param y0 Center-point y coordinate
412+
@param rw Horizontal radius of ellipse
413+
@param rh Vertical radius of ellipse
414+
@param color 16-bit 5-6-5 Color to draw with
415+
*/
416+
/**************************************************************************/
417+
void Adafruit_GFX::fillEllipse(int16_t x0, int16_t y0, int16_t rw, int16_t rh,
418+
uint16_t color) {
419+
#if defined(ESP8266)
420+
yield();
421+
#endif
422+
// Bresenham's ellipse algorithm
423+
int16_t x = 0, y = rh;
424+
int32_t rw2 = rw * rw, rh2 = rh * rh;
425+
int32_t twoRw2 = 2 * rw2, twoRh2 = 2 * rh2;
426+
427+
int32_t decision = rh2 - (rw2 * rh) + (rw2 / 4);
428+
429+
startWrite();
430+
431+
// region 1
432+
while ((twoRh2 * x) < (twoRw2 * y)) {
433+
drawFastHLine(x0 - x, y0 + y, 2 * x + 1, color);
434+
drawFastHLine(x0 - x, y0 - y, 2 * x + 1, color);
435+
436+
x++;
437+
if (decision < 0) {
438+
decision += rh2 + (twoRh2 * x);
439+
} else {
440+
decision += rh2 + (twoRh2 * x) - (twoRw2 * y);
441+
y--;
442+
}
443+
}
444+
445+
// region 2
446+
decision = ((rh2 * (2 * x + 1) * (2 * x + 1)) >> 2) +
447+
(rw2 * (y - 1) * (y - 1)) - (rw2 * rh2);
448+
while (y >= 0) {
449+
drawFastHLine(x0 - x, y0 + y, 2 * x + 1, color);
450+
drawFastHLine(x0 - x, y0 - y, 2 * x + 1, color);
451+
452+
y--;
453+
if (decision > 0) {
454+
decision += rw2 - (twoRw2 * y);
455+
} else {
456+
decision += rw2 + (twoRh2 * x) - (twoRw2 * y);
457+
x++;
458+
}
459+
}
460+
461+
endWrite();
462+
}
463+
348464
/**************************************************************************/
349465
/*!
350466
@brief Draw a circle outline

Adafruit_GFX.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ class Adafruit_GFX : public Print {
7373
void fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
7474
void fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername,
7575
int16_t delta, uint16_t color);
76+
void drawEllipse(int16_t x0, int16_t y0, int16_t rw, int16_t rh,
77+
uint16_t color);
78+
void fillEllipse(int16_t x0, int16_t y0, int16_t rw, int16_t rh,
79+
uint16_t color);
7680
void drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2,
7781
int16_t y2, uint16_t color);
7882
void fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2,

0 commit comments

Comments
 (0)