Skip to content

Instantly share code, notes, and snippets.

@uwezi
Last active October 14, 2024 20:43
Show Gist options
  • Save uwezi/45e97353748b56ccf1e5a5e8d20ae63f to your computer and use it in GitHub Desktop.
Save uwezi/45e97353748b56ccf1e5a5e8d20ae63f to your computer and use it in GitHub Desktop.
[Connect the Dots] Three ways to connect a set of dots with lines. #manim #animate #vmobject #line #plotlinegraph #axes
class connectTheDots(Scene):
def construct(self):
xvals = [np.random.uniform(-6.5,6.5) for _ in range(20)]
yvals = [np.random.uniform(-3.5,3.5) for _ in range(20)]
colors = [np.random.choice([RED,GREEN,BLUE,YELLOW,MAROON,ORANGE,WHITE]) for _ in range(20)]
dots = VGroup(
*[Dot(point=[x,y,0], color=c) for x,y,c in zip(xvals,yvals,colors)]
)
self.add(dots)
lines = VGroup(
*[
Line(
start=dots[i].get_center(),
end=dots[i+1].get_center(),
) for i in range(len(dots)-1)
]
)
self.play(
LaggedStart(
*[
Create(
line
) for line in lines
],
lag_ratio=0.5,
),
run_time=4,
rate_func=rate_functions.linear,
)
self.wait(2)
self.play(FadeOut(lines))
ax = Axes(
x_range=[-7,7,1],
y_range=[-4,4,1],
x_length=14,
y_length=8,
tips=False
).add_coordinates()
lineplot = ax.plot_line_graph(
xvals, yvals,
add_vertex_dots=True,
vertex_dot_radius=0.15,
vertex_dot_style={'fill_color':WHITE}
)
self.add(ax)
self.play(Create(lineplot),run_time=4,rate_func=rate_functions.linear)
self.wait()
self.play(FadeOut(lineplot,ax))
self.wait()
vmobj = VMobject(stroke_color=BLUE).set_points_as_corners(
[(x,y,0) for x,y in zip(xvals,yvals)]
)
self.play(Create(vmobj),run_time=4,rate_func=rate_functions.linear)
self.wait()
self.play(FadeOut(vmobj))
self.wait()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment