Skip to content

Commit

Permalink
feat: adding three new rate_functions based on the SmoothStep functio…
Browse files Browse the repository at this point in the history
…n. These have the added benefits of zero derivatives at the endpoints. (ManimCommunity#3361)
  • Loading branch information
uwezi authored Sep 9, 2023
1 parent 50d663e commit bff2ea4
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions manim/utils/rate_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ def construct(self):
__all__ = [
"linear",
"smooth",
"smoothstep",
"smootherstep",
"smoothererstep",
"rush_into",
"rush_from",
"slow_into",
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit bff2ea4

Please sign in to comment.