Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

feat: adding three new rate_functions based on the SmoothStep function. These have the added benefits of zero derivatives at the endpoints. (#3361)

Uwe Zimmermannbff2ea444203
diff --git a/manim/utils/rate_functions.py b/manim/utils/rate_functions.py
index 261199b52..dec857969 100644
--- a/manim/utils/rate_functions.py
+++ b/manim/utils/rate_functions.py
@@ -89,6 +89,9 @@ from __future__ import annotations
__all__ = [
"linear",
"smooth",
+ "smoothstep",
+ "smootherstep",
+ "smoothererstep",
"rush_into",
"rush_from",
"slow_into",
@@ -155,6 +158,36 @@ def smooth(t: float, inflection: float = 10.0) -> float:
)
+def smoothstep(t: float) -> float:
+ """Implementation of the 1st order SmoothStep sigmoid function.
+ The 1st derivative (speed) is zero at the endpoints.
+ https://en.wikipedia.org/wiki/Smoothstep
+ """
+ return 0 if t <= 0 else 3 * t**2 - 2 * t**3 if t < 1 else 1
+
+
+def smootherstep(t: float) -> float:
+ """Implementation of the 2nd order SmoothStep sigmoid function.
+ The 1st and 2nd derivatives (speed and acceleration) are zero at the endpoints.
+ https://en.wikipedia.org/wiki/Smoothstep
+ """
+ return 0 if t <= 0 else 6 * t**5 - 15 * t**4 + 10 * t**3 if t < 1 else 1
+
+
+def smoothererstep(t: float) -> float:
+ """Implementation of the 3rd order SmoothStep sigmoid function.
+ The 1st, 2nd and 3rd derivatives (speed, acceleration and jerk) are zero at the endpoints.
+ https://en.wikipedia.org/wiki/Smoothstep
+ """
+ return (
+ 0
+ if t <= 0
+ else 35 * t**4 - 84 * t**5 + 70 * t**6 - 20 * t**7
+ if t < 1
+ else 1
+ )
+
+
@unit_interval
def rush_into(t: float, inflection: float = 10.0) -> float:
return 2 * smooth(t / 2.0, inflection)