Skip to content

Commit

Permalink
Merge pull request #1467 from alembcke/gradient
Browse files Browse the repository at this point in the history
Align axes and parametric functions/surfaces when using :class:`Axes` and :class:`ThreeDAxes`
  • Loading branch information
hydrobeam authored May 20, 2021
2 parents fb085d4 + ef2fd8b commit fbee8ad
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
31 changes: 21 additions & 10 deletions manim/mobject/coordinate_systems.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import math
import numbers
from typing import Iterable, Optional, Sequence
from typing import Iterable, List, Optional, Sequence

import numpy as np

Expand Down Expand Up @@ -246,18 +246,13 @@ def create_axis(self, range_terms, axis_config, length):
new_config["length"] = length
axis = NumberLine(range_terms, **new_config)

# without the if/elif, graph does not exist when min > 0 or max < 0
# without the call to origin_shift, graph does not exist when min > 0 or max < 0
# shifts the axis so that 0 is centered
if range_terms[0] > 0:
axis.shift(-axis.number_to_point(range_terms[0]))
elif range_terms[1] < 0:
axis.shift(-axis.number_to_point(range_terms[1]))
else:
axis.shift(-axis.number_to_point(0))
axis.shift(-axis.number_to_point(self.origin_shift(range_terms)))
return axis

def coords_to_point(self, *coords):
origin = self.x_axis.number_to_point(0)
origin = self.x_axis.number_to_point(self.origin_shift(self.x_range))
result = np.array(origin)
for axis, coord in zip(self.get_axes(), coords):
result += axis.number_to_point(coord) - origin
Expand Down Expand Up @@ -367,6 +362,22 @@ def construct(self):

return line_graph

@staticmethod
def origin_shift(axis_range: List[float]) -> float:
"""Determines how to shift graph mobjects to compensate when 0 is not on the axis.
Parameters
----------
axis_range
The range of the axis : ``(x_min, x_max, x_step)``.
"""
if axis_range[0] > 0:
return axis_range[0]
if axis_range[1] < 0:
return axis_range[1]
else:
return 0


class ThreeDAxes(Axes):
"""A 3-dimensional set of axes.
Expand Down Expand Up @@ -444,7 +455,7 @@ def __init__(
z_axis = self.create_axis(self.z_range, self.z_axis_config, self.z_length)
z_axis.rotate_about_zero(-PI / 2, UP)
z_axis.rotate_about_zero(angle_of_vector(self.z_normal))
z_axis.shift(self.x_axis.n2p(0))
z_axis.shift(self.x_axis.number_to_point(self.origin_shift(x_range)))

self.axes.add(z_axis)
self.add(z_axis)
Expand Down
7 changes: 7 additions & 0 deletions tests/test_axes_shift.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numpy as np
import pytest

from manim.mobject.coordinate_systems import Axes
from manim.scene.graph_scene import GraphScene


Expand Down Expand Up @@ -42,3 +43,9 @@ def test_axes_with_xy_shift():
G.setup_axes()
assert all(np.isclose(G.graph_origin, G.x_axis.n2p(1)))
assert all(np.isclose(G.graph_origin, G.y_axis.n2p(-5)))


def test_axes_origin_shift():
ax = Axes(x_range=(5, 10, 1), y_range=(40, 45, 0.5))
assert np.allclose(ax.coords_to_point(5, 40), ax.x_axis.number_to_point(5))
assert np.allclose(ax.coords_to_point(5, 40), ax.y_axis.number_to_point(40))

0 comments on commit fbee8ad

Please sign in to comment.