Polygon Class

This commit is contained in:
Grant Sanderson 2015-10-08 11:50:49 -07:00
parent 5b986b9a9e
commit b8fba1f3ec
2 changed files with 25 additions and 6 deletions

View file

@ -353,6 +353,14 @@ class Mobject1D(Mobject):
self.epsilon = 1.0 / self.density
Mobject.__init__(self, **kwargs)
def add_line(self, start, end, min_density = 0.1):
length = np.linalg.norm(end - start)
epsilon = self.epsilon / max(length, min_density)
self.add_points([
interpolate(start, end, t)
for t in np.arange(0, 1, epsilon)
])
class Mobject2D(Mobject):
DEFAULT_CONFIG = {
"density" : DEFAULT_POINT_DENSITY_2D,

View file

@ -78,12 +78,7 @@ class Line(Mobject1D):
]
def generate_points(self):
length = np.linalg.norm(self.end - self.start)
epsilon = self.epsilon / max(length, self.min_density)
self.add_points([
interpolate(self.start, self.end, t)
for t in np.arange(0, 1, epsilon)
])
self.add_line(self.start, self.end, self.min_density)
def get_length(self):
return np.linalg.norm(self.start - self.end)
@ -165,6 +160,22 @@ class Circle(Mobject1D):
for theta in np.arange(0, 2 * np.pi, self.epsilon/self.radius)
])
class Polygon(Mobject1D):
DEFAULT_CONFIG = {
"color" : "limegreen",
}
def __init__(self, *points, **kwargs):
assert len(points) > 1
digest_config(self, Polygon, kwargs)
self.vertices = points
Mobject1D.__init__(self, **kwargs)
def generate_points(self):
points = list(self.vertices) + [self.vertices[0]]
for start, end in zip(points, points[1:]):
self.add_line(start, end)
class Rectangle(Mobject1D):
DEFAULT_CONFIG = {
"color" : "yellow",