Skip to content

Commit fbee8ad

Browse files
authored
Merge pull request #1467 from alembcke/gradient
Align axes and parametric functions/surfaces when using :class:`Axes` and :class:`ThreeDAxes`
2 parents fb085d4 + ef2fd8b commit fbee8ad

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

manim/mobject/coordinate_systems.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import math
77
import numbers
8-
from typing import Iterable, Optional, Sequence
8+
from typing import Iterable, List, Optional, Sequence
99

1010
import numpy as np
1111

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

249-
# without the if/elif, graph does not exist when min > 0 or max < 0
249+
# without the call to origin_shift, graph does not exist when min > 0 or max < 0
250250
# shifts the axis so that 0 is centered
251-
if range_terms[0] > 0:
252-
axis.shift(-axis.number_to_point(range_terms[0]))
253-
elif range_terms[1] < 0:
254-
axis.shift(-axis.number_to_point(range_terms[1]))
255-
else:
256-
axis.shift(-axis.number_to_point(0))
251+
axis.shift(-axis.number_to_point(self.origin_shift(range_terms)))
257252
return axis
258253

259254
def coords_to_point(self, *coords):
260-
origin = self.x_axis.number_to_point(0)
255+
origin = self.x_axis.number_to_point(self.origin_shift(self.x_range))
261256
result = np.array(origin)
262257
for axis, coord in zip(self.get_axes(), coords):
263258
result += axis.number_to_point(coord) - origin
@@ -367,6 +362,22 @@ def construct(self):
367362

368363
return line_graph
369364

365+
@staticmethod
366+
def origin_shift(axis_range: List[float]) -> float:
367+
"""Determines how to shift graph mobjects to compensate when 0 is not on the axis.
368+
369+
Parameters
370+
----------
371+
axis_range
372+
The range of the axis : ``(x_min, x_max, x_step)``.
373+
"""
374+
if axis_range[0] > 0:
375+
return axis_range[0]
376+
if axis_range[1] < 0:
377+
return axis_range[1]
378+
else:
379+
return 0
380+
370381

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

449460
self.axes.add(z_axis)
450461
self.add(z_axis)

tests/test_axes_shift.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
import pytest
33

4+
from manim.mobject.coordinate_systems import Axes
45
from manim.scene.graph_scene import GraphScene
56

67

@@ -42,3 +43,9 @@ def test_axes_with_xy_shift():
4243
G.setup_axes()
4344
assert all(np.isclose(G.graph_origin, G.x_axis.n2p(1)))
4445
assert all(np.isclose(G.graph_origin, G.y_axis.n2p(-5)))
46+
47+
48+
def test_axes_origin_shift():
49+
ax = Axes(x_range=(5, 10, 1), y_range=(40, 45, 0.5))
50+
assert np.allclose(ax.coords_to_point(5, 40), ax.x_axis.number_to_point(5))
51+
assert np.allclose(ax.coords_to_point(5, 40), ax.y_axis.number_to_point(40))

0 commit comments

Comments
 (0)