1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 23:27:35 +00:00

AK: Add to_radians and to_degrees math functions

This commit is contained in:
Bastiaan van der Plaat 2023-09-09 14:43:39 +02:00 committed by Sam Atkins
parent 9b7aa8f6b6
commit 494a8cb816
15 changed files with 40 additions and 40 deletions

View file

@ -145,7 +145,7 @@ void GLContext::gl_rotate(GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
FloatVector3 axis = { x, y, z };
if (axis.length() > 0.f)
axis.normalize();
auto rotation_mat = Gfx::rotation_matrix(axis, angle * static_cast<float>(M_PI * 2 / 360));
auto rotation_mat = Gfx::rotation_matrix(axis, AK::to_radians(angle));
update_current_matrix(*m_current_matrix * rotation_mat);
}

View file

@ -5,6 +5,7 @@
*/
#include <AK/Format.h>
#include <AK/Math.h>
#include <LibGfx/DeltaE.h>
#include <math.h>
@ -39,7 +40,7 @@ float DeltaE(CIELAB const& c1, CIELAB const& c2)
float h_prime = atan2(b, a_prime);
if (h_prime < 0)
h_prime += 2 * static_cast<float>(M_PI);
return h_prime * 180 / static_cast<float>(M_PI);
return AK::to_degrees(h_prime);
};
float h1_prime = h_prime(c1.b, a1_prime);
float h2_prime = h_prime(c2.b, a2_prime);
@ -54,8 +55,8 @@ float DeltaE(CIELAB const& c1, CIELAB const& c2)
else
delta_h_prime = h2_prime - h1_prime - 360;
auto sin_degrees = [](float x) { return sinf(x * static_cast<float>(M_PI) / 180); };
auto cos_degrees = [](float x) { return cosf(x * static_cast<float>(M_PI) / 180); };
auto sin_degrees = [](float x) { return sinf(AK::to_radians(x)); };
auto cos_degrees = [](float x) { return cosf(AK::to_radians(x)); };
float delta_H_prime = 2 * sqrtf(C1_prime * C2_prime) * sin_degrees(delta_h_prime / 2);

View file

