2015-06-10 22:00:35 -07:00
|
|
|
from helpers import *
|
|
|
|
|
2015-12-19 13:06:09 -08:00
|
|
|
from mobject import Mobject1D
|
2016-04-19 00:20:19 -07:00
|
|
|
from mobject.vectorized_mobject import VMobject
|
2015-12-19 13:06:09 -08:00
|
|
|
from mobject.tex_mobject import TexMobject
|
2016-04-19 00:20:19 -07:00
|
|
|
from topics.geometry import Line, Arrow
|
2015-10-27 21:00:50 -07:00
|
|
|
from scene import Scene
|
2015-06-10 22:00:35 -07:00
|
|
|
|
2016-04-19 00:20:19 -07:00
|
|
|
class NumberLine(VMobject):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2016-04-19 00:20:19 -07:00
|
|
|
"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)
|
2015-09-28 16:25:18 -07:00
|
|
|
"numbers_with_elongated_ticks" : [0],
|
2016-04-19 00:20:19 -07:00
|
|
|
"longer_tick_multiple" : 2,
|
|
|
|
"number_at_center" : 0,
|
2016-04-23 23:36:05 -07:00
|
|
|
"propogate_style_to_family" : True
|
2015-09-28 16:25:18 -07:00
|
|
|
}
|
|
|
|
def __init__(self, **kwargs):
|
2015-10-28 16:03:33 -07:00
|
|
|
digest_config(self, kwargs)
|
2015-10-08 11:14:55 -07:00
|
|
|
if self.leftmost_tick is None:
|
2016-04-19 00:20:19 -07:00
|
|
|
self.leftmost_tick = np.ceil(self.x_min)
|
|
|
|
VMobject.__init__(self, **kwargs)
|
2015-06-10 22:00:35 -07:00
|
|
|
|
|
|
|
def generate_points(self):
|
2016-04-19 00:20:19 -07:00
|
|
|
self.main_line = Line(self.x_min*RIGHT, self.x_max*RIGHT)
|
|
|
|
self.add(self.main_line)
|
|
|
|
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.add(Line(
|
|
|
|
x*RIGHT+size*DOWN,
|
|
|
|
x*RIGHT+size*UP,
|
|
|
|
))
|
|
|
|
return self
|
2015-09-30 14:22:17 -07:00
|
|
|
|
|
|
|
def get_tick_numbers(self):
|
2016-04-19 00:20:19 -07:00
|
|
|
return np.arange(self.leftmost_tick, self.x_max, self.tick_frequency)
|
2015-08-01 11:34:33 -07:00
|
|
|
|
2015-09-25 19:43:53 -07:00
|
|
|
def number_to_point(self, number):
|
|
|
|
return interpolate(
|
2016-04-19 00:20:19 -07:00
|
|
|
self.main_line.get_left(),
|
|
|
|
self.main_line.get_right(),
|
|
|
|
float(number-self.x_min)/(self.x_max - self.x_min)
|
2015-09-25 19:43:53 -07:00
|
|
|
)
|
|
|
|
|
2015-09-30 14:22:17 -07:00
|
|
|
def point_to_number(self, point):
|
2016-04-19 00:20:19 -07:00
|
|
|
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
|
2015-09-30 14:22:17 -07:00
|
|
|
|
|
|
|
def default_numbers_to_display(self):
|
|
|
|
return self.get_tick_numbers()[::2]
|
|
|
|
|
2016-03-08 23:13:41 -08:00
|
|
|
def get_vertical_number_offset(self, direction = DOWN):
|
|
|
|
return 4*direction*self.tick_size
|
2015-09-30 14:22:17 -07:00
|
|
|
|
2016-03-08 23:13:41 -08:00
|
|
|
def get_number_mobjects(self, *numbers, **kwargs):
|
2015-10-08 11:14:55 -07:00
|
|
|
#TODO, handle decimals
|
2015-09-25 19:43:53 -07:00
|
|
|
if len(numbers) == 0:
|
2015-09-30 14:22:17 -07:00
|
|
|
numbers = self.default_numbers_to_display()
|
|
|
|
result = []
|
2015-09-25 19:43:53 -07:00
|
|
|
for number in numbers:
|
2015-10-28 17:18:50 -07:00
|
|
|
mob = TexMobject(str(int(number)))
|
2016-04-19 00:20:19 -07:00
|
|
|
mob.scale_to_fit_height(2*self.tick_size)
|
|
|
|
mob.shift(
|
|
|
|
self.number_to_point(number),
|
|
|
|
self.get_vertical_number_offset(**kwargs)
|
|
|
|
)
|
2015-09-30 14:22:17 -07:00
|
|
|
result.append(mob)
|
|
|
|
return result
|
|
|
|
|
2016-03-08 23:13:41 -08:00
|
|
|
def add_numbers(self, *numbers, **kwargs):
|
|
|
|
self.numbers = self.get_number_mobjects(
|
|
|
|
*numbers, **kwargs
|
|
|
|
)
|
|
|
|
self.add(*self.numbers)
|
2015-10-08 11:14:55 -07:00
|
|
|
return self
|
2015-06-10 22:00:35 -07:00
|
|
|
|
2015-09-25 19:43:53 -07:00
|
|
|
class UnitInterval(NumberLine):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2016-04-19 00:20:19 -07:00
|
|
|
"x_min" : 0,
|
|
|
|
"x_max" : 1,
|
|
|
|
"space_unit_to_num" : 6,
|
|
|
|
"tick_frequency" : 0.1,
|
2015-09-25 19:43:53 -07:00
|
|
|
"numbers_with_elongated_ticks" : [0, 1],
|
2016-04-19 00:20:19 -07:00
|
|
|
"number_at_center" : 0.5,
|
2015-09-25 19:43:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-19 00:20:19 -07:00
|
|
|
class NumberPlane(VMobject):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2016-04-19 00:20:19 -07:00
|
|
|
"color" : BLUE_D,
|
|
|
|
"secondary_color" : BLUE_E,
|
|
|
|
"axes_color" : WHITE,
|
|
|
|
"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.5,
|
|
|
|
"written_coordinate_nudge" : 0.1*(DOWN+RIGHT),
|
|
|
|
"num_pair_at_center" : (0, 0),
|
2015-10-06 15:27:40 -07:00
|
|
|
}
|
2015-10-28 16:03:33 -07:00
|
|
|
|
2015-10-06 15:27:40 -07:00
|
|
|
def generate_points(self):
|
2016-04-19 00:20:19 -07:00
|
|
|
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,
|
|
|
|
),
|
2015-10-06 18:41:26 -07:00
|
|
|
]
|
2016-04-19 00:20:19 -07:00
|
|
|
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)
|
|
|
|
|
|
|
|
def init_colors(self):
|
|
|
|
VMobject.init_colors(self)
|
|
|
|
self.axes.set_stroke(self.axes_color)
|
|
|
|
# self.main_lines.set_stroke(self.color)
|
|
|
|
self.secondary_lines.set_stroke(self.secondary_color, 1)
|
|
|
|
return self
|
2015-10-20 21:55:46 -07:00
|
|
|
|
2015-10-06 15:27:40 -07:00
|
|
|
def get_center_point(self):
|
|
|
|
return self.num_pair_to_point(self.num_pair_at_center)
|
|
|
|
|
|
|
|
def num_pair_to_point(self, pair):
|
2016-04-19 00:20:19 -07:00
|
|
|
pair = np.array(pair) + self.num_pair_at_center
|
2015-11-02 19:09:55 -08:00
|
|
|
result = self.get_center()
|
2016-04-19 00:20:19 -07:00
|
|
|
result[0] += pair[0]*self.space_unit_to_x_unit
|
|
|
|
result[1] += pair[1]*self.space_unit_to_y_unit
|
2015-11-02 19:09:55 -08:00
|
|
|
return result
|
|
|
|
|
|
|
|
def point_to_num_pair(self, point):
|
|
|
|
new_point = point-self.get_center()
|
|
|
|
center_x, center_y = self.num_pair_at_center
|
2016-04-19 00:20:19 -07:00
|
|
|
x = center_x + point[0]/self.space_unit_to_x_unit
|
|
|
|
y = center_y + point[1]/self.space_unit_to_y_unit
|
2015-11-02 19:09:55 -08:00
|
|
|
return x, y
|
2015-10-06 15:27:40 -07:00
|
|
|
|
|
|
|
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))
|
2016-04-19 00:20:19 -07:00
|
|
|
for index, vals in enumerate([x_vals, y_vals]):
|
2015-10-06 15:27:40 -07:00
|
|
|
num_pair = [0, 0]
|
|
|
|
for val in vals:
|
|
|
|
num_pair[index] = val
|
|
|
|
point = self.num_pair_to_point(num_pair)
|
2015-10-28 17:18:50 -07:00
|
|
|
num = TexMobject(str(val))
|
2016-04-19 00:20:19 -07:00
|
|
|
num.scale_to_fit_height(
|
|
|
|
self.written_coordinate_height
|
|
|
|
)
|
|
|
|
num.shift(
|
|
|
|
point-num.get_corner(UP+LEFT),
|
|
|
|
self.written_coordinate_nudge
|
|
|
|
)
|
2015-10-06 15:27:40 -07:00
|
|
|
result.append(num)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def add_coordinates(self, x_vals = None, y_vals = None):
|
|
|
|
self.add(*self.get_coordinate_labels(x_vals, y_vals))
|
|
|
|
return self
|
2015-06-27 04:49:10 -07:00
|
|
|
|
2015-10-12 19:39:46 -07:00
|
|
|
def get_vector(self, coords, **kwargs):
|
2016-04-19 00:20:19 -07:00
|
|
|
point = coords[0]*RIGHT + coords[1]*UP
|
2015-10-12 19:39:46 -07:00
|
|
|
arrow = Arrow(ORIGIN, coords, **kwargs)
|
|
|
|
return arrow
|
|
|
|
|
2016-04-19 00:20:19 -07:00
|
|
|
def prepare_for_nonlinear_transform(self):
|
|
|
|
for mob in self.submobject_family():
|
|
|
|
if mob.get_num_points() > 0:
|
|
|
|
mob.insert_n_anchor_points(20)
|
|
|
|
mob.change_anchor_mode("smooth")
|
2015-10-27 21:00:50 -07:00
|
|
|
|
|
|
|
|
2015-10-12 19:39:46 -07:00
|
|
|
|
|
|
|
|
2015-10-20 21:55:46 -07:00
|
|
|
|
2015-06-27 04:49:10 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-06-10 22:00:35 -07:00
|
|
|
|
|
|
|
|