mirror of
https://github.com/adafruit/Adafruit-GFX-Library.git
synced 2026-07-27 20:05:52 +00:00
Add drawRotatedRect and fillRotatedRect methods (#508)
* Add drawRotatedRect and fillRotatedRect methods * Corrected docs
This commit is contained in:
@@ -701,6 +701,147 @@ void Adafruit_GFX::fillRoundRect(int16_t x, int16_t y, int16_t w, int16_t h,
|
||||
endWrite();
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Draw a rotated rectangle
|
||||
@param cenX x coordinate of center of rectangle.
|
||||
For even width rectangles, this point
|
||||
represents the pixel to the left of
|
||||
true center.
|
||||
@param cenY y coordinate of center of rectangle.
|
||||
For even height rectangles, this point
|
||||
represents the pixel above
|
||||
true center.
|
||||
@param w width of rectangle
|
||||
@param h height of rectangle
|
||||
@param angleDeg angle of rotation of rectangle
|
||||
@param color 16-bit 5-6-5 Color to fill/draw with
|
||||
*/
|
||||
/**************************************************************************/
|
||||
void Adafruit_GFX::drawRotatedRect(int16_t cenX, int16_t cenY, int16_t w,
|
||||
int16_t h, int16_t angleDeg,
|
||||
uint16_t color) {
|
||||
|
||||
if (w < 1 || h < 1)
|
||||
return; // We don't draw zero dimensioned objects
|
||||
|
||||
int16_t W = w - 1;
|
||||
int16_t H = h - 1;
|
||||
|
||||
int16_t halfW = (W / 2); // Midpoint should always be integer
|
||||
int16_t halfH = (H / 2); // Midpoint should always be integer
|
||||
|
||||
int16_t x0 = W - halfW; // bottom-right
|
||||
int16_t y0 = H - halfH; // bottom-right
|
||||
int16_t x1 = -halfW; // bottom-left
|
||||
int16_t y1 = H - halfH; // bottom-left
|
||||
int16_t x2 = -halfW; // top-left
|
||||
int16_t y2 = -halfH; // top-left
|
||||
int16_t x3 = W - halfW; // top-right
|
||||
int16_t y3 = -halfH; // top-right
|
||||
|
||||
rotatePoint(x0, y0, angleDeg);
|
||||
rotatePoint(x1, y1, angleDeg);
|
||||
rotatePoint(x2, y2, angleDeg);
|
||||
rotatePoint(x3, y3, angleDeg);
|
||||
|
||||
x0 += cenX;
|
||||
x1 += cenX;
|
||||
x2 += cenX;
|
||||
x3 += cenX;
|
||||
|
||||
y0 += cenY;
|
||||
y1 += cenY;
|
||||
y2 += cenY;
|
||||
y3 += cenY;
|
||||
|
||||
drawLine(x0, y0, x1, y1, color); // bottom right to bottom left
|
||||
drawLine(x1, y1, x2, y2, color); // bottom left to top left
|
||||
drawLine(x2, y2, x3, y3, color); // top left to top right
|
||||
drawLine(x3, y3, x0, y0, color); // top right to bottom right
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Draw a filled rotated rectangle
|
||||
@param cenX x coordinate of center of rectangle.
|
||||
For even width rectangles, this point
|
||||
represents the pixel to the left of
|
||||
true center.
|
||||
@param cenY y coordinate of center of rectangle.
|
||||
For even height rectangles, this point
|
||||
represents the pixel above
|
||||
true center.
|
||||
@param w width of rectangle
|
||||
@param h height of rectangle
|
||||
@param angleDeg angle of rotation of rectangle
|
||||
@param color 16-bit 5-6-5 Color to fill/draw with
|
||||
*/
|
||||
/**************************************************************************/
|
||||
void Adafruit_GFX::fillRotatedRect(int16_t cenX, int16_t cenY, int16_t w,
|
||||
int16_t h, int16_t angleDeg,
|
||||
uint16_t color) {
|
||||
|
||||
if (w < 1 || h < 1)
|
||||
return; // We don't draw zero dimensioned objects
|
||||
|
||||
int16_t W = w - 1;
|
||||
int16_t H = h - 1;
|
||||
|
||||
int16_t halfW = (W / 2); // Midpoint should always be integer
|
||||
int16_t halfH = (H / 2); // Midpoint should always be integer
|
||||
|
||||
int16_t x0 = W - halfW; // bottom-right
|
||||
int16_t y0 = H - halfH; // bottom-right
|
||||
int16_t x1 = -halfW; // bottom-left
|
||||
int16_t y1 = H - halfH; // bottom-left
|
||||
int16_t x2 = -halfW; // top-left
|
||||
int16_t y2 = -halfH; // top-left
|
||||
int16_t x3 = W - halfW; // top-right
|
||||
int16_t y3 = -halfH; // top-right
|
||||
|
||||
rotatePoint(x0, y0, angleDeg);
|
||||
rotatePoint(x1, y1, angleDeg);
|
||||
rotatePoint(x2, y2, angleDeg);
|
||||
rotatePoint(x3, y3, angleDeg);
|
||||
|
||||
x0 += cenX;
|
||||
x1 += cenX;
|
||||
x2 += cenX;
|
||||
x3 += cenX;
|
||||
|
||||
y0 += cenY;
|
||||
y1 += cenY;
|
||||
y2 += cenY;
|
||||
y3 += cenY;
|
||||
|
||||
fillTriangle(x0, y0, x1, y1, x2, y2, color);
|
||||
fillTriangle(x2, y2, x3, y3, x0, y0, color);
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Rotate a point in standard position
|
||||
@param x0 x coordinate of point to rotate. This is passed by reference
|
||||
and updated upon return
|
||||
@param y0 y coordinate of point to rotate. This is passed by reference
|
||||
and updated upon return
|
||||
@param angleDeg angle to rotate the point by (degrees)
|
||||
*/
|
||||
/**************************************************************************/
|
||||
void Adafruit_GFX::rotatePoint(int16_t &x0, int16_t &y0, int16_t angleDeg) {
|
||||
float angleRad = radians(angleDeg);
|
||||
float s = sin(angleRad);
|
||||
float c = cos(angleRad);
|
||||
|
||||
float x = x0;
|
||||
float y = y0;
|
||||
|
||||
// Rotate point
|
||||
x0 = (int16_t)((x * c) - (y * s));
|
||||
y0 = (int16_t)((x * s) + (y * c));
|
||||
}
|
||||
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@brief Draw a triangle with no fill color
|
||||
|
||||
@@ -85,6 +85,11 @@ public:
|
||||
int16_t radius, uint16_t color);
|
||||
void fillRoundRect(int16_t x0, int16_t y0, int16_t w, int16_t h,
|
||||
int16_t radius, uint16_t color);
|
||||
void drawRotatedRect(int16_t cenX, int16_t cenY, int16_t w, int16_t h,
|
||||
int16_t angleDeg, uint16_t color);
|
||||
void fillRotatedRect(int16_t cenX, int16_t cenY, int16_t w, int16_t h,
|
||||
int16_t angleDeg, uint16_t color);
|
||||
void rotatePoint(int16_t &x0, int16_t &y0, int16_t angleDeg);
|
||||
void drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w,
|
||||
int16_t h, uint16_t color);
|
||||
void drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w,
|
||||
|
||||
Reference in New Issue
Block a user