Speed up bind_graph_to_func

This commit is contained in:
Grant Sanderson 2023-01-19 09:50:24 -08:00
parent 148898f983
commit e6abff4299

View file

@ -54,7 +54,7 @@ class CoordinateSystem(ABC):
self, self,
x_range: RangeSpecifier = DEFAULT_X_RANGE, x_range: RangeSpecifier = DEFAULT_X_RANGE,
y_range: RangeSpecifier = DEFAULT_Y_RANGE, y_range: RangeSpecifier = DEFAULT_Y_RANGE,
num_sampled_graph_points_per_tick: int = 20, num_sampled_graph_points_per_tick: int = 5,
): ):
self.x_range = x_range self.x_range = x_range
self.y_range = y_range self.y_range = y_range
@ -241,21 +241,20 @@ class CoordinateSystem(ABC):
Use for graphing functions which might change over time, or change with Use for graphing functions which might change over time, or change with
conditions conditions
""" """
x_values = [self.x_axis.p2n(p) for p in graph.get_points()] x_values = np.array([self.x_axis.p2n(p) for p in graph.get_points()])
def get_x_values(): def get_graph_points():
xs = x_values
if get_discontinuities: if get_discontinuities:
ds = get_discontinuities() ds = get_discontinuities()
ep = 1e-6 ep = 1e-6
added_xs = it.chain(*((d - ep, d + ep) for d in ds)) added_xs = it.chain(*((d - ep, d + ep) for d in ds))
return sorted([*x_values, *added_xs])[:len(x_values)] xs[:] = sorted([*x_values, *added_xs])[:len(x_values)]
else: return self.c2p(xs, func(xs))
return x_values
graph.add_updater(lambda g: g.set_points_as_corners([ graph.add_updater(lambda g: g.set_points_as_corners(
self.c2p(x, func(x)) get_graph_points()
for x in get_x_values() ))
]))
if not jagged: if not jagged:
graph.add_updater(lambda g: g.make_approximately_smooth()) graph.add_updater(lambda g: g.make_approximately_smooth())
return graph return graph