From d3bd55dbc7d2ed5c0fe4b5ccafc677dcd0751789 Mon Sep 17 00:00:00 2001 From: Mira Grudzinska Date: Tue, 22 Dec 2020 13:12:58 +0100 Subject: [PATCH] sw_engine shape: expand the algorithm to draw arcs with negative angles The _appendArc function allows to draw angles only in a clockwise direction. The introduced improvement allows to change this direction by giving the 'sweep' argument with a negative value. --- src/lib/tvgShape.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/tvgShape.cpp b/src/lib/tvgShape.cpp index 1e0316d7..765d2b5a 100644 --- a/src/lib/tvgShape.cpp +++ b/src/lib/tvgShape.cpp @@ -153,14 +153,15 @@ Result Shape::appendArc(float cx, float cy, float radius, float startAngle, floa const float M_PI_HALF = M_PI * 0.5f; //just circle - if (sweep >= 360) return appendCircle(cx, cy, radius, radius); + if (sweep >= 360 || sweep <= -360) return appendCircle(cx, cy, radius, radius); startAngle = (startAngle * M_PI) / 180; sweep = sweep * M_PI / 180; auto nCurves = ceil(abs(sweep / M_PI_HALF)); + auto sweepSign = (sweep < 0 ? -1 : 1); auto fract = fmod(sweep, M_PI_HALF); - fract = (fract < std::numeric_limits::epsilon()) ? M_PI_HALF : fract; + fract = (abs(fract) < std::numeric_limits::epsilon()) ? M_PI_HALF : fract; //Start from here Point start = {radius * cos(startAngle), radius * sin(startAngle)}; @@ -173,8 +174,7 @@ Result Shape::appendArc(float cx, float cy, float radius, float startAngle, floa } for (int i = 0; i < nCurves; ++i) { - - auto endAngle = startAngle + ((i != nCurves - 1) ? M_PI_HALF : fract); + auto endAngle = startAngle + ((i != nCurves - 1) ? M_PI_HALF * sweepSign : fract); Point end = {radius * cos(endAngle), radius * sin(endAngle)}; //variables needed to calculate bezier control points