[common] code clean up

use MATH_PI and MATH_PI2 instead of M_PI and M_PI_2
This commit is contained in:
vtorri 2024-03-22 15:48:25 +01:00 committed by GitHub
parent 832f22e915
commit 04977c43f1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 19 additions and 19 deletions

View file

@ -71,7 +71,7 @@ void mathRotate(Matrix* m, float degree)
{ {
if (degree == 0.0f) return; if (degree == 0.0f) return;
auto radian = degree / 180.0f * M_PI; auto radian = degree / 180.0f * MATH_PI;
auto cosVal = cosf(radian); auto cosVal = cosf(radian);
auto sinVal = sinf(radian); auto sinVal = sinf(radian);

View file

@ -70,7 +70,7 @@ static inline bool mathEqual(const Matrix& a, const Matrix& b)
static inline bool mathRightAngle(const Matrix* m) static inline bool mathRightAngle(const Matrix* m)
{ {
auto radian = fabsf(atan2f(m->e21, m->e11)); auto radian = fabsf(atan2f(m->e21, m->e11));
if (radian < FLT_EPSILON || mathEqual(radian, float(M_PI_2)) || mathEqual(radian, float(M_PI))) return true; if (radian < FLT_EPSILON || mathEqual(radian, MATH_PI2) || mathEqual(radian, MATH_PI)) return true;
return false; return false;
} }

View file

@ -94,7 +94,7 @@ static bool _buildComposition(LottieComposition* comp, LottieGroup* parent);
static void _rotateX(Matrix* m, float degree) static void _rotateX(Matrix* m, float degree)
{ {
if (degree == 0.0f) return; if (degree == 0.0f) return;
auto radian = degree / 180.0f * M_PI; auto radian = degree / 180.0f * MATH_PI;
m->e22 *= cosf(radian); m->e22 *= cosf(radian);
} }
@ -102,7 +102,7 @@ static void _rotateX(Matrix* m, float degree)
static void _rotateY(Matrix* m, float degree) static void _rotateY(Matrix* m, float degree)
{ {
if (degree == 0.0f) return; if (degree == 0.0f) return;
auto radian = degree / 180.0f * M_PI; auto radian = degree / 180.0f * MATH_PI;
m->e11 *= cosf(radian); m->e11 *= cosf(radian);
} }
@ -110,7 +110,7 @@ static void _rotateY(Matrix* m, float degree)
static void _rotationZ(Matrix* m, float degree) static void _rotationZ(Matrix* m, float degree)
{ {
if (degree == 0.0f) return; if (degree == 0.0f) return;
auto radian = degree / 180.0f * M_PI; auto radian = degree / 180.0f * MATH_PI;
m->e11 = cosf(radian); m->e11 = cosf(radian);
m->e12 = -sinf(radian); m->e12 = -sinf(radian);
m->e21 = sinf(radian); m->e21 = sinf(radian);

View file

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

View file

@ -126,7 +126,7 @@ void _pathAppendArcTo(Array<PathCommand>* cmds, Array<Point>* pts, Point* cur, P
rx = fabsf(rx); rx = fabsf(rx);
ry = fabsf(ry); ry = fabsf(ry);
angle = angle * M_PI / 180.0f; angle = angle * MATH_PI / 180.0f;
cosPhi = cosf(angle); cosPhi = cosf(angle);
sinPhi = sinf(angle); sinPhi = sinf(angle);
dx2 = (sx - x) / 2.0f; dx2 = (sx - x) / 2.0f;
@ -195,24 +195,24 @@ void _pathAppendArcTo(Array<PathCommand>* cmds, Array<Point>* pts, Point* cur, P
//http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm //http://www.euclideanspace.com/maths/algebra/vectors/angleBetween/index.htm
//Note: atan2 (0.0, 1.0) == 0.0 //Note: atan2 (0.0, 1.0) == 0.0
at = atan2(((y1p - cyp) / ry), ((x1p - cxp) / rx)); at = atan2(((y1p - cyp) / ry), ((x1p - cxp) / rx));
theta1 = (at < 0.0f) ? 2.0f * M_PI + at : at; theta1 = (at < 0.0f) ? 2.0f * MATH_PI + at : at;
nat = atan2(((-y1p - cyp) / ry), ((-x1p - cxp) / rx)); nat = atan2(((-y1p - cyp) / ry), ((-x1p - cxp) / rx));
deltaTheta = (nat < at) ? 2.0f * M_PI - at + nat : nat - at; deltaTheta = (nat < at) ? 2.0f * MATH_PI - at + nat : nat - at;
if (sweep) { if (sweep) {
//Ensure delta theta < 0 or else add 360 degrees //Ensure delta theta < 0 or else add 360 degrees
if (deltaTheta < 0.0f) deltaTheta += (float)(2.0f * M_PI); if (deltaTheta < 0.0f) deltaTheta += 2.0f * MATH_PI;
} else { } else {
//Ensure delta theta > 0 or else substract 360 degrees //Ensure delta theta > 0 or else substract 360 degrees
if (deltaTheta > 0.0f) deltaTheta -= (float)(2.0f * M_PI); if (deltaTheta > 0.0f) deltaTheta -= 2.0f * MATH_PI;
} }
//Add several cubic bezier to approximate the arc //Add several cubic bezier to approximate the arc
//(smaller than 90 degrees) //(smaller than 90 degrees)
//We add one extra segment because we want something //We add one extra segment because we want something
//Smaller than 90deg (i.e. not 90 itself) //Smaller than 90deg (i.e. not 90 itself)
segments = static_cast<int>(fabsf(deltaTheta / float(M_PI_2)) + 1.0f); segments = static_cast<int>(fabsf(deltaTheta / MATH_PI2) + 1.0f);
delta = deltaTheta / segments; delta = deltaTheta / segments;
//http://www.stillhq.com/ctpfaq/2001/comp.text.pdf-faq-2001-04.txt (section 2.13) //http://www.stillhq.com/ctpfaq/2001/comp.text.pdf-faq-2001-04.txt (section 2.13)

View file

@ -164,7 +164,7 @@ Result Shape::appendArc(float cx, float cy, float radius, float startAngle, floa
} }
for (int i = 0; i < nCurves; ++i) { for (int i = 0; i < nCurves; ++i) {
auto endAngle = startAngle + ((i != nCurves - 1) ? float(M_PI_2) * sweepSign : fract); auto endAngle = startAngle + ((i != nCurves - 1) ? MATH_PI2 * sweepSign : fract);
Point end = {radius * cosf(endAngle), radius * sinf(endAngle)}; Point end = {radius * cosf(endAngle), radius * sinf(endAngle)};
//variables needed to calculate bezier control points //variables needed to calculate bezier control points

View file

@ -131,8 +131,8 @@ void WgGeometryData::appendCircle(WgPoint center, float radius)
uint32_t index = positions.count; uint32_t index = positions.count;
uint32_t nSegments = std::trunc(radius); uint32_t nSegments = std::trunc(radius);
for (uint32_t i = 0; i < nSegments; i++) { for (uint32_t i = 0; i < nSegments; i++) {
float angle0 = (float)(i + 0) / nSegments * (float)M_PI * 2.0f; float angle0 = (float)(i + 0) / nSegments * MATH_PI * 2.0f;
float angle1 = (float)(i + 1) / nSegments * (float)M_PI * 2.0f; float angle1 = (float)(i + 1) / nSegments * MATH_PI * 2.0f;
WgPoint p0 = center + WgPoint(sin(angle0) * radius, cos(angle0) * radius); WgPoint p0 = center + WgPoint(sin(angle0) * radius, cos(angle0) * radius);
WgPoint p1 = center + WgPoint(sin(angle1) * radius, cos(angle1) * radius); WgPoint p1 = center + WgPoint(sin(angle1) * radius, cos(angle1) * radius);
positions.push(center); // +0 positions.push(center); // +0