Skip to content
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
116 changes: 116 additions & 0 deletions Adafruit_GFX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,122 @@ void Adafruit_GFX::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
}
}

/**************************************************************************/
/*!
@brief Draw an ellipse outline
@param x0 Center-point x coordinate
@param y0 Center-point y coordinate
@param rw Horizontal radius of ellipse
@param rh Vertical radius of ellipse
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::drawEllipse(int16_t x0, int16_t y0, int16_t rw, int16_t rh,
uint16_t color) {
#if defined(ESP8266)
yield();
#endif
// Bresenham's ellipse algorithm
int16_t x = 0, y = rh;
int32_t rw2 = rw * rw, rh2 = rh * rh;
int32_t twoRw2 = 2 * rw2, twoRh2 = 2 * rh2;

int32_t decision = rh2 - (rw2 * rh) + (rw2 / 4);

startWrite();

// region 1
while ((twoRh2 * x) < (twoRw2 * y)) {
writePixel(x0 + x, y0 + y, color);
writePixel(x0 - x, y0 + y, color);
writePixel(x0 + x, y0 - y, color);
writePixel(x0 - x, y0 - y, color);
x++;
if (decision < 0) {
decision += rh2 + (twoRh2 * x);
} else {
decision += rh2 + (twoRh2 * x) - (twoRw2 * y);
y--;
}
}

// region 2
decision = ((rh2 * (2 * x + 1) * (2 * x + 1)) >> 2) +
(rw2 * (y - 1) * (y - 1)) - (rw2 * rh2);
while (y >= 0) {
writePixel(x0 + x, y0 + y, color);
writePixel(x0 - x, y0 + y, color);
writePixel(x0 + x, y0 - y, color);
writePixel(x0 - x, y0 - y, color);
y--;
if (decision > 0) {
decision += rw2 - (twoRw2 * y);
} else {
decision += rw2 + (twoRh2 * x) - (twoRw2 * y);
x++;
}
}

endWrite();
}

/**************************************************************************/
/*!
@brief Draw an ellipse with filled colour
@param x0 Center-point x coordinate
@param y0 Center-point y coordinate
@param rw Horizontal radius of ellipse
@param rh Vertical radius of ellipse
@param color 16-bit 5-6-5 Color to draw with
*/
/**************************************************************************/
void Adafruit_GFX::fillEllipse(int16_t x0, int16_t y0, int16_t rw, int16_t rh,
uint16_t color) {
#if defined(ESP8266)
yield();
#endif
// Bresenham's ellipse algorithm
int16_t x = 0, y = rh;
int32_t rw2 = rw * rw, rh2 = rh * rh;
int32_t twoRw2 = 2 * rw2, twoRh2 = 2 * rh2;

int32_t decision = rh2 - (rw2 * rh) + (rw2 / 4);

startWrite();

// region 1
while ((twoRh2 * x) < (twoRw2 * y)) {
drawFastHLine(x0 - x, y0 + y, 2 * x + 1, color);
drawFastHLine(x0 - x, y0 - y, 2 * x + 1, color);

x++;
if (decision < 0) {
decision += rh2 + (twoRh2 * x);
} else {
decision += rh2 + (twoRh2 * x) - (twoRw2 * y);
y--;
}
}

// region 2
decision = ((rh2 * (2 * x + 1) * (2 * x + 1)) >> 2) +
(rw2 * (y - 1) * (y - 1)) - (rw2 * rh2);
while (y >= 0) {
drawFastHLine(x0 - x, y0 + y, 2 * x + 1, color);
drawFastHLine(x0 - x, y0 - y, 2 * x + 1, color);

y--;
if (decision > 0) {
decision += rw2 - (twoRw2 * y);
} else {
decision += rw2 + (twoRh2 * x) - (twoRw2 * y);
x++;
}
}

endWrite();
}

/**************************************************************************/
/*!
@brief Draw a circle outline
Expand Down
4 changes: 4 additions & 0 deletions Adafruit_GFX.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ class Adafruit_GFX : public Print {
void fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);
void fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername,
int16_t delta, uint16_t color);
void drawEllipse(int16_t x0, int16_t y0, int16_t rw, int16_t rh,
uint16_t color);
void fillEllipse(int16_t x0, int16_t y0, int16_t rw, int16_t rh,
uint16_t color);
void drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2,
int16_t y2, uint16_t color);
void fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2,
Expand Down