2015-06-10 22:00:35 -07:00
|
|
|
from helpers import *
|
|
|
|
|
2015-12-21 12:45:56 -08:00
|
|
|
from mobject import Mobject, Mobject1D, Point
|
2015-10-27 21:00:50 -07:00
|
|
|
|
2015-06-10 22:00:35 -07:00
|
|
|
class Dot(Mobject1D): #Use 1D density, even though 2D
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2015-09-28 16:25:18 -07:00
|
|
|
"radius" : 0.05
|
|
|
|
}
|
|
|
|
def __init__(self, center_point = ORIGIN, **kwargs):
|
2015-10-28 16:03:33 -07:00
|
|
|
digest_locals(self)
|
2015-09-28 16:25:18 -07:00
|
|
|
Mobject1D.__init__(self, **kwargs)
|
2015-06-10 22:00:35 -07:00
|
|
|
|
|
|
|
def generate_points(self):
|
|
|
|
self.add_points([
|
|
|
|
np.array((t*np.cos(theta), t*np.sin(theta), 0)) + self.center_point
|
|
|
|
for t in np.arange(self.epsilon, self.radius, self.epsilon)
|
|
|
|
for new_epsilon in [2*np.pi*self.epsilon*self.radius/t]
|
|
|
|
for theta in np.arange(0, 2 * np.pi, new_epsilon)
|
|
|
|
])
|
|
|
|
|
|
|
|
class Cross(Mobject1D):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2015-10-29 13:45:28 -07:00
|
|
|
"color" : YELLOW,
|
2015-09-28 16:25:18 -07:00
|
|
|
"radius" : 0.3
|
|
|
|
}
|
|
|
|
def __init__(self, center_point = ORIGIN, **kwargs):
|
2015-10-28 16:03:33 -07:00
|
|
|
digest_locals(self)
|
2015-09-28 16:25:18 -07:00
|
|
|
Mobject1D.__init__(self, **kwargs)
|
|
|
|
|
2015-06-10 22:00:35 -07:00
|
|
|
def generate_points(self):
|
|
|
|
self.add_points([
|
|
|
|
(sgn * x, x, 0)
|
2015-09-28 16:25:18 -07:00
|
|
|
for x in np.arange(-self.radius / 2, self.radius/2, self.epsilon)
|
2015-06-10 22:00:35 -07:00
|
|
|
for sgn in [-1, 1]
|
|
|
|
])
|
2015-09-28 16:25:18 -07:00
|
|
|
self.shift(self.center_point)
|
2015-06-10 22:00:35 -07:00
|
|
|
|
2015-10-27 21:00:50 -07:00
|
|
|
|
2015-06-10 22:00:35 -07:00
|
|
|
class Line(Mobject1D):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2015-12-31 09:24:02 -08:00
|
|
|
"buff" : 0
|
2015-12-19 13:06:09 -08:00
|
|
|
}
|
2015-09-28 16:25:18 -07:00
|
|
|
def __init__(self, start, end, **kwargs):
|
2015-12-19 13:06:09 -08:00
|
|
|
digest_config(self, kwargs)
|
2015-08-17 11:12:56 -07:00
|
|
|
self.set_start_and_end(start, end)
|
2015-09-28 16:25:18 -07:00
|
|
|
Mobject1D.__init__(self, **kwargs)
|
2015-06-10 22:00:35 -07:00
|
|
|
|
2015-08-17 11:12:56 -07:00
|
|
|
def set_start_and_end(self, start, end):
|
|
|
|
preliminary_start, preliminary_end = [
|
|
|
|
arg.get_center()
|
|
|
|
if isinstance(arg, Mobject)
|
2015-12-21 22:52:01 -08:00
|
|
|
else np.array(arg).astype('float')
|
2015-08-17 11:12:56 -07:00
|
|
|
for arg in start, end
|
|
|
|
]
|
|
|
|
start_to_end = preliminary_end - preliminary_start
|
2015-11-02 19:09:55 -08:00
|
|
|
vect = np.zeros(len(start_to_end))
|
2015-08-17 11:12:56 -07:00
|
|
|
longer_dim = np.argmax(map(abs, start_to_end))
|
2015-11-02 19:09:55 -08:00
|
|
|
vect[longer_dim] = start_to_end[longer_dim]
|
2015-08-17 11:12:56 -07:00
|
|
|
self.start, self.end = [
|
2015-11-02 19:09:55 -08:00
|
|
|
arg.get_edge_center(unit*vect)
|
2015-08-17 11:12:56 -07:00
|
|
|
if isinstance(arg, Mobject)
|
|
|
|
else np.array(arg)
|
|
|
|
for arg, unit in zip([start, end], [1, -1])
|
|
|
|
]
|
2015-12-19 13:06:09 -08:00
|
|
|
start_to_end = self.end - self.start
|
2016-01-07 16:25:01 -08:00
|
|
|
length = np.linalg.norm(start_to_end)
|
|
|
|
if length > 2*self.buff:
|
|
|
|
start_to_end /= np.linalg.norm(start_to_end)
|
|
|
|
self.start = self.start + self.buff*start_to_end
|
|
|
|
self.end = self.end - self.buff*start_to_end
|
2015-08-17 11:12:56 -07:00
|
|
|
|
2015-06-10 22:00:35 -07:00
|
|
|
def generate_points(self):
|
2015-11-25 15:28:07 -08:00
|
|
|
self.add_line(self.start, self.end)
|
2015-06-10 22:00:35 -07:00
|
|
|
|
2015-06-13 19:00:23 -07:00
|
|
|
def get_length(self):
|
|
|
|
return np.linalg.norm(self.start - self.end)
|
|
|
|
|
|
|
|
def get_slope(self):
|
|
|
|
rise, run = [
|
|
|
|
float(self.end[i] - self.start[i])
|
|
|
|
for i in [1, 0]
|
|
|
|
]
|
|
|
|
return rise/run
|
|
|
|
|
2015-08-17 11:12:56 -07:00
|
|
|
class Arrow(Line):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2015-12-22 11:04:36 -08:00
|
|
|
"color" : YELLOW_C,
|
2015-12-19 13:06:09 -08:00
|
|
|
"tip_length" : 0.25,
|
2015-12-31 09:24:02 -08:00
|
|
|
"buff" : 0.3,
|
2015-09-28 16:25:18 -07:00
|
|
|
}
|
2015-08-12 14:24:36 -07:00
|
|
|
def __init__(self, *args, **kwargs):
|
2015-12-31 09:24:02 -08:00
|
|
|
if len(args) == 1:
|
|
|
|
target = args[0]
|
|
|
|
if isinstance(target, Mobject):
|
|
|
|
point = target.get_center()
|
|
|
|
else:
|
|
|
|
point = target
|
|
|
|
args = (point+UP+LEFT, target)
|
2015-08-12 14:24:36 -07:00
|
|
|
Line.__init__(self, *args, **kwargs)
|
2015-09-28 16:25:18 -07:00
|
|
|
self.add_tip()
|
2015-08-12 14:24:36 -07:00
|
|
|
|
2015-09-28 16:25:18 -07:00
|
|
|
def add_tip(self):
|
2015-10-06 18:41:53 -07:00
|
|
|
num_points = self.get_num_points()
|
2015-08-17 11:12:56 -07:00
|
|
|
vect = self.start-self.end
|
2016-01-07 16:25:01 -08:00
|
|
|
length = np.linalg.norm(vect)
|
|
|
|
vect = vect*self.tip_length/length
|
2015-08-17 11:12:56 -07:00
|
|
|
self.add_points([
|
|
|
|
interpolate(self.end, self.end+v, t)
|
2015-09-28 16:25:18 -07:00
|
|
|
for t in np.arange(0, 1, self.tip_length*self.epsilon)
|
2015-08-17 11:12:56 -07:00
|
|
|
for v in [
|
|
|
|
rotate_vector(vect, np.pi/4, axis)
|
|
|
|
for axis in IN, OUT
|
|
|
|
]
|
|
|
|
])
|
2015-10-06 18:41:53 -07:00
|
|
|
self.num_tip_points = self.get_num_points()-num_points
|
|
|
|
|
|
|
|
def remove_tip(self):
|
|
|
|
if not hasattr(self, "num_tip_points"):
|
|
|
|
return self
|
|
|
|
for attr in "points", "rgbs":
|
|
|
|
setattr(self, attr, getattr(self, attr)[:-self.num_tip_points])
|
|
|
|
return self
|
2015-08-12 14:24:36 -07:00
|
|
|
|
2015-06-10 22:00:35 -07:00
|
|
|
class CurvedLine(Line):
|
2015-09-28 16:25:18 -07:00
|
|
|
def __init__(self, start, end, via = None, **kwargs):
|
2015-08-17 11:12:56 -07:00
|
|
|
self.set_start_and_end(start, end)
|
2015-06-19 08:31:02 -07:00
|
|
|
if via == None:
|
2015-08-17 11:12:56 -07:00
|
|
|
self.via = rotate_vector(
|
|
|
|
self.end - self.start,
|
2015-06-19 08:31:02 -07:00
|
|
|
np.pi/3, [0,0,1]
|
2015-08-17 11:12:56 -07:00
|
|
|
) + self.start
|
|
|
|
elif isinstance(via, Mobject):
|
|
|
|
self.via = via.get_center()
|
|
|
|
else:
|
|
|
|
self.via = via
|
2015-09-28 16:25:18 -07:00
|
|
|
Line.__init__(self, start, end, **kwargs)
|
2015-06-19 08:31:02 -07:00
|
|
|
|
2015-06-10 22:00:35 -07:00
|
|
|
def generate_points(self):
|
|
|
|
self.add_points([
|
2015-08-17 11:12:56 -07:00
|
|
|
interpolate(
|
|
|
|
interpolate(self.start, self.end, t),
|
|
|
|
self.via,
|
|
|
|
t*(1-t)
|
|
|
|
)
|
2015-06-10 22:00:35 -07:00
|
|
|
for t in np.arange(0, 1, self.epsilon)
|
|
|
|
])
|
|
|
|
|
2015-10-20 21:55:46 -07:00
|
|
|
|
2015-12-19 13:06:09 -08:00
|
|
|
class Arc(Mobject1D):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2015-10-29 13:45:28 -07:00
|
|
|
"radius" : 1.0,
|
|
|
|
"start_angle" : 0,
|
2015-09-28 16:25:18 -07:00
|
|
|
}
|
2015-10-20 21:55:46 -07:00
|
|
|
def __init__(self, angle, **kwargs):
|
2015-10-28 16:03:33 -07:00
|
|
|
digest_locals(self)
|
2015-08-07 18:10:00 -07:00
|
|
|
Mobject1D.__init__(self, **kwargs)
|
|
|
|
|
2015-06-10 22:00:35 -07:00
|
|
|
def generate_points(self):
|
2015-10-20 21:55:46 -07:00
|
|
|
sign = 1 if self.angle >= 0 else -1
|
2015-06-10 22:00:35 -07:00
|
|
|
self.add_points([
|
2015-08-07 18:10:00 -07:00
|
|
|
(self.radius*np.cos(theta), self.radius*np.sin(theta), 0)
|
2015-10-20 21:55:46 -07:00
|
|
|
for theta in np.arange(
|
|
|
|
self.start_angle,
|
|
|
|
self.start_angle+self.angle,
|
|
|
|
sign*self.epsilon/self.radius
|
|
|
|
)
|
2015-06-10 22:00:35 -07:00
|
|
|
])
|
|
|
|
|
2015-12-19 13:06:09 -08:00
|
|
|
class Circle(Arc):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2015-10-20 21:55:46 -07:00
|
|
|
"color" : RED,
|
|
|
|
}
|
|
|
|
def __init__(self, **kwargs):
|
2015-12-19 13:06:09 -08:00
|
|
|
Arc.__init__(self, angle = 2*np.pi, **kwargs)
|
2015-10-20 21:55:46 -07:00
|
|
|
|
2015-10-08 11:50:49 -07:00
|
|
|
class Polygon(Mobject1D):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2015-10-29 13:45:28 -07:00
|
|
|
"color" : GREEN_D,
|
2015-10-09 19:53:38 -07:00
|
|
|
"edge_colors" : None
|
2015-10-08 11:50:49 -07:00
|
|
|
}
|
2016-01-09 20:10:06 -08:00
|
|
|
def __init__(self, *vertices, **kwargs):
|
|
|
|
assert len(vertices) > 1
|
2015-10-28 16:03:33 -07:00
|
|
|
digest_locals(self)
|
2015-10-08 11:50:49 -07:00
|
|
|
Mobject1D.__init__(self, **kwargs)
|
|
|
|
|
|
|
|
def generate_points(self):
|
2015-10-09 19:53:38 -07:00
|
|
|
if self.edge_colors:
|
|
|
|
colors = it.cycle(self.edge_colors)
|
|
|
|
else:
|
|
|
|
colors = it.cycle([self.color])
|
|
|
|
self.indices_of_vertices = []
|
2016-01-09 20:10:06 -08:00
|
|
|
for start, end in adjascent_pairs(self.vertices):
|
2015-10-09 19:53:38 -07:00
|
|
|
self.indices_of_vertices.append(self.get_num_points())
|
|
|
|
self.add_line(start, end, color = colors.next())
|
|
|
|
|
|
|
|
|
|
|
|
def get_vertices(self):
|
2016-01-09 20:10:06 -08:00
|
|
|
return self.vertices[self.indices_of_vertices]
|
2015-10-08 11:50:49 -07:00
|
|
|
|
|
|
|
|
2015-12-19 13:06:09 -08:00
|
|
|
|
|
|
|
class Grid(Mobject1D):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2015-12-19 13:06:09 -08:00
|
|
|
"height" : 6.0,
|
|
|
|
"width" : 6.0,
|
|
|
|
}
|
|
|
|
def __init__(self, rows, columns, **kwargs):
|
|
|
|
digest_config(self, kwargs, locals())
|
|
|
|
Mobject1D.__init__(self, **kwargs)
|
|
|
|
|
|
|
|
def generate_points(self):
|
|
|
|
x_step = self.width / self.columns
|
|
|
|
y_step = self.height / self.rows
|
|
|
|
|
|
|
|
for x in np.arange(0, self.width+x_step, x_step):
|
|
|
|
self.add_line(
|
|
|
|
[x-self.width/2., -self.height/2., 0],
|
|
|
|
[x-self.width/2., self.height/2., 0],
|
|
|
|
)
|
|
|
|
for y in np.arange(0, self.height+y_step, y_step):
|
|
|
|
self.add_line(
|
|
|
|
[-self.width/2., y-self.height/2., 0],
|
|
|
|
[self.width/2., y-self.height/2., 0]
|
|
|
|
)
|
|
|
|
|
|
|
|
class Rectangle(Grid):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2015-10-29 13:45:28 -07:00
|
|
|
"color" : YELLOW,
|
2015-09-28 16:25:18 -07:00
|
|
|
"height" : 2.0,
|
2015-12-19 13:06:09 -08:00
|
|
|
"width" : 4.0,
|
2015-09-28 16:25:18 -07:00
|
|
|
}
|
2015-12-19 13:06:09 -08:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
Grid.__init__(self, 1, 1, **kwargs)
|
|
|
|
|
2015-08-07 18:10:00 -07:00
|
|
|
def generate_points(self):
|
2016-02-29 20:52:25 -08:00
|
|
|
hw = [self.width/2.0, self.height/2.0]
|
2015-08-07 18:10:00 -07:00
|
|
|
self.add_points([
|
2016-02-29 20:52:25 -08:00
|
|
|
(x, u, 0) if dim==1 else (u, x, 0)
|
2015-08-07 18:10:00 -07:00
|
|
|
for dim in 0, 1
|
2016-02-29 20:52:25 -08:00
|
|
|
for u in hw[1-dim], -hw[1-dim]
|
|
|
|
for x in np.arange(-hw[dim], hw[dim], self.epsilon)
|
2015-08-07 18:10:00 -07:00
|
|
|
])
|
|
|
|
|
|
|
|
class Square(Rectangle):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2015-10-09 19:53:38 -07:00
|
|
|
"side_length" : 2.0,
|
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-09 19:53:38 -07:00
|
|
|
for arg in ["height", "width"]:
|
|
|
|
kwargs[arg] = self.side_length
|
2015-10-28 16:03:33 -07:00
|
|
|
Rectangle.__init__(self, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-01-26 10:23:05 -08:00
|
|
|
class FilledRectangle(Mobject1D):
|
2016-02-27 16:32:53 -08:00
|
|
|
CONFIG = {
|
2016-01-26 10:23:05 -08:00
|
|
|
"color" : GREY,
|
|
|
|
"height" : 2.0,
|
|
|
|
"width" : 4.0,
|
|
|
|
}
|
|
|
|
def generate_points(self):
|
|
|
|
self.add_points([
|
|
|
|
(x, y, 0)
|
|
|
|
for x in np.arange(-self.width, self.width, self.epsilon)
|
|
|
|
for y in np.arange(-self.height, self.height, self.epsilon)
|
|
|
|
])
|
|
|
|
|
2015-10-28 16:03:33 -07:00
|
|
|
|
|
|
|
|