rotation.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from __future__ import annotations
  2. from manimlib.animation.animation import Animation
  3. from manimlib.constants import ORIGIN, OUT
  4. from manimlib.constants import PI, TAU
  5. from manimlib.utils.rate_functions import linear
  6. from manimlib.utils.rate_functions import smooth
  7. from typing import TYPE_CHECKING
  8. if TYPE_CHECKING:
  9. import numpy as np
  10. from typing import Callable
  11. from manimlib.mobject.mobject import Mobject
  12. class Rotating(Animation):
  13. def __init__(
  14. self,
  15. mobject: Mobject,
  16. angle: float = TAU,
  17. axis: np.ndarray = OUT,
  18. about_point: np.ndarray | None = None,
  19. about_edge: np.ndarray | None = None,
  20. run_time: float = 5.0,
  21. rate_func: Callable[[float], float] = linear,
  22. suspend_mobject_updating: bool = False,
  23. **kwargs
  24. ):
  25. self.angle = angle
  26. self.axis = axis
  27. self.about_point = about_point
  28. self.about_edge = about_edge
  29. super().__init__(
  30. mobject,
  31. run_time=run_time,
  32. rate_func=rate_func,
  33. suspend_mobject_updating=suspend_mobject_updating,
  34. **kwargs
  35. )
  36. def interpolate_mobject(self, alpha: float) -> None:
  37. pairs = zip(
  38. self.mobject.family_members_with_points(),
  39. self.starting_mobject.family_members_with_points(),
  40. )
  41. for sm1, sm2 in pairs:
  42. for key in sm1.pointlike_data_keys:
  43. sm1.data[key][:] = sm2.data[key]
  44. self.mobject.rotate(
  45. self.rate_func(self.time_spanned_alpha(alpha)) * self.angle,
  46. axis=self.axis,
  47. about_point=self.about_point,
  48. about_edge=self.about_edge,
  49. )
  50. class Rotate(Rotating):
  51. def __init__(
  52. self,
  53. mobject: Mobject,
  54. angle: float = PI,
  55. axis: np.ndarray = OUT,
  56. run_time: float = 1,
  57. rate_func: Callable[[float], float] = smooth,
  58. about_edge: np.ndarray = ORIGIN,
  59. **kwargs
  60. ):
  61. super().__init__(
  62. mobject, angle, axis,
  63. run_time=run_time,
  64. rate_func=rate_func,
  65. about_edge=about_edge,
  66. **kwargs
  67. )