Skip to content

Instantly share code, notes, and snippets.

@uwezi
Last active May 10, 2025 20:06
Show Gist options
  • Save uwezi/e4393857cf0e356a19c63167350c9a75 to your computer and use it in GitHub Desktop.
Save uwezi/e4393857cf0e356a19c63167350c9a75 to your computer and use it in GitHub Desktop.
[better tipped lines] Adding a tip to a line without distorting it. #manim #line #tip
from manim import *
import manim.mobject.geometry.tips as tips
# https://discord.com/channels/581738731934056449/1370706466541011055/1370728872416514141
class reasonablyTipped(Scene):
def construct(self):
def add_tip(
line,
shorten=True,
tip_shape: type[tips.ArrowTip] | None = None,
tip_length: float | None = None,
tip_width: float | None = None,
):
tip = line.get_unpositioned_tip(
tip_length=tip_length,
tip_shape = tip_shape,
tip_width = tip_width,
)
tipangle = Line(tip.base, tip.tip_point).get_angle()
angle = Line(line.point_from_proportion(0.999),line.point_from_proportion(1.0)).get_angle()
tip.rotate(angle-tipangle,about_point=tip.base)
tip.shift(line.get_end()-tip.tip_point)
if shorten==True:
points = line.get_all_points()
points[-1]=tip.base
line.set_points(points)
line.add(tip)
return line
Line.add_tip = add_tip
npl = NumberPlane().add_coordinates()
self.add(npl)
line1 = Line([5,3,0],[2,2,0]).add_tip(tip_length=0.2)
line2 = Line([5,3,0],[5,0,0]).add_tip(tip_length=0.5)
line3 = Line().add_points_as_corners(
[[6,-1,0],[5,-2,0],[4,-1,0],[3,-2,0],[2,-1,0]]
).add_tip_nodist(tip_length=0.5)
self.add(line1,line2,line3)
line1a = Line([-5,3,0],[-2,2,0]).add_tip(shorten=False, tip_length=0.2)
line2a = Line([-5,3,0],[-5,0,0]).add_tip(shorten=False, tip_length=0.5)
line3a = Line().add_points_as_corners(
[[-6,-1,0],[-5,-2,0],[-4,-1,0],[-3,-2,0],[-2,-1,0]]
).add_tip_nodist(shorten=False, tip_length=0.5)
self.add(line1a,line2a,line3a)

When adding a tip to a line in ManimCE using .add_tip() then the complete line object is adjusted to a new shorter length to make space for the tip. Nice for straight lines, but not so nice if you have a line object with corners.

This is a rough attempt to add a tip without this distorsion.

bild

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment