Skip to content

Instantly share code, notes, and snippets.

@uwezi
Last active January 23, 2025 19:40
Show Gist options
  • Save uwezi/55acc7c8384acc3a79cb4ad92f3cdd60 to your computer and use it in GitHub Desktop.
Save uwezi/55acc7c8384acc3a79cb4ad92f3cdd60 to your computer and use it in GitHub Desktop.
[openglCylinder] A cylinder with arbitrary orientation for the opengl renderer. #manim #3D #cylinder #opengl
# https://discord.com/channels/581738731934056449/1332024484496150589/1332024484496150589
from manim import *
from manim.opengl import *
config.renderer="opengl"
config.write_to_movie=True
class openglCylinder(OpenGLSurface):
def __init__(self, start=ORIGIN, end=LEFT, radius=0.05, color=WHITE, **kwargs):
start = np.array(start)
end = np.array(end)
direction = end-start
theta = np.arctan2(direction[1],direction[0])
phi = np.arctan2(direction[2],np.linalg.norm(direction[0:2]))
length = np.linalg.norm(direction)
def uvfunc(u,v): # cylinder around x-axis
return np.array([v, radius * np.cos(u), radius * np.sin(u)])
super().__init__(
uv_func=uvfunc,
u_range=[0,2*PI],
v_range=[0,length],
fill_color=color,
**kwargs
)
self.rotate(angle=phi, axis=DOWN, about_point=ORIGIN)
self.rotate(angle=theta, axis=OUT, about_point=ORIGIN)
self.shift(start)
class testcylinder(ThreeDScene):
def construct(self):
self.set_camera_orientation(phi=75 * DEGREES, theta=-60 * DEGREES)
ax = ThreeDAxes().add_coordinates()
self.add(ax)
self.add(
openglCylinder(start=ax.c2p(0,0,0),end=ax.c2p(3,3,3))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment