Skip to content
Open
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
109 changes: 96 additions & 13 deletions Adafruit_GFX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,89 @@ void Adafruit_GFX::writePixel(int16_t x, int16_t y, uint16_t color){
drawPixel(x, y, color);
}

// zjw分支 做的椭圆
void Adafruit_GFX::drawEllipse(int16_t x0, int16_t y0, int16_t rx, int16_t ry, uint16_t color) {
int32_t x = 0;
int32_t y = ry;

int32_t rx2 = (int32_t)rx * rx;
int32_t ry2 = (int32_t)ry * ry;
int32_t two_rx2 = 2 * rx2;
int32_t two_ry2 = 2 * ry2;

int32_t px = 0;
int32_t py = two_rx2 * y;

// Region 1
int32_t p = ry2 - rx2 * ry + rx2 / 4;

while (px < py) {
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++;
px += two_ry2;

if (p < 0) {
p += ry2 + px;
} else {
y--;
py -= two_rx2;
p += ry2 + px - py;
}
}

// Region 2
p = ry2 * (x * x + x) + rx2 * (y * y - 2 * y + 1) - rx2 * ry2;

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--;
py -= two_rx2;

if (p > 0) {
p += rx2 - py;
} else {
x++;
px += two_ry2;
p += rx2 - py + px;
}
}
}

// qdz 做的爱心
//爱心
void Adafruit_GFX::drawHeart(int16_t x0, int16_t y0,
int16_t size, uint16_t color) {
int16_t prevX = 0, prevY = 0;
bool first = true;
for (int i = 0; i <= 360; i += 5) { // 角度步进,越小越平滑
float t = i * PI / 180.0;
float fx = 16 * pow(sin(t), 3);
float fy = 13 * cos(t)
- 5 * cos(2 * t)
- 2 * cos(3 * t)
- cos(4 * t);
int16_t x = x0 + fx * size / 16;
int16_t y = y0 - fy * size / 16;
if (!first) {
drawLine(prevX, prevY, x, y, color);
} else {
first = false;
}
prevX = x;
prevY = y;
}
}



// (x,y) is topmost point; if unsure, calling function
// should sort endpoints or call writeLine() instead
void Adafruit_GFX::writeFastVLine(int16_t x, int16_t y,
Expand Down Expand Up @@ -396,19 +479,19 @@ void Adafruit_GFX::drawPentagram(int16_t x0, int16_t y0,
}

// Draw a ellipse outline
void Adafruit_GFX::drawEllipse(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t a, uint16_t color) {
int16_t max_x = ((x1 > x2 ? x1 : x2) + a > 128 ? (x1 > x2 ? x1 : x2) + a : 128);
int16_t max_y = ((y1 > y2 ? y1 : y2) + a > 64 ? (y1 > y2 ? y1 : y2) + a : 64);
for (int16_t x = ((x1 > x2 ? x2 : x1) - a > 0 ? (x1 > x2 ? x2 : x1) - a : 0 ); x <= max_x; x++) {
for (int16_t y = ((y1 > y2 ? y2 : y1) - a > 0 ? (y1 > y2 ? y2 : y1) - a : 0); y <= max_y; y++) {
int32_t distance = sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1)) + sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
if (distance-a == a) {
writePixel(x, y, color);
}
}
}
endWrite();
}
// void Adafruit_GFX::drawEllipse(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t a, uint16_t color) {
// int16_t max_x = ((x1 > x2 ? x1 : x2) + a > 128 ? (x1 > x2 ? x1 : x2) + a : 128);
// int16_t max_y = ((y1 > y2 ? y1 : y2) + a > 64 ? (y1 > y2 ? y1 : y2) + a : 64);
// for (int16_t x = ((x1 > x2 ? x2 : x1) - a > 0 ? (x1 > x2 ? x2 : x1) - a : 0 ); x <= max_x; x++) {
// for (int16_t y = ((y1 > y2 ? y2 : y1) - a > 0 ? (y1 > y2 ? y2 : y1) - a : 0); y <= max_y; y++) {
// int32_t distance = sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1)) + sqrt((x - x2) * (x - x2) + (y - y2) * (y - y2));
// if (distance-a == a) {
// writePixel(x, y, color);
// }
// }
// }
// endWrite();
// }


// Draw a triangle
Expand Down
11 changes: 10 additions & 1 deletion Adafruit_GFX.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,16 @@ class Adafruit_GFX : public Print {
fillCircleHelper(int16_t x0, int16_t y0, int16_t r, uint8_t cornername,
int16_t delta, uint16_t color),
drawPentagram(int16_t x0, int16_t y0, int16_t r0, uint16_t color),
drawEllipse(int16_t x1, int16_t y1, int16_t x2, int16_t y2, int16_t a, uint16_t color),


// qdz分支画椭圆:
// Draw heart
drawHeart(int16_t x0, int16_t y0, int16_t size, uint16_t color),
// zjw分支画椭圆:
// Draw drawEllipse
drawEllipse(int16_t x0, int16_t y0, int16_t rx, int16_t ry,uint16_t color),


drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
int16_t x2, int16_t y2, uint16_t color),
fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1,
Expand Down