Skip to content

Commit edc814a

Browse files
committed
Math/Easing
1 parent b1b1192 commit edc814a

File tree

6 files changed

+159
-1
lines changed

6 files changed

+159
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
- [`ChronoLogger`](modules/Log/ChronoLogger.mpp) - RAII timer that logs elapsed time at scope exit
6363

6464
### 🧮 Math
65+
- [`Easing`](modules/Math/Easing.mpp) - Collection of easing functions for smooth animation
6566
- [`Random`](modules/Math/Random.mpp) - Pseudorandom number generation
6667
- [`Utility`](modules/Math/Utility.mpp) - Floating-point comparison with epsilon tolerance
6768

modules/Execution/EventDispatcher.mpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export namespace CppUtils::Execution
2121
{
2222
auto payload = std::make_tuple(args...);
2323
for (auto& function : subscriberIt->second)
24-
function(&payload);
24+
function(std::addressof(payload));
2525
}
2626
}
2727

modules/Math/Easing.mpp

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
export module CppUtils.Math.Easing;
2+
3+
import std;
4+
5+
export namespace CppUtils::Math::Easing
6+
{
7+
[[nodiscard]] constexpr auto inQuad(std::floating_point auto value) noexcept -> auto
8+
{
9+
return value * value;
10+
}
11+
12+
[[nodiscard]] constexpr auto outQuad(std::floating_point auto value) noexcept -> auto
13+
{
14+
return value * (2 - value);
15+
}
16+
17+
[[nodiscard]] constexpr auto inOutQuad(std::floating_point auto value) noexcept -> auto
18+
{
19+
return value < 0.5 ? 2 * value * value : -1 + (4 - 2 * value) * value;
20+
}
21+
22+
[[nodiscard]] constexpr auto inCubic(std::floating_point auto value) noexcept -> auto
23+
{
24+
return value * value * value;
25+
}
26+
27+
[[nodiscard]] constexpr auto outCubic(std::floating_point auto value) noexcept -> auto
28+
{
29+
auto temp = value - 1;
30+
return temp * temp * temp + 1;
31+
}
32+
33+
[[nodiscard]] constexpr auto inOutCubic(std::floating_point auto value) noexcept -> auto
34+
{
35+
if (value < 0.5)
36+
return 4 * value * value * value;
37+
38+
auto temp = -2 * value + 2;
39+
return 1 - (temp * temp * temp) / 2;
40+
}
41+
42+
[[nodiscard]] constexpr auto inQuart(std::floating_point auto value) noexcept -> auto
43+
{
44+
return value * value * value * value;
45+
}
46+
47+
[[nodiscard]] constexpr auto outQuart(std::floating_point auto value) noexcept -> auto
48+
{
49+
auto temp = value - 1;
50+
return 1 - temp * temp * temp * temp;
51+
}
52+
53+
[[nodiscard]] constexpr auto inOutQuart(std::floating_point auto value) noexcept -> auto
54+
{
55+
if (value < 0.5)
56+
return 8 * value * value * value * value;
57+
58+
auto temp = -2 * value + 2;
59+
return 1 - (temp * temp * temp * temp) / 2;
60+
}
61+
62+
[[nodiscard]] constexpr auto inQuint(std::floating_point auto value) noexcept -> auto
63+
{
64+
return value * value * value * value * value;
65+
}
66+
67+
[[nodiscard]] constexpr auto outQuint(std::floating_point auto value) noexcept -> auto
68+
{
69+
auto temp = value - 1;
70+
return 1 + temp * temp * temp * temp * temp;
71+
}
72+
73+
[[nodiscard]] constexpr auto inOutQuint(std::floating_point auto value) noexcept -> auto
74+
{
75+
if (value < 0.5)
76+
return 16 * value * value * value * value * value;
77+
78+
auto temp = -2 * value + 2;
79+
return 1 - (temp * temp * temp * temp * temp) / 2;
80+
}
81+
82+
[[nodiscard]] constexpr auto inSine(std::floating_point auto value) noexcept -> auto
83+
{
84+
return 1 - std::cos((value * std::numbers::pi_v<decltype(value)>) / 2);
85+
}
86+
87+
[[nodiscard]] constexpr auto outSine(std::floating_point auto value) noexcept -> auto
88+
{
89+
return std::sin((value * std::numbers::pi_v<decltype(value)>) / 2);
90+
}
91+
92+
[[nodiscard]] constexpr auto inOutSine(std::floating_point auto value) noexcept -> auto
93+
{
94+
return -(std::cos(std::numbers::pi_v<decltype(value)> * value) - 1) / 2;
95+
}
96+
97+
[[nodiscard]] constexpr auto inCirc(std::floating_point auto value) noexcept -> auto
98+
{
99+
return 1 - std::sqrt(1 - std::pow(value, 2));
100+
}
101+
102+
[[nodiscard]] constexpr auto outCirc(std::floating_point auto value) noexcept -> auto
103+
{
104+
return std::sqrt(1 - (value - 1) * (value - 1));
105+
}
106+
107+
[[nodiscard]] constexpr auto inOutCirc(std::floating_point auto value) noexcept -> auto
108+
{
109+
if (value < 0.5)
110+
return (1 - std::sqrt(1 - 4 * value * value)) / 2;
111+
112+
auto temp = -2 * value + 2;
113+
return (std::sqrt(1 - temp * temp) + 1) / 2;
114+
}
115+
}

