mirror of
https://github.com/3b1b/manim.git
synced 2025-08-01 17:29:06 +08:00
Polygon Class
This commit is contained in:
@ -353,6 +353,14 @@ class Mobject1D(Mobject):
|
|||||||
self.epsilon = 1.0 / self.density
|
self.epsilon = 1.0 / self.density
|
||||||
Mobject.__init__(self, **kwargs)
|
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):
|
class Mobject2D(Mobject):
|
||||||
DEFAULT_CONFIG = {
|
DEFAULT_CONFIG = {
|
||||||
"density" : DEFAULT_POINT_DENSITY_2D,
|
"density" : DEFAULT_POINT_DENSITY_2D,
|
||||||
|
@ -78,12 +78,7 @@ class Line(Mobject1D):
|
|||||||
]
|
]
|
||||||
|
|
||||||
def generate_points(self):
|
def generate_points(self):
|
||||||
length = np.linalg.norm(self.end - self.start)
|
self.add_line(self.start, self.end, self.min_density)
|
||||||
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)
|
|
||||||
])
|
|
||||||
|
|
||||||
def get_length(self):
|
def get_length(self):
|
||||||
return np.linalg.norm(self.start - self.end)
|
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)
|
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):
|
class Rectangle(Mobject1D):
|
||||||
DEFAULT_CONFIG = {
|
DEFAULT_CONFIG = {
|
||||||
"color" : "yellow",
|
"color" : "yellow",
|
||||||
|
Reference in New Issue
Block a user