Skip to content

Modify drawSmoothArc to support a separate background color for arc ends #3707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
21 changes: 15 additions & 6 deletions TFT_eSPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3969,16 +3969,24 @@ uint16_t TFT_eSPI::drawPixel(int32_t x, int32_t y, uint32_t color, uint8_t alpha
** Function name: drawSmoothArc
** Description: Draw a smooth arc clockwise from 6 o'clock
***************************************************************************************/
void TFT_eSPI::drawSmoothArc(int32_t x, int32_t y, int32_t r, int32_t ir, uint32_t startAngle, uint32_t endAngle, uint32_t fg_color, uint32_t bg_color, bool roundEnds)
void TFT_eSPI::drawSmoothArc(int32_t x, int32_t y, int32_t r, int32_t ir,
uint32_t startAngle, uint32_t endAngle,
uint32_t fg_color, uint32_t bg_color, bool roundEnds,
uint32_t end_bg_color)
// Centre at x,y
// r = arc outer radius, ir = arc inner radius. Inclusive so arc thickness = r - ir + 1
// Angles in range 0-360
// Arc foreground colour anti-aliased with background colour at edges
// anti-aliased roundEnd is optional, default is anti-aliased straight end
// Note: rounded ends extend the arc angle so can overlap, user sketch to manage this.
// Added parameter: end_bg_color - background color for antialiasing the ends
// (defaults to bg_color if not specified)
{
inTransaction = true;

// If end_bg_color is not specified (zero), use the same color as bg_color
if (end_bg_color == 0) end_bg_color = bg_color;

if (endAngle != startAngle && (startAngle != 0 || endAngle != 360))
{
float sx = -sinf(startAngle * deg2rad);
Expand All @@ -3990,33 +3998,34 @@ void TFT_eSPI::drawSmoothArc(int32_t x, int32_t y, int32_t r, int32_t ir, uint32
{ // Round ends
sx = sx * (r + ir)/2.0 + x;
sy = sy * (r + ir)/2.0 + y;
drawSpot(sx, sy, (r - ir)/2.0, fg_color, bg_color);
drawSpot(sx, sy, (r - ir)/2.0, fg_color, end_bg_color); // Use end_bg_color here

ex = ex * (r + ir)/2.0 + x;
ey = ey * (r + ir)/2.0 + y;
drawSpot(ex, ey, (r - ir)/2.0, fg_color, bg_color);
drawSpot(ex, ey, (r - ir)/2.0, fg_color, end_bg_color); // Use end_bg_color here
}
else
{ // Square ends
float asx = sx * ir + x;
float asy = sy * ir + y;
float aex = sx * r + x;
float aey = sy * r + y;
drawWedgeLine(asx, asy, aex, aey, 0.3, 0.3, fg_color, bg_color);
drawWedgeLine(asx, asy, aex, aey, 0.3, 0.3, fg_color, end_bg_color); // Use end_bg_color here

asx = ex * ir + x;
asy = ey * ir + y;
aex = ex * r + x;
aey = ey * r + y;
drawWedgeLine(asx, asy, aex, aey, 0.3, 0.3, fg_color, bg_color);
drawWedgeLine(asx, asy, aex, aey, 0.3, 0.3, fg_color, end_bg_color); // Use end_bg_color here
}

// Draw arc
// Draw arc - using original bg_color for sides
drawArc(x, y, r, ir, startAngle, endAngle, fg_color, bg_color);

}
else // Draw full 360
{
// For a full circle, there are no ends, so just use the regular bg_color
drawArc(x, y, r, ir, 0, 360, fg_color, bg_color);
}

Expand Down
2 changes: 1 addition & 1 deletion TFT_eSPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ class TFT_eSPI : public Print { friend class TFT_eSprite; // Sprite class has ac
// By default the arc is drawn with square ends unless the "roundEnds" parameter is included and set true
// Angle = 0 is at 6 o'clock position, 90 at 9 o'clock etc. The angles must be in range 0-360 or they will be clipped to these limits
// The start angle may be larger than the end angle. Arcs are always drawn clockwise from the start angle.
void drawSmoothArc(int32_t x, int32_t y, int32_t r, int32_t ir, uint32_t startAngle, uint32_t endAngle, uint32_t fg_color, uint32_t bg_color, bool roundEnds = false);
void drawSmoothArc(int32_t x, int32_t y, int32_t r, int32_t ir, uint32_t startAngle, uint32_t endAngle, uint32_t fg_color, uint32_t bg_color, bool roundEnds = false, uint32_t end_bg_color = 0);

// As per "drawSmoothArc" except the ends of the arc are NOT anti-aliased, this facilitates dynamic arc length changes with
// arc segments and ensures clean segment joints.
Expand Down