@ -27,7 +27,7 @@ public:
private:
static FloatMatrix3x3 calculate_hue_rotate_matrix(float angle_degrees)
{
float angle_rads = angle_degrees * (AK::Pi<float> / 180);
float angle_rads = AK::to_radians(angle_degrees);
float cos_angle = 0;
float sin_angle = 0;
AK::sincos(angle_rads, sin_angle, cos_angle);

View file

@ -241,7 +241,7 @@ static auto create_conic_gradient(ReadonlySpan<ColorStop> color_stops, FloatPoin
[=](int x, int y) {
auto point = FloatPoint { x, y } - center_point;
// FIXME: We could probably get away with some approximation here:
auto loc = fmod((AK::atan2(point.y(), point.x()) * 180.0f / AK::Pi<float> + 360.0f + normalized_start_angle), 360.0f);
auto loc = fmod((AK::to_degrees(AK::atan2(point.y(), point.x())) + 360.0f + normalized_start_angle), 360.0f);
return should_floor_angles ? floor(loc) : loc;
}
};
@ -256,7 +256,7 @@ static auto create_radial_gradient(IntRect const& physical_rect, ReadonlySpan<Co
auto center_point = FloatPoint { center }.translated(0.5, 0.5);
AffineTransform rotation_transform;
if (rotation_angle.has_value()) {
auto angle_as_radians = rotation_angle.value() * (AK::Pi<float> / 180);
auto angle_as_radians = AK::to_radians(rotation_angle.value());
rotation_transform.rotate_radians(angle_as_radians);
}

View file

@ -24,7 +24,7 @@ inline float normalized_gradient_angle_radians(float gradient_angle)
{
// Adjust angle so 0 degrees is bottom
float real_angle = 90 - gradient_angle;
return real_angle * (AK::Pi<float> / 180);
return AK::to_radians(real_angle);
}
template<typename T>

View file

@ -904,7 +904,7 @@ void Device::calculate_vertex_lighting(GPU::Vertex& vertex) const
float spotlight_factor = 1.0f;
if (light.spotlight_cutoff_angle != 180.0f) {
auto const vertex_to_light_dot_spotlight_direction = sgi_dot_operator(vertex_to_light, light.spotlight_direction.normalized());
auto const cos_spotlight_cutoff = AK::cos<float>(light.spotlight_cutoff_angle * AK::Pi<float> / 180.f);
auto const cos_spotlight_cutoff = AK::cos<float>(AK::to_radians(light.spotlight_cutoff_angle));
if (vertex_to_light_dot_spotlight_direction >= cos_spotlight_cutoff)
spotlight_factor = AK::pow<float>(vertex_to_light_dot_spotlight_direction, light.spotlight_exponent);

View file

@ -39,7 +39,7 @@ double Angle::to_degrees() const
case Type::Grad:
return m_value * (360.0 / 400.0);
case Type::Rad:
return m_value * (180.0 / AK::Pi<double>);
return AK::to_degrees(m_value);
case Type::Turn:
return m_value * 360.0;
}
@ -48,7 +48,7 @@ double Angle::to_degrees() const
double Angle::to_radians() const
{
return to_degrees() * (AK::Pi<double> / 180.0);
return AK::to_radians(to_degrees());
}
StringView Angle::unit_name() const

View file

@ -66,7 +66,7 @@ bool LinearGradientStyleValue::equals(StyleValue const& other_) const
float LinearGradientStyleValue::angle_degrees(CSSPixelSize gradient_size) const
{
auto corner_angle_degrees = [&] {
return atan2(gradient_size.height().to_double(), gradient_size.width().to_double()) * 180 / AK::Pi<double>;
return AK::to_degrees(atan2(gradient_size.height().to_double(), gradient_size.width().to_double()));
};
return m_properties.direction.visit(
[&](SideOrCorner side_or_corner) {

View file

@ -313,7 +313,7 @@ JS::NonnullGCPtr<DOMMatrix> DOMMatrix::skew_x_self(double sx)
{
// 1. Post-multiply a skewX transformation on the current matrix by the specified angle sx in degrees. The 2D skewX matrix is described in CSS Transforms with alpha = sx in degrees. [CSS3-TRANSFORMS]
// clang-format off
Gfx::DoubleMatrix4x4 skew_matrix = { 1, tan(sx * M_PI / 180.0), 0, 0,
Gfx::DoubleMatrix4x4 skew_matrix = { 1, tan(AK::to_radians(sx)), 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 };
@ -330,7 +330,7 @@ JS::NonnullGCPtr<DOMMatrix> DOMMatrix::skew_y_self(double sy)
// 1. Post-multiply a skewX transformation on the current matrix by the specified angle sy in degrees. The 2D skewY matrix is described in CSS Transforms with beta = sy in degrees. [CSS3-TRANSFORMS]
// clang-format off
Gfx::DoubleMatrix4x4 skew_matrix = { 1, 0, 0, 0,
tan(sy * M_PI / 180.0), 1, 0, 0,
tan(AK::to_radians(sy)), 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1 };
// clang-format on

View file

@ -74,9 +74,6 @@ Optional<Gfx::PaintStyle const&> SVGGraphicsElement::stroke_paint_style(SVGPaint
Gfx::AffineTransform transform_from_transform_list(ReadonlySpan<Transform> transform_list)
{
Gfx::AffineTransform affine_transform;
auto to_radians = [](float degrees) {
return degrees * (AK::Pi<float> / 180.0f);
};
for (auto& transform : transform_list) {
transform.operation.visit(
[&](Transform::Translate const& translate) {
@ -90,14 +87,14 @@ Gfx::AffineTransform transform_from_transform_list(ReadonlySpan<Transform> trans
affine_transform.multiply(
Gfx::AffineTransform {}
.translate({ rotate.x, rotate.y })
.rotate_radians(to_radians(rotate.a))
.rotate_radians(AK::to_radians(rotate.a))
.translate({ -rotate.x, -rotate.y }));
},
[&](Transform::SkewX const& skew_x) {
affine_transform.multiply(Gfx::AffineTransform {}.skew_radians(to_radians(skew_x.a), 0));
affine_transform.multiply(Gfx::AffineTransform {}.skew_radians(AK::to_radians(skew_x.a), 0));
},
[&](Transform::SkewY const& skew_y) {
affine_transform.multiply(Gfx::AffineTransform {}.skew_radians(0, to_radians(skew_y.a)));
affine_transform.multiply(Gfx::AffineTransform {}.skew_radians(0, AK::to_radians(skew_y.a)));
},
[&](Transform::Matrix const& matrix) {
affine_transform.multiply(Gfx::AffineTransform {