modules/Math/Math.mpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export module CppUtils.Math;
22

33
export import CppUtils.Math.Concept;
4+
export import CppUtils.Math.Easing;
45
export import CppUtils.Math.Endian;
56
export import CppUtils.Math.Random;
67
export import CppUtils.Math.Utility;

modules/Math/Utility.mpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@ export namespace CppUtils::Math
2727
static_assert(absolute(0u) == 0u);
2828
static_assert(absolute(1u) == 1u);
2929

30+
template<std::floating_point Float>
31+
[[nodiscard]] inline constexpr auto isEqual(Float lhs, Float rhs, Float epsilon = std::numeric_limits<Float>::epsilon()) noexcept -> bool
32+
{
33+
auto difference = absolute(lhs - rhs);
34+
auto maximum = std::max(absolute(lhs), absolute(rhs));
35+
if (maximum >= Float{1})
36+
return difference <= epsilon * maximum;
37+
else if (maximum > Float{0})
38+
return difference / maximum <= epsilon;
39+
return true;
40+
}
41+
42+
static_assert(isEqual(0.0, 0.0));
43+
static_assert(isEqual(0.1 + 0.2, 0.3));
44+
static_assert(not isEqual(0.1 + 0.2, 0.29));
45+
static_assert(not isEqual(0.1 + 0.2, 0.31));
46+
3047
template<Type::Numeric T>
3148
[[nodiscard]] inline constexpr auto isBetween(T value, T low, T high) noexcept -> bool
3249
{

tests/Terminal/Canvas.mpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,30 @@ export namespace CppUtils::UnitTest::Terminal::Canvas
8484
canvas.wait();
8585
});
8686

87+
suite.addTest("Multiple Progress Bars", [&] {
88+
auto canvas = CppUtils::Terminal::v2::Canvas{CppUtils::Container::Size2{50, 3}};
89+
auto& layout = canvas.addWidget(std::make_unique<CppUtils::Terminal::Layout>());
90+
91+
auto& bar1 = layout.addWidget(std::make_unique<CppUtils::Terminal::ProgressBar>("Bar 1"));
92+
auto& bar2 = layout.addWidget(std::make_unique<CppUtils::Terminal::ProgressBar>("Bar 2"));
93+
auto& bar3 = layout.addWidget(std::make_unique<CppUtils::Terminal::ProgressBar>("Bar 3"));
94+
95+
auto scheduler = CppUtils::Thread::Scheduler{};
96+
scheduler.schedule([&canvas]() mutable {
97+
canvas.close();
98+
}, 500ms);
99+
100+
for (auto i = 0.f; i <= 100.f; i += 2.f)
101+
{
102+
bar1.setPercent(i);
103+
bar2.setPercent(CppUtils::Math::Easing::inQuad(i / 100.f) * 100.f);
104+
bar3.setPercent(CppUtils::Math::Easing::outCubic(i / 100.f) * 100.f);
105+
std::this_thread::sleep_for(5ms);
106+
}
107+
108+
canvas.wait();
109+
});
110+
87111
suite.addTest("Bouncing ball", [&] {
88112
const auto terminalSize = CppUtils::Terminal::getTerminalSize();
89113
auto canvasSize = terminalSize;

0 commit comments

Comments
 (0)