def add_polygons(self): a = self.i_hat.get_end()[0]*RIGHT b = self.j_hat.get_end()[0]*RIGHT c = self.i_hat.get_end()[1]*UP d = self.j_hat.get_end()[1]*UP shapes_colors_and_tex = [ (Polygon(ORIGIN, a, a+c), TEAL, "bd/2"), (Polygon(ORIGIN, d+b, d), TEAL, "\\dfrac{bd}{2}"), (Polygon(a+c, a+b+c, a+b+c+d), MAROON, "\\dfrac{ac}{2}"), (Polygon(b+d, a+b+c+d, b+c+d), MAROON, "ac/2"), (Polygon(a, a+b, a+b+c, a+c), PINK, "bc"), (Polygon(d, d+b, d+b+c, d+c), PINK, "bc"), ] everyone = VMobject() for shape, color, tex in shapes_colors_and_tex: shape.set_stroke(width = 0) shape.set_fill(color = color, opacity = 0.7) tex_mob = TexMobject(tex) tex_mob.scale(0.7) tex_mob.move_to(shape.get_center_of_mass()) everyone.add(shape, tex_mob) self.play(FadeIn( everyone, submobject_mode = "lagged_start", run_time = 1 ))
def construct(self): self.setup() blob = Blob( height = self.blob_height, random_seed = 5, random_nudge_size = 0.2, ) blob.next_to(ORIGIN, UP+RIGHT) self.add_transformable_mobject(blob) arange = np.arange( 0, self.blob_height + self.square_size, self.square_size ) square = Square(side_length = self.square_size) square.set_stroke(YELLOW, width = 2) square.set_fill(YELLOW, opacity = 0.3) squares = VMobject() for x, y in it.product(*[arange]*2): point = x*RIGHT + y*UP if blob.probably_contains(point): squares.add(square.copy().shift(point)) self.play(ShowCreation( squares, submobject_mode = "lagged_start", run_time = 2, )) self.add_transformable_mobject(squares) self.dither() self.apply_transposed_matrix([[1, -1], [0.5, 1]]) self.dither()
def construct(self): self.setup() blob = Blob( height=self.blob_height, random_seed=5, random_nudge_size=0.2, ) blob.next_to(ORIGIN, UP + RIGHT) self.add_transformable_mobject(blob) arange = np.arange(0, self.blob_height + self.square_size, self.square_size) square = Square(side_length=self.square_size) square.set_stroke(YELLOW, width=2) square.set_fill(YELLOW, opacity=0.3) squares = VMobject() for x, y in it.product(*[arange] * 2): point = x * RIGHT + y * UP if blob.probably_contains(point): squares.add(square.copy().shift(point)) self.play( ShowCreation( squares, submobject_mode="lagged_start", run_time=2, )) self.add_transformable_mobject(squares) self.wait() self.apply_transposed_matrix([[1, -1], [0.5, 1]]) self.wait()
def add_braces(self): a = self.i_hat.get_end()[0]*RIGHT b = self.j_hat.get_end()[0]*RIGHT c = self.i_hat.get_end()[1]*UP d = self.j_hat.get_end()[1]*UP quads = [ (ORIGIN, a, DOWN, "a"), (a, a+b, DOWN, "b"), (a+b, a+b+c, RIGHT, "c"), (a+b+c, a+b+c+d, RIGHT, "d"), (a+b+c+d, a+c+d, UP, "a"), (a+c+d, d+c, UP, "b"), (d+c, d, LEFT, "c"), (d, ORIGIN, LEFT, "d"), ] everyone = VMobject() for p1, p2, direction, char in quads: line = Line(p1, p2) brace = Brace(line, direction, buff = 0) text = brace.get_text(char) text.add_background_rectangle() if char in ["a", "c"]: text.highlight(X_COLOR) else: text.highlight(Y_COLOR) everyone.add(brace, text) self.play(Write(everyone), run_time = 1)
def add_braces(self): a = self.i_hat.get_end()[0] * RIGHT b = self.j_hat.get_end()[0] * RIGHT c = self.i_hat.get_end()[1] * UP d = self.j_hat.get_end()[1] * UP quads = [ (ORIGIN, a, DOWN, "a"), (a, a + b, DOWN, "b"), (a + b, a + b + c, RIGHT, "c"), (a + b + c, a + b + c + d, RIGHT, "d"), (a + b + c + d, b + c + d, UP, "a"), (b + c + d, d + c, UP, "b"), (d + c, d, LEFT, "c"), (d, ORIGIN, LEFT, "d"), ] everyone = VMobject() for p1, p2, direction, char in quads: line = Line(p1, p2) brace = Brace(line, direction, buff=0) text = brace.get_text(char) text.add_background_rectangle() if char in ["a", "c"]: text.highlight(X_COLOR) else: text.highlight(Y_COLOR) everyone.add(brace, text) self.play(Write(everyone), run_time=1)
def add_lines(self, left, right): line_kwargs = { "color": BLUE, "stroke_width": 2, } left_rows = [VMobject(*row) for row in left.get_mob_matrix()] h_lines = VMobject() for row in left_rows[:-1]: h_line = Line(row.get_left(), row.get_right(), **line_kwargs) h_line.next_to(row, DOWN, buff=left.v_buff / 2.) h_lines.add(h_line) right_cols = [ VMobject(*col) for col in np.transpose(right.get_mob_matrix()) ] v_lines = VMobject() for col in right_cols[:-1]: v_line = Line(col.get_top(), col.get_bottom(), **line_kwargs) v_line.next_to(col, RIGHT, buff=right.h_buff / 2.) v_lines.add(v_line) self.play(ShowCreation(h_lines)) self.play(ShowCreation(v_lines)) self.wait() self.show_frame()
def add_lines(self, left, right): line_kwargs = { "color" : BLUE, "stroke_width" : 2, } left_rows = [ VMobject(*row) for row in left.get_mob_matrix() ] h_lines = VMobject() for row in left_rows[:-1]: h_line = Line(row.get_left(), row.get_right(), **line_kwargs) h_line.next_to(row, DOWN, buff = left.v_buff/2.) h_lines.add(h_line) right_cols = [ VMobject(*col) for col in np.transpose(right.get_mob_matrix()) ] v_lines = VMobject() for col in right_cols[:-1]: v_line = Line(col.get_top(), col.get_bottom(), **line_kwargs) v_line.next_to(col, RIGHT, buff = right.h_buff/2.) v_lines.add(v_line) self.play(ShowCreation(h_lines)) self.play(ShowCreation(v_lines)) self.dither() self.show_frame()
def get_det_text(matrix, determinant = None): parens = TexMobject(["(", ")"]) parens.scale(2) parens.stretch_to_fit_height(matrix.get_height()) l_paren, r_paren = parens.split() l_paren.next_to(matrix, LEFT, buff = 0.1) r_paren.next_to(matrix, RIGHT, buff = 0.1) det = TextMobject("det").next_to(l_paren, LEFT, buff = 0.1) det.add_background_rectangle() det_text = VMobject(det, l_paren, r_paren) if determinant is not None: eq = TexMobject("=") eq.next_to(r_paren, RIGHT, buff = 0.1) result = TexMobject(str(determinant)) result.next_to(eq, RIGHT, buff = 0.2) det_text.add(eq, result) return det_text
def get_det_text(matrix, determinant=None, background_rect=True): parens = TexMobject(["(", ")"]) parens.scale(2) parens.stretch_to_fit_height(matrix.get_height()) l_paren, r_paren = parens.split() l_paren.next_to(matrix, LEFT, buff=0.1) r_paren.next_to(matrix, RIGHT, buff=0.1) det = TextMobject("det").next_to(l_paren, LEFT, buff=0.1) if background_rect: det.add_background_rectangle() det_text = VMobject(det, l_paren, r_paren) if determinant is not None: eq = TexMobject("=") eq.next_to(r_paren, RIGHT, buff=0.1) result = TexMobject(str(determinant)) result.next_to(eq, RIGHT, buff=0.2) det_text.add(eq, result) return det_text
def construct(self): self.setup() self.add_unit_square() pairs = [ (2 * RIGHT + UP, 1), (3 * LEFT, 2), (2 * LEFT + DOWN, 0.5), (3.5 * RIGHT + 2.5 * UP, 1.5), (RIGHT + 2 * DOWN, 0.25), (3 * LEFT + 3 * DOWN, 1), ] squares = VMobject() for position, side_length in pairs: square = self.square.copy() square.scale(side_length) square.shift(position) squares.add(square) self.play(FadeIn(squares, submobject_mode="lagged_start", run_time=3)) self.add_transformable_mobject(squares) self.apply_transposed_matrix([[1, -1], [0.5, 1]]) self.wait()
def construct(self): self.setup() self.add_unit_square() pairs = [ (2*RIGHT+UP, 1), (3*LEFT, 2), (2*LEFT+DOWN, 0.5), (3.5*RIGHT+2.5*UP, 1.5), (RIGHT+2*DOWN, 0.25), (3*LEFT+3*DOWN, 1), ] squares = VMobject() for position, side_length in pairs: square = self.square.copy() square.scale(side_length) square.shift(position) squares.add(square) self.play(FadeIn( squares, submobject_mode = "lagged_start", run_time = 3 )) self.add_transformable_mobject(squares) self.apply_transposed_matrix([[1, -1], [0.5, 1]]) self.dither()
def add_polygons(self): a = self.i_hat.get_end()[0] * RIGHT b = self.j_hat.get_end()[0] * RIGHT c = self.i_hat.get_end()[1] * UP d = self.j_hat.get_end()[1] * UP shapes_colors_and_tex = [ (Polygon(ORIGIN, a, a + c), MAROON, "ac/2"), (Polygon(ORIGIN, d + b, d, d), TEAL, "\\dfrac{bd}{2}"), (Polygon(a + c, a + b + c, a + b + c, a + b + c + d), TEAL, "\\dfrac{bd}{2}"), (Polygon(b + d, a + b + c + d, b + c + d), MAROON, "ac/2"), (Polygon(a, a + b, a + b + c, a + c), PINK, "bc"), (Polygon(d, d + b, d + b + c, d + c), PINK, "bc"), ] everyone = VMobject() for shape, color, tex in shapes_colors_and_tex: shape.set_stroke(width=0) shape.set_fill(color=color, opacity=0.7) tex_mob = TexMobject(tex) tex_mob.scale(0.7) tex_mob.move_to(shape.get_center_of_mass()) everyone.add(shape, tex_mob) self.play(FadeIn(everyone, submobject_mode="lagged_start", run_time=1))
class NumberPlane(VMobject): CONFIG = { "color" : BLUE_D, "secondary_color" : BLUE_E, "axes_color" : WHITE, "secondary_stroke_width" : 1, "x_radius": None, "y_radius": None, "x_unit_size" : 1, "y_unit_size" : 1, "center_point" : ORIGIN, "x_line_frequency" : 1, "y_line_frequency" : 1, "secondary_line_ratio" : 1, "written_coordinate_height" : 0.2, "propagate_style_to_family" : False, } def generate_points(self): if self.x_radius is None: center_to_edge = (SPACE_WIDTH + abs(self.center_point[0])) self.x_radius = center_to_edge / self.x_unit_size if self.y_radius is None: center_to_edge = (SPACE_HEIGHT + abs(self.center_point[1])) self.y_radius = center_to_edge / self.y_unit_size self.axes = VMobject() self.main_lines = VMobject() self.secondary_lines = VMobject() tuples = [ ( self.x_radius, self.x_line_frequency, self.y_radius*DOWN, self.y_radius*UP, RIGHT ), ( self.y_radius, self.y_line_frequency, self.x_radius*LEFT, self.x_radius*RIGHT, UP, ), ] for radius, freq, start, end, unit in tuples: main_range = np.arange(0, radius, freq) step = freq/float(freq + self.secondary_line_ratio) for v in np.arange(0, radius, step): line1 = Line(start+v*unit, end+v*unit) line2 = Line(start-v*unit, end-v*unit) if v == 0: self.axes.add(line1) elif v in main_range: self.main_lines.add(line1, line2) else: self.secondary_lines.add(line1, line2) self.add(self.secondary_lines, self.main_lines, self.axes) self.stretch(self.x_unit_size, 0) self.stretch(self.y_unit_size, 1) self.shift(self.center_point) #Put x_axis before y_axis y_axis, x_axis = self.axes.split() self.axes = VMobject(x_axis, y_axis) def init_colors(self): VMobject.init_colors(self) self.axes.set_stroke(self.axes_color, self.stroke_width) self.main_lines.set_stroke(self.color, self.stroke_width) self.secondary_lines.set_stroke( self.secondary_color, self.secondary_stroke_width ) return self def get_center_point(self): return self.coords_to_point(0, 0) def coords_to_point(self, x, y): x, y = np.array([x, y]) result = self.axes.get_center() result += x*self.get_x_unit_size()*RIGHT result += y*self.get_y_unit_size()*UP return result def point_to_coords(self, point): new_point = point - self.axes.get_center() x = new_point[0]/self.get_x_unit_size() y = new_point[1]/self.get_y_unit_size() return x, y def get_x_unit_size(self): return self.axes.get_width() / (2.0*self.x_radius) def get_y_unit_size(self): return self.axes.get_height() / (2.0*self.y_radius) def get_coordinate_labels(self, x_vals = None, y_vals = None): coordinate_labels = VGroup() if x_vals == None: x_vals = range(-int(self.x_radius), int(self.x_radius)+1) if y_vals == None: y_vals = range(-int(self.y_radius), int(self.y_radius)+1) for index, vals in enumerate([x_vals, y_vals]): num_pair = [0, 0] for val in vals: if val == 0: continue num_pair[index] = val point = self.coords_to_point(*num_pair) num = TexMobject(str(val)) num.add_background_rectangle() num.scale_to_fit_height( self.written_coordinate_height ) num.next_to(point, DOWN+LEFT, buff = SMALL_BUFF) coordinate_labels.add(num) self.coordinate_labels = coordinate_labels return coordinate_labels def get_axes(self): return self.axes def get_axis_labels(self, x_label = "x", y_label = "y"): x_axis, y_axis = self.get_axes().split() quads = [ (x_axis, x_label, UP, RIGHT), (y_axis, y_label, RIGHT, UP), ] labels = VGroup() for axis, tex, vect, edge in quads: label = TexMobject(tex) label.add_background_rectangle() label.next_to(axis, vect) label.to_edge(edge) labels.add(label) self.axis_labels = labels return labels def add_coordinates(self, x_vals = None, y_vals = None): self.add(*self.get_coordinate_labels(x_vals, y_vals)) return self def get_vector(self, coords, **kwargs): point = coords[0]*RIGHT + coords[1]*UP arrow = Arrow(ORIGIN, coords, **kwargs) return arrow def prepare_for_nonlinear_transform(self, num_inserted_anchor_points = 50): for mob in self.family_members_with_points(): num_anchors = mob.get_num_anchor_points() if num_inserted_anchor_points > num_anchors: mob.insert_n_anchor_points(num_inserted_anchor_points-num_anchors) mob.make_smooth() return self def apply_function(self, function, maintain_smoothness = True): VMobject.apply_function(self, function, maintain_smoothness = maintain_smoothness)
class NumberPlane(VMobject): CONFIG = { "color" : BLUE_D, "secondary_color" : BLUE_E, "axes_color" : WHITE, "secondary_stroke_width" : 1, "x_radius": SPACE_WIDTH, "y_radius": SPACE_HEIGHT, "space_unit_to_x_unit" : 1, "space_unit_to_y_unit" : 1, "x_line_frequency" : 1, "y_line_frequency" : 1, "secondary_line_ratio" : 1, "written_coordinate_height" : 0.2, "written_coordinate_nudge" : 0.1*(DOWN+RIGHT), "num_pair_at_center" : (0, 0), "propogate_style_to_family" : False, "submobject_partial_creation_mode" : "smoothed_lagged_start", } def generate_points(self): self.axes = VMobject() self.main_lines = VMobject() self.secondary_lines = VMobject() tuples = [ ( self.x_radius, self.x_line_frequency, self.y_radius*DOWN, self.y_radius*UP, RIGHT ), ( self.y_radius, self.y_line_frequency, self.x_radius*LEFT, self.x_radius*RIGHT, UP, ), ] for radius, freq, start, end, unit in tuples: main_range = np.arange(0, radius, freq) step = freq/float(freq + self.secondary_line_ratio) for v in np.arange(0, radius, step): line1 = Line(start+v*unit, end+v*unit) line2 = Line(start-v*unit, end-v*unit) if v == 0: self.axes.add(line1) elif v in main_range: self.main_lines.add(line1, line2) else: self.secondary_lines.add(line1, line2) self.add(self.axes, self.main_lines, self.secondary_lines) self.stretch(self.space_unit_to_x_unit, 0) self.stretch(self.space_unit_to_y_unit, 1) #Put x_axis before y_axis y_axis, x_axis = self.axes.split() self.axes = VMobject(x_axis, y_axis) def init_colors(self): VMobject.init_colors(self) self.axes.set_stroke(self.axes_color, self.stroke_width) self.main_lines.set_stroke(self.color, self.stroke_width) self.secondary_lines.set_stroke( self.secondary_color, self.secondary_stroke_width ) return self def get_center_point(self): return self.num_pair_to_point(self.num_pair_at_center) def num_pair_to_point(self, pair): pair = np.array(pair) + self.num_pair_at_center result = self.get_center() result[0] += pair[0]*self.space_unit_to_x_unit result[1] += pair[1]*self.space_unit_to_y_unit return result def point_to_num_pair(self, point): new_point = point-self.get_center() center_x, center_y = self.num_pair_at_center x = center_x + point[0]/self.space_unit_to_x_unit y = center_y + point[1]/self.space_unit_to_y_unit return x, y def get_coordinate_labels(self, x_vals = None, y_vals = None): result = [] if x_vals == None and y_vals == None: x_vals = range(-int(self.x_radius), int(self.x_radius)) y_vals = range(-int(self.y_radius), int(self.y_radius)) for index, vals in enumerate([x_vals, y_vals]): num_pair = [0, 0] for val in vals: num_pair[index] = val point = self.num_pair_to_point(num_pair) num = TexMobject(str(val)) num.scale_to_fit_height( self.written_coordinate_height ) num.shift( point-num.get_corner(UP+LEFT), self.written_coordinate_nudge ) result.append(num) return result def get_axes(self): return self.axes def get_axis_labels(self, x_label = "x", y_label = "y"): x_axis, y_axis = self.get_axes().split() x_label_mob = TexMobject(x_label) y_label_mob = TexMobject(y_label) x_label_mob.next_to(x_axis, DOWN) x_label_mob.to_edge(RIGHT) y_label_mob.next_to(y_axis, RIGHT) y_label_mob.to_edge(UP) return VMobject(x_label_mob, y_label_mob) def add_coordinates(self, x_vals = None, y_vals = None): self.add(*self.get_coordinate_labels(x_vals, y_vals)) return self def get_vector(self, coords, **kwargs): point = coords[0]*RIGHT + coords[1]*UP arrow = Arrow(ORIGIN, coords, **kwargs) return arrow def prepare_for_nonlinear_transform(self, num_inserted_anchor_points = 50): for mob in self.family_members_with_points(): mob.insert_n_anchor_points(num_inserted_anchor_points) mob.make_smooth() return self
class NumberLine(VMobject): CONFIG = { "color" : BLUE, "x_min" : -SPACE_WIDTH, "x_max" : SPACE_WIDTH, "space_unit_to_num" : 1, "tick_size" : 0.1, "tick_frequency" : 0.5, "leftmost_tick" : None, #Defaults to ceil(x_min) "numbers_with_elongated_ticks" : [0], "longer_tick_multiple" : 2, "number_at_center" : 0, "propogate_style_to_family" : True } def __init__(self, **kwargs): digest_config(self, kwargs) if self.leftmost_tick is None: self.leftmost_tick = np.ceil(self.x_min) VMobject.__init__(self, **kwargs) def generate_points(self): self.main_line = Line(self.x_min*RIGHT, self.x_max*RIGHT) self.tick_marks = VMobject() self.add(self.main_line, self.tick_marks) for x in self.get_tick_numbers(): self.add_tick(x, self.tick_size) for x in self.numbers_with_elongated_ticks: self.add_tick(x, self.longer_tick_multiple*self.tick_size) self.stretch(self.space_unit_to_num, 0) self.shift(-self.number_to_point(self.number_at_center)) def add_tick(self, x, size): self.tick_marks.add(Line( x*RIGHT+size*DOWN, x*RIGHT+size*UP, )) return self def get_tick_marks(self): return self.tick_marks def get_tick_numbers(self): return np.arange(self.leftmost_tick, self.x_max, self.tick_frequency) def number_to_point(self, number): return interpolate( self.main_line.get_left(), self.main_line.get_right(), float(number-self.x_min)/(self.x_max - self.x_min) ) def point_to_number(self, point): dist_from_left = (point[0]-self.main_line.get_left()[0]) num_dist_from_left = num_dist_from_left/self.space_unit_to_num return self.x_min + dist_from_left def default_numbers_to_display(self): return self.get_tick_numbers()[::2] def get_vertical_number_offset(self, direction = DOWN): return 4*direction*self.tick_size def get_number_mobjects(self, *numbers, **kwargs): #TODO, handle decimals if len(numbers) == 0: numbers = self.default_numbers_to_display() result = [] for number in numbers: mob = TexMobject(str(int(number))) mob.scale_to_fit_height(2*self.tick_size) mob.shift( self.number_to_point(number), self.get_vertical_number_offset(**kwargs) ) result.append(mob) return result def add_numbers(self, *numbers, **kwargs): self.numbers = self.get_number_mobjects( *numbers, **kwargs ) self.add(*self.numbers) return self
class NumberLine(VMobject): CONFIG = { "color": BLUE, "x_min": -SPACE_WIDTH, "x_max": SPACE_WIDTH, "space_unit_to_num": 1, "tick_size": 0.1, "tick_frequency": 1, "leftmost_tick": None, #Defaults to ceil(x_min) "numbers_with_elongated_ticks": [0], "longer_tick_multiple": 2, "number_at_center": 0, "propogate_style_to_family": True } def __init__(self, **kwargs): digest_config(self, kwargs) if self.leftmost_tick is None: self.leftmost_tick = np.ceil(self.x_min) VMobject.__init__(self, **kwargs) def generate_points(self): self.main_line = Line(self.x_min * RIGHT, self.x_max * RIGHT) self.tick_marks = VMobject() self.add(self.main_line, self.tick_marks) for x in self.get_tick_numbers(): self.add_tick(x, self.tick_size) for x in self.numbers_with_elongated_ticks: self.add_tick(x, self.longer_tick_multiple * self.tick_size) self.stretch(self.space_unit_to_num, 0) self.shift(-self.number_to_point(self.number_at_center)) def add_tick(self, x, size): self.tick_marks.add( Line( x * RIGHT + size * DOWN, x * RIGHT + size * UP, )) return self def get_tick_marks(self): return self.tick_marks def get_tick_numbers(self): return np.arange(self.leftmost_tick, self.x_max, self.tick_frequency) def number_to_point(self, number): return interpolate( self.main_line.get_left(), self.main_line.get_right(), float(number - self.x_min) / (self.x_max - self.x_min)) def point_to_number(self, point): dist_from_left = (point[0] - self.main_line.get_left()[0]) num_dist_from_left = num_dist_from_left / self.space_unit_to_num return self.x_min + dist_from_left def default_numbers_to_display(self): return np.arange(self.leftmost_tick, self.x_max, 1) def get_vertical_number_offset(self, direction=DOWN): return 4 * direction * self.tick_size def get_number_mobjects(self, *numbers, **kwargs): #TODO, handle decimals if len(numbers) == 0: numbers = self.default_numbers_to_display() result = [] for number in numbers: mob = TexMobject(str(int(number))) mob.scale_to_fit_height(3 * self.tick_size) mob.shift(self.number_to_point(number), self.get_vertical_number_offset(**kwargs)) result.append(mob) return result def get_numbers(self, *numbers, **kwargs): ##TODO, this shouldn't exist alongside the above method. return VMobject(*self.get_number_mobjects(*numbers, **kwargs)) def add_numbers(self, *numbers, **kwargs): self.numbers = self.get_number_mobjects(*numbers, **kwargs) self.add(*self.numbers) return self
class NumberPlane(VMobject): CONFIG = { "color" : BLUE_D, "secondary_color" : BLUE_E, "axes_color" : WHITE, "secondary_stroke_width" : 1, "x_radius": None, "y_radius": None, "x_unit_size" : 1, "y_unit_size" : 1, "center_point" : ORIGIN, "x_line_frequency" : 1, "y_line_frequency" : 1, "secondary_line_ratio" : 1, "written_coordinate_height" : 0.2, "propogate_style_to_family" : False, } def generate_points(self): if self.x_radius is None: center_to_edge = (SPACE_WIDTH + abs(self.center_point[0])) self.x_radius = center_to_edge / self.x_unit_size if self.y_radius is None: center_to_edge = (SPACE_HEIGHT + abs(self.center_point[1])) self.y_radius = center_to_edge / self.y_unit_size self.axes = VMobject() self.main_lines = VMobject() self.secondary_lines = VMobject() tuples = [ ( self.x_radius, self.x_line_frequency, self.y_radius*DOWN, self.y_radius*UP, RIGHT ), ( self.y_radius, self.y_line_frequency, self.x_radius*LEFT, self.x_radius*RIGHT, UP, ), ] for radius, freq, start, end, unit in tuples: main_range = np.arange(0, radius, freq) step = freq/float(freq + self.secondary_line_ratio) for v in np.arange(0, radius, step): line1 = Line(start+v*unit, end+v*unit) line2 = Line(start-v*unit, end-v*unit) if v == 0: self.axes.add(line1) elif v in main_range: self.main_lines.add(line1, line2) else: self.secondary_lines.add(line1, line2) self.add(self.secondary_lines, self.main_lines, self.axes) self.stretch(self.x_unit_size, 0) self.stretch(self.y_unit_size, 1) self.shift(self.center_point) #Put x_axis before y_axis y_axis, x_axis = self.axes.split() self.axes = VMobject(x_axis, y_axis) def init_colors(self): VMobject.init_colors(self) self.axes.set_stroke(self.axes_color, self.stroke_width) self.main_lines.set_stroke(self.color, self.stroke_width) self.secondary_lines.set_stroke( self.secondary_color, self.secondary_stroke_width ) return self def get_center_point(self): return self.coords_to_point(0, 0) def coords_to_point(self, x, y): x, y = np.array([x, y]) result = self.axes.get_center() result += x*self.get_x_unit_size()*RIGHT result += y*self.get_y_unit_size()*UP return result def point_to_coords(self, point): new_point = point - self.axes.get_center() x = new_point[0]/self.get_x_unit_size() y = new_point[1]/self.get_y_unit_size() return x, y def get_x_unit_size(self): return self.axes.get_width() / (2.0*self.x_radius) def get_y_unit_size(self): return self.axes.get_height() / (2.0*self.y_radius) def get_coordinate_labels(self, x_vals = None, y_vals = None): result = [] if x_vals == None and y_vals == None: x_vals = range(-int(self.x_radius), int(self.x_radius)) y_vals = range(-int(self.y_radius), int(self.y_radius)) for index, vals in enumerate([x_vals, y_vals]): num_pair = [0, 0] for val in vals: if val == 0: continue num_pair[index] = val point = self.coords_to_point(*num_pair) num = TexMobject(str(val)) num.add_background_rectangle() num.scale_to_fit_height( self.written_coordinate_height ) vect = DOWN if index == 0 else LEFT num.next_to(point, vect, buff = SMALL_BUFF) result.append(num) return result def get_axes(self): return self.axes def get_axis_labels(self, x_label = "x", y_label = "y"): x_axis, y_axis = self.get_axes().split() quads = [ (x_axis, x_label, UP, RIGHT), (y_axis, y_label, RIGHT, UP), ] labels = VGroup() for axis, tex, vect, edge in quads: label = TexMobject(tex) label.add_background_rectangle() label.next_to(axis, vect) label.to_edge(edge) labels.add(label) self.axis_labels = labels return labels def add_coordinates(self, x_vals = None, y_vals = None): self.add(*self.get_coordinate_labels(x_vals, y_vals)) return self def get_vector(self, coords, **kwargs): point = coords[0]*RIGHT + coords[1]*UP arrow = Arrow(ORIGIN, coords, **kwargs) return arrow def prepare_for_nonlinear_transform(self, num_inserted_anchor_points = 50): for mob in self.family_members_with_points(): num_anchors = mob.get_num_anchor_points() if num_inserted_anchor_points > num_anchors: mob.insert_n_anchor_points(num_inserted_anchor_points-num_anchors) mob.make_smooth() return self
class NumberLine(VMobject): CONFIG = { "color" : BLUE, "x_min" : -SPACE_WIDTH, "x_max" : SPACE_WIDTH, "unit_size" : 1, "tick_size" : 0.1, "tick_frequency" : 1, "leftmost_tick" : None, #Defaults to ceil(x_min) "numbers_with_elongated_ticks" : [0], "numbers_to_show" : None, "longer_tick_multiple" : 2, "number_at_center" : 0, "propogate_style_to_family" : True } def __init__(self, **kwargs): digest_config(self, kwargs) if self.leftmost_tick is None: self.leftmost_tick = np.ceil(self.x_min) VMobject.__init__(self, **kwargs) def generate_points(self): self.main_line = Line(self.x_min*RIGHT, self.x_max*RIGHT) self.tick_marks = VMobject() self.add(self.main_line, self.tick_marks) for x in self.get_tick_numbers(): self.add_tick(x, self.tick_size) for x in self.numbers_with_elongated_ticks: self.add_tick(x, self.longer_tick_multiple*self.tick_size) self.stretch(self.unit_size, 0) self.shift(-self.number_to_point(self.number_at_center)) def add_tick(self, x, size): self.tick_marks.add(Line( x*RIGHT+size*DOWN, x*RIGHT+size*UP, )) return self def get_tick_marks(self): return self.tick_marks def get_tick_numbers(self): epsilon = 0.001 return np.arange( self.leftmost_tick, self.x_max+epsilon, self.tick_frequency ) def number_to_point(self, number): alpha = float(number-self.x_min)/(self.x_max - self.x_min) return interpolate( self.main_line.get_start(), self.main_line.get_end(), alpha ) def point_to_number(self, point): left_point, right_point = self.main_line.get_start_and_end() full_vect = right_point-left_point def distance_from_left(p): return np.dot(p-left_point, full_vect)/np.linalg.norm(full_vect) return interpolate( self.x_min, self.x_max, distance_from_left(point)/distance_from_left(right_point) ) def default_numbers_to_display(self): if self.numbers_to_show is not None: return self.numbers_to_show return np.arange(self.leftmost_tick, self.x_max, 1) def get_vertical_number_offset(self, direction = DOWN): return 4*direction*self.tick_size def get_number_mobjects(self, *numbers, **kwargs): #TODO, handle decimals if len(numbers) == 0: numbers = self.default_numbers_to_display() result = VGroup() for number in numbers: mob = TexMobject(str(int(number))) mob.scale_to_fit_height(3*self.tick_size) mob.shift( self.number_to_point(number), self.get_vertical_number_offset(**kwargs) ) result.add(mob) return result def add_numbers(self, *numbers, **kwargs): self.numbers = self.get_number_mobjects( *numbers, **kwargs ) self.add(*self.numbers) return self
class NumberPlane(VMobject): CONFIG = { "color": BLUE_D, "secondary_color": BLUE_E, "axes_color": WHITE, "secondary_stroke_width": 1, "x_radius": SPACE_WIDTH, "y_radius": SPACE_HEIGHT, "space_unit_to_x_unit": 1, "space_unit_to_y_unit": 1, "x_line_frequency": 1, "y_line_frequency": 1, "secondary_line_ratio": 1, "written_coordinate_height": 0.2, "written_coordinate_nudge": 0.1 * (DOWN + RIGHT), "num_pair_at_center": (0, 0), "propogate_style_to_family": False, } def generate_points(self): self.axes = VMobject() self.main_lines = VMobject() self.secondary_lines = VMobject() tuples = [ (self.x_radius, self.x_line_frequency, self.y_radius * DOWN, self.y_radius * UP, RIGHT), ( self.y_radius, self.y_line_frequency, self.x_radius * LEFT, self.x_radius * RIGHT, UP, ), ] for radius, freq, start, end, unit in tuples: main_range = np.arange(0, radius, freq) step = freq / float(freq + self.secondary_line_ratio) for v in np.arange(0, radius, step): line1 = Line(start + v * unit, end + v * unit) line2 = Line(start - v * unit, end - v * unit) if v == 0: self.axes.add(line1) elif v in main_range: self.main_lines.add(line1, line2) else: self.secondary_lines.add(line1, line2) self.add(self.secondary_lines, self.main_lines, self.axes) self.stretch(self.space_unit_to_x_unit, 0) self.stretch(self.space_unit_to_y_unit, 1) #Put x_axis before y_axis y_axis, x_axis = self.axes.split() self.axes = VMobject(x_axis, y_axis) def init_colors(self): VMobject.init_colors(self) self.axes.set_stroke(self.axes_color, self.stroke_width) self.main_lines.set_stroke(self.color, self.stroke_width) self.secondary_lines.set_stroke(self.secondary_color, self.secondary_stroke_width) return self def get_center_point(self): return self.num_pair_to_point(self.num_pair_at_center) def num_pair_to_point(self, pair): pair = np.array(pair) + self.num_pair_at_center result = self.axes.get_center() result[0] += pair[0] * self.space_unit_to_x_unit result[1] += pair[1] * self.space_unit_to_y_unit return result def point_to_num_pair(self, point): new_point = point - self.get_center() center_x, center_y = self.num_pair_at_center x = center_x + point[0] / self.space_unit_to_x_unit y = center_y + point[1] / self.space_unit_to_y_unit return x, y def get_coordinate_labels(self, x_vals=None, y_vals=None): result = [] if x_vals == None and y_vals == None: x_vals = range(-int(self.x_radius), int(self.x_radius)) y_vals = range(-int(self.y_radius), int(self.y_radius)) for index, vals in enumerate([x_vals, y_vals]): num_pair = [0, 0] for val in vals: num_pair[index] = val point = self.num_pair_to_point(num_pair) num = TexMobject(str(val)) num.scale_to_fit_height(self.written_coordinate_height) num.shift(point - num.get_corner(UP + LEFT), self.written_coordinate_nudge) result.append(num) return result def get_axes(self): return self.axes def get_axis_labels(self, x_label="x", y_label="y"): x_axis, y_axis = self.get_axes().split() x_label_mob = TexMobject(x_label) y_label_mob = TexMobject(y_label) x_label_mob.next_to(x_axis, DOWN) x_label_mob.to_edge(RIGHT) y_label_mob.next_to(y_axis, RIGHT) y_label_mob.to_edge(UP) return VMobject(x_label_mob, y_label_mob) def add_coordinates(self, x_vals=None, y_vals=None): self.add(*self.get_coordinate_labels(x_vals, y_vals)) return self def get_vector(self, coords, **kwargs): point = coords[0] * RIGHT + coords[1] * UP arrow = Arrow(ORIGIN, coords, **kwargs) return arrow def prepare_for_nonlinear_transform(self, num_inserted_anchor_points=50): for mob in self.family_members_with_points(): num_anchors = mob.get_num_anchor_points() if num_inserted_anchor_points > num_anchors: mob.insert_n_anchor_points(num_inserted_anchor_points - num_anchors) mob.make_smooth() return self
class NumberLine(VMobject): CONFIG = { "color": BLUE, "x_min": -SPACE_WIDTH, "x_max": SPACE_WIDTH, "space_unit_to_num": 1, "tick_size": 0.1, "tick_frequency": 1, "leftmost_tick": None, #Defaults to ceil(x_min) "numbers_with_elongated_ticks": [0], "numbers_to_show": None, "longer_tick_multiple": 2, "number_at_center": 0, "propogate_style_to_family": True } def __init__(self, **kwargs): digest_config(self, kwargs) if self.leftmost_tick is None: self.leftmost_tick = np.ceil(self.x_min) VMobject.__init__(self, **kwargs) def generate_points(self): self.main_line = Line(self.x_min * RIGHT, self.x_max * RIGHT) self.tick_marks = VMobject() self.add(self.main_line, self.tick_marks) for x in self.get_tick_numbers(): self.add_tick(x, self.tick_size) for x in self.numbers_with_elongated_ticks: self.add_tick(x, self.longer_tick_multiple * self.tick_size) self.stretch(self.space_unit_to_num, 0) self.shift(-self.number_to_point(self.number_at_center)) def add_tick(self, x, size): self.tick_marks.add( Line( x * RIGHT + size * DOWN, x * RIGHT + size * UP, )) return self def get_tick_marks(self): return self.tick_marks def get_tick_numbers(self): return np.arange(self.leftmost_tick, self.x_max + self.tick_frequency, self.tick_frequency) def number_to_point(self, number): alpha = float(number - self.x_min) / (self.x_max - self.x_min) return interpolate(self.main_line.get_start(), self.main_line.get_end(), alpha) def point_to_number(self, point): left_point, right_point = self.main_line.get_start_and_end() full_vect = right_point - left_point def distance_from_left(p): return np.dot(p - left_point, full_vect) / np.linalg.norm(full_vect) return interpolate( self.x_min, self.x_max, distance_from_left(point) / distance_from_left(right_point)) def default_numbers_to_display(self): if self.numbers_to_show is not None: return self.numbers_to_show return np.arange(self.leftmost_tick, self.x_max, 1) def get_vertical_number_offset(self, direction=DOWN): return 4 * direction * self.tick_size def get_number_mobjects(self, *numbers, **kwargs): #TODO, handle decimals if len(numbers) == 0: numbers = self.default_numbers_to_display() result = VGroup() for number in numbers: mob = TexMobject(str(int(number))) mob.scale_to_fit_height(3 * self.tick_size) mob.shift(self.number_to_point(number), self.get_vertical_number_offset(**kwargs)) result.add(mob) return result def add_numbers(self, *numbers, **kwargs): self.numbers = self.get_number_mobjects(*numbers, **kwargs) self.add(*self.numbers) return self