Added elipse + examples.

This commit is contained in:
nitko12 2020-08-26 09:22:16 +02:00
parent a0d91f1004
commit 1e434d6b60
4 changed files with 210 additions and 2 deletions

View File

@ -515,6 +515,99 @@ int Inkplate::drawBitmapFromWeb(char *url, int x, int y, bool dither, bool inver
return ret; return ret;
} }
void Inkplate::drawElipse(int rx, int ry,
int xc, int yc,
int c)
{
float dx, dy, d1, d2, x, y;
x = 0;
y = ry;
d1 = (ry * ry) - (rx * rx * ry) +
(0.25 * rx * rx);
dx = 2 * ry * ry * x;
dy = 2 * rx * rx * y;
while (dx < dy)
{
drawPixel(x + xc, y + yc, c);
drawPixel(-x + xc, y + yc, c);
drawPixel(x + xc, -y + yc, c);
drawPixel(-x + xc, -y + yc, c);
if (d1 < 0)
{
x++;
dx = dx + (2 * ry * ry);
d1 = d1 + dx + (ry * ry);
}
else
{
x++;
y--;
dx = dx + (2 * ry * ry);
dy = dy - (2 * rx * rx);
d1 = d1 + dx - dy + (ry * ry);
}
}
d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5))) +
((rx * rx) * ((y - 1) * (y - 1))) -
(rx * rx * ry * ry);
while (y >= 0)
{
drawPixel(x + xc, y + yc, c);
drawPixel(-x + xc, y + yc, c);
drawPixel(x + xc, -y + yc, c);
drawPixel(-x + xc, -y + yc, c);
if (d2 > 0)
{
y--;
dy = dy - (2 * rx * rx);
d2 = d2 + (rx * rx) - dy;
}
else
{
y--;
x++;
dx = dx + (2 * ry * ry);
dy = dy - (2 * rx * rx);
d2 = d2 + dx - dy + (rx * rx);
}
}
}
void Inkplate::fillElipse(int rx, int ry,
int xc, int yc,
int c)
{
int hh = ry * ry;
int ww = rx * rx;
int hhww = hh * ww;
int x0 = rx;
int dx = 0;
for (int x = -rx; x <= rx; x++)
drawPixel(xc + x, yc, c);
for (int y = 1; y <= ry; y++)
{
int x1 = x0 - (dx - 1);
for (; x1 > 0; x1--)
if (x1 * x1 * hh + y * y * ww <= hhww)
break;
dx = x0 - x1;
x0 = x1;
for (int x = -x0; x <= x0; x++)
{
drawPixel(xc + x, yc - y, c);
drawPixel(xc + x, yc + y, c);
}
}
}
void Inkplate::fillPolygon(int *x, int *y, int n, int color) void Inkplate::fillPolygon(int *x, int *y, int n, int color)
{ {
int tx[100], ty[100]; int tx[100], ty[100];
@ -530,6 +623,12 @@ void Inkplate::fillPolygon(int *x, int *y, int n, int color)
} }
} }
void Inkplate::drawPolygon(int *x, int *y, int n, int color)
{
for (int i = 0; i < n; ++i)
drawLine(x[i], y[i], x[(i + 1) % n], y[(i + 1) % n], color);
}
void Inkplate::drawThickLine(int x1, int y1, int x2, int y2, int color, float thickness) void Inkplate::drawThickLine(int x1, int y1, int x2, int y2, int color, float thickness)
{ {
float deg = atan2f((float)(y2 - y1), (float)(x2 - x1)); float deg = atan2f((float)(y2 - y1), (float)(x2 - x1));

View File

@ -204,6 +204,9 @@ public:
int drawBitmapFromSD(char *fileName, int x, int y, bool dither = false, bool invert = false); int drawBitmapFromSD(char *fileName, int x, int y, bool dither = false, bool invert = false);
int drawBitmapFromWeb(WiFiClient *s, int x, int y, int len, bool dither = false, bool invert = false); int drawBitmapFromWeb(WiFiClient *s, int x, int y, int len, bool dither = false, bool invert = false);
int drawBitmapFromWeb(char *url, int x, int y, bool dither = false, bool invert = false); int drawBitmapFromWeb(char *url, int x, int y, bool dither = false, bool invert = false);
void drawElipse(int rx, int ry, int xc, int yc, int c);
void fillElipse(int rx, int ry, int xc, int yc, int c);
void drawPolygon(int *x, int *y, int n, int color);
void fillPolygon(int *x, int *y, int n, int color); void fillPolygon(int *x, int *y, int n, int color);
void drawThickLine(int x1, int y1, int x2, int y2, int color, float thickness); void drawThickLine(int x1, int y1, int x2, int y2, int color, float thickness);
void drawGradientLine(int x1, int y1, int x2, int y2, int color1, int color2, float thickness = -1); void drawGradientLine(int x1, int y1, int x2, int y2, int color1, int color2, float thickness = -1);

View File

@ -361,6 +361,59 @@ void loop()
display.display(); display.display();
delay(DELAY_MS); delay(DELAY_MS);
// Draws an elipse with x radius, y radius, center x, center y and color
display.clearDisplay();
display.drawElipse(100, 200, 400, 300, 0);
displayCurrentAction("Drawing an elipse");
display.display();
delay(DELAY_MS);
// Fills an elipse with x radius, y radius, center x, center y and color
display.clearDisplay();
display.fillElipse(100, 200, 400, 300, 0);
displayCurrentAction("Drawing a filled elipse");
display.display();
delay(DELAY_MS);
// Code block for generating random points and sorting them in a counter
// clockwise direction.
int xt[10];
int yt[10];
int n = 10;
for (int i = 0; i < n; ++i)
{
xt[i] = random(100, 700);
yt[i] = random(100, 500);
}
int k;
for (int i = 0; i < n - 1; ++i)
for (int j = i + 1; j < n; ++j)
if (atan2(yt[j] - 300, xt[j] - 400) < atan2(yt[i] - 300, xt[i] - 400))
{
k = xt[i], xt[i] = xt[j], xt[j] = k;
k = yt[i], yt[i] = yt[j], yt[j] = k;
}
// Draws a polygon, from x and y coordinate arrays of n points in color c
display.clearDisplay();
display.drawPolygon(xt, yt, n, 0);
displayCurrentAction("Drawing a polygon");
display.display();
delay(DELAY_MS);
// Fills a polygon, from x and y coordinate arrays of n points in color c,
// Points need to be counter clockwise sorted
// Method can be quite slow for now, probably will improve
display.clearDisplay();
display.fillPolygon(xt, yt, n, 0);
displayCurrentAction("Drawing a filled polygon");
display.display();
delay(DELAY_MS);
//Write text and rotate it by 90 deg. forever //Write text and rotate it by 90 deg. forever
int r = 0; int r = 0;
display.setTextSize(8); display.setTextSize(8);

View File

@ -54,9 +54,9 @@ void loop()
//Now, let's draw some random pixels! //Now, let's draw some random pixels!
display.clearDisplay(); //Clear everything that is inside frame buffer in ESP32 display.clearDisplay(); //Clear everything that is inside frame buffer in ESP32
for (int i = 0; i < 1000; i++) for (int i = 0; i < 1000; i++)
{ //Write 1000 random colored pixels at random locations { //Write 1000 random colored pixels at random locations
display.drawPixel(random(0, 799), random(0, 599), random(0, 7)); //We are setting color of the pixels using numbers from 0 to 7, display.drawPixel(random(0, 799), random(0, 599), random(0, 7)); //We are setting color of the pixels using numbers from 0 to 7,
} //where 0 mens black, 7 white and gray is in between } //where 0 mens black, 7 white and gray is in between
displayCurrentAction("Drawing 600 random pixels in random colors"); displayCurrentAction("Drawing 600 random pixels in random colors");
display.display(); //Write everything from frame buffer to screen display.display(); //Write everything from frame buffer to screen
delay(DELAY_MS); //Wait delay(DELAY_MS); //Wait
@ -275,6 +275,59 @@ void loop()
display.display(); display.display();
delay(DELAY_MS); delay(DELAY_MS);
// Draws an elipse with x radius, y radius, center x, center y and color
display.clearDisplay();
display.drawElipse(100, 200, 400, 300, 0);
displayCurrentAction("Drawing an elipse");
display.display();
delay(DELAY_MS);
// Fills an elipse with x radius, y radius, center x, center y and color
display.clearDisplay();
display.fillElipse(100, 200, 400, 300, 0);
displayCurrentAction("Drawing a filled elipse");
display.display();
delay(DELAY_MS);
// Code block for generating random points and sorting them in a counter
// clockwise direction.
int xt[10];
int yt[10];
int n = 10;
for (int i = 0; i < n; ++i)
{
xt[i] = random(100, 700);
yt[i] = random(100, 500);
}
int k;
for (int i = 0; i < n - 1; ++i)
for (int j = i + 1; j < n; ++j)
if (atan2(yt[j] - 300, xt[j] - 400) < atan2(yt[i] - 300, xt[i] - 400))
{
k = xt[i], xt[i] = xt[j], xt[j] = k;
k = yt[i], yt[i] = yt[j], yt[j] = k;
}
// Draws a polygon, from x and y coordinate arrays of n points in color c
display.clearDisplay();
display.drawPolygon(xt, yt, n, 0);
displayCurrentAction("Drawing a polygon");
display.display();
delay(DELAY_MS);
// Fills a polygon, from x and y coordinate arrays of n points in color c,
// Points need to be counter clockwise sorted
// Method can be quite slow for now, probably will improve
display.clearDisplay();
display.fillPolygon(xt, yt, n, 0);
displayCurrentAction("Drawing a filled polygon");
display.display();
delay(DELAY_MS);
//Write text and rotate it by 90 deg. forever //Write text and rotate it by 90 deg. forever
int r = 0; int r = 0;
display.setTextSize(8); display.setTextSize(8);