Skip to content

Commit bff2ea4

Browse files
authored
feat: adding three new rate_functions based on the SmoothStep function. These have the added benefits of zero derivatives at the endpoints. (#3361)
1 parent 50d663e commit bff2ea4

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

manim/utils/rate_functions.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ def construct(self):
8989
__all__ = [
9090
"linear",
9191
"smooth",
92+
"smoothstep",
93+
"smootherstep",
94+
"smoothererstep",
9295
"rush_into",
9396
"rush_from",
9497
"slow_into",
@@ -155,6 +158,36 @@ def smooth(t: float, inflection: float = 10.0) -> float:
155158
)
156159

157160

161+
def smoothstep(t: float) -> float:
162+
"""Implementation of the 1st order SmoothStep sigmoid function.
163+
The 1st derivative (speed) is zero at the endpoints.
164+
https://en.wikipedia.org/wiki/Smoothstep
165+
"""
166+
return 0 if t <= 0 else 3 * t**2 - 2 * t**3 if t < 1 else 1
167+
168+
169+
def smootherstep(t: float) -> float:
170+
"""Implementation of the 2nd order SmoothStep sigmoid function.
171+
The 1st and 2nd derivatives (speed and acceleration) are zero at the endpoints.
172+
https://en.wikipedia.org/wiki/Smoothstep
173+
"""
174+
return 0 if t <= 0 else 6 * t**5 - 15 * t**4 + 10 * t**3 if t < 1 else 1
175+
176+
177+
def smoothererstep(t: float) -> float:
178+
"""Implementation of the 3rd order SmoothStep sigmoid function.
179+
The 1st, 2nd and 3rd derivatives (speed, acceleration and jerk) are zero at the endpoints.
180+
https://en.wikipedia.org/wiki/Smoothstep
181+
"""
182+
return (
183+
0
184+
if t <= 0
185+
else 35 * t**4 - 84 * t**5 + 70 * t**6 - 20 * t**7
186+
if t < 1
187+
else 1
188+
)
189+
190+
158191
@unit_interval
159192
def rush_into(t: float, inflection: float = 10.0) -> float:
160193
return 2 * smooth(t / 2.0, inflection)

0 commit comments

Comments
 (0)