common: code refactoring

introduced mathDeg2Rad() and mathRad2Deg() for a common implementation.
This commit is contained in:
Hermet Park 2024-04-15 11:57:26 +09:00
parent b7eb72a27d
commit bfed28e86e
7 changed files with 27 additions and 15 deletions

View file

@ -238,7 +238,7 @@ float bezAngleAt(const Bezier& bz, float t)
pt.x *= 3;
pt.y *= 3;
return atan2(pt.x, pt.y) * 180.0f / 3.141592f;
return mathRad2Deg(atan2(pt.x, pt.y));
}

View file

@ -44,6 +44,18 @@ bool mathIdentity(const Matrix* m);
void mathMultiply(Point* pt, const Matrix* transform);
static inline float mathDeg2Rad(float degree)
{
return degree * (MATH_PI / 180.0f);
}
static inline float mathRad2Deg(float radian)
{
return radian * (180.0f / MATH_PI);
}
static inline bool mathZero(float a)
{
return (fabsf(a) < FLT_EPSILON) ? true : false;

View file

@ -104,7 +104,7 @@ static Shape* _draw(LottieGroup* parent, RenderContext* ctx);
static void _rotateX(Matrix* m, float degree)
{
if (degree == 0.0f) return;
auto radian = degree / 180.0f * MATH_PI;
auto radian = mathDeg2Rad(degree);
m->e22 *= cosf(radian);
}
@ -112,7 +112,7 @@ static void _rotateX(Matrix* m, float degree)
static void _rotateY(Matrix* m, float degree)
{
if (degree == 0.0f) return;
auto radian = degree / 180.0f * MATH_PI;
auto radian = mathDeg2Rad(degree);
m->e11 *= cosf(radian);
}
@ -120,7 +120,7 @@ static void _rotateY(Matrix* m, float degree)
static void _rotationZ(Matrix* m, float degree)
{
if (degree == 0.0f) return;
auto radian = degree / 180.0f * MATH_PI;
auto radian = mathDeg2Rad(degree);
m->e11 = cosf(radian);
m->e12 = -sinf(radian);
m->e21 = sinf(radian);
@ -617,7 +617,7 @@ static void _updateStar(LottieGroup* parent, LottiePolyStar* star, Matrix* trans
auto innerRoundness = star->innerRoundness(frameNo) * 0.01f;
auto outerRoundness = star->outerRoundness(frameNo) * 0.01f;
auto angle = -90.0f * MATH_PI / 180.0f;
auto angle = mathDeg2Rad(-90.0f);
auto partialPointRadius = 0.0f;
auto anglePerPoint = (2.0f * MATH_PI / ptsCnt);
auto halfAnglePerPoint = anglePerPoint * 0.5f;
@ -724,7 +724,7 @@ static void _updatePolygon(LottieGroup* parent, LottiePolyStar* star, Matrix* tr
auto radius = star->outerRadius(frameNo);
auto roundness = star->outerRoundness(frameNo) * 0.01f;
auto angle = -90.0f * MATH_PI / 180.0f;
auto angle = mathDeg2Rad(-90.0f);
auto anglePerPoint = 2.0f * MATH_PI / float(ptsCnt);
auto direction = (star->direction == 0) ? 1.0f : -1.0f;
auto hasRoundness = false;

View file

@ -115,8 +115,8 @@ Fill* LottieGradient::fill(float frameNo)
P(static_cast<RadialGradient*>(fill))->radial(sx, sy, r, sx, sy, 0.0f);
} else {
if (mathEqual(progress, 1.0f)) progress = 0.99f;
auto startAngle = atan2(ey - sy, ex - sx) * 180.0f / MATH_PI;
auto angle = (startAngle + this->angle(frameNo)) * (MATH_PI / 180.0f);
auto startAngle = mathRad2Deg(atan2(ey - sy, ex - sx));
auto angle = mathDeg2Rad((startAngle + this->angle(frameNo)));
auto fx = sx + cos(angle) * progress * r;
auto fy = sy + sin(angle) * progress * r;
// Lottie dosen't have any focal radius concept

View file

@ -853,8 +853,8 @@ static Matrix* _parseTransformationMatrix(const char* value)
//Transform to signed.
points[0] = fmodf(points[0], 360.0f);
if (points[0] < 0) points[0] += 360.0f;
auto c = cosf(points[0] * (MATH_PI / 180.0f));
auto s = sinf(points[0] * (MATH_PI / 180.0f));
auto c = cosf(mathDeg2Rad(points[0]));
auto s = sinf(mathDeg2Rad(points[0]));
if (ptCount == 1) {
Matrix tmp = { c, -s, 0, s, c, 0, 0, 0, 1 };
*matrix = mathMultiply(matrix, &tmp);
@ -877,12 +877,12 @@ static Matrix* _parseTransformationMatrix(const char* value)
*matrix = mathMultiply(matrix, &tmp);
} else if (state == MatrixState::SkewX) {
if (ptCount != 1) goto error;
auto deg = tanf(points[0] * (MATH_PI / 180.0f));
auto deg = tanf(mathDeg2Rad(points[0]));
Matrix tmp = { 1, deg, 0, 0, 1, 0, 0, 0, 1 };
*matrix = mathMultiply(matrix, &tmp);
} else if (state == MatrixState::SkewY) {
if (ptCount != 1) goto error;
auto deg = tanf(points[0] * (MATH_PI / 180.0f));
auto deg = tanf(mathDeg2Rad(points[0]));
Matrix tmp = { 1, 0, 0, deg, 1, 0, 0, 0, 1 };
*matrix = mathMultiply(matrix, &tmp);
}

View file

@ -126,7 +126,7 @@ void _pathAppendArcTo(Array<PathCommand>* cmds, Array<Point>* pts, Point* cur, P
rx = fabsf(rx);
ry = fabsf(ry);
angle = angle * MATH_PI / 180.0f;
angle = mathDeg2Rad(angle);
cosPhi = cosf(angle);
sinPhi = sinf(angle);
dx2 = (sx - x) / 2.0f;

View file

@ -145,8 +145,8 @@ Result Shape::appendArc(float cx, float cy, float radius, float startAngle, floa
//just circle
if (sweep >= 360.0f || sweep <= -360.0f) return appendCircle(cx, cy, radius, radius);
startAngle = (startAngle * MATH_PI) / 180.0f;
sweep = sweep * MATH_PI / 180.0f;
startAngle = mathDeg2Rad(startAngle);
sweep = mathDeg2Rad(sweep);
auto nCurves = ceil(fabsf(sweep / MATH_PI2));
auto sweepSign = (sweep < 0 ? -1 : 1);