Skip to content

Instantly share code, notes, and snippets.

@abul4fia
Last active March 5, 2024 08:21
Show Gist options
  • Save abul4fia/c096930dd7cafe9b6a8c5c3723b618e0 to your computer and use it in GitHub Desktop.
Save abul4fia/c096930dd7cafe9b6a8c5c3723b618e0 to your computer and use it in GitHub Desktop.
New LatexTip for latex-style arrows

LatexTip

Manim provides few styles of arrow tips, basically a filled triangle and the "stealth".

This python module implements Latex-style arrow tips. The shape is a perfect copy of the one used by Computer Modern fonts. It was produced by exporting to svg a latex document containing $\to$, and editing the resulting SVG using Inkscape to remove the arrow stem and leave only the tip. This SVG was read into manim to extract the array of points used to define this class.

This is an example of several arrows with several kinds of tips pointing in all directions (the code is the file example.py in this gist.

image

from manim import *
from latex_tip import LatexTip
class Test(Scene):
def construct(self):
tips = [LatexTip, ArrowTriangleFilledTip, StealthTip]
c = Circle().scale(4)
for i in range(40):
tip = tips[i % 3]
a = Arrow(ORIGIN, c.point_from_proportion(i / 40), tip_shape=tip)
self.add(a)
from manim import *
class LatexTip(ArrowTip):
def __init__(
self,
fill_opacity=1,
stroke_width=3,
length=DEFAULT_ARROW_TIP_LENGTH / 2,
start_angle=PI,
**kwargs,
):
self.start_angle = start_angle
VMobject.__init__(
self, fill_opacity=fill_opacity, stroke_width=stroke_width, **kwargs
)
pts = [
[0.13040412163656126, -0.15336252690279117, 1.8781492768065307e-17],
[-0.29132632741912245, -0.4748715152945279, 5.815498812116969e-17],
[-0.4979583035543512, -0.789178398566812, 9.664647997610804e-17],
[-0.559258018625476, -0.8889401215389154, 1.088637674476292e-16],
[-0.559258018625476, -0.8889401215389154, 1.088637674476292e-16],
[-0.9042122731429533, -1.417547152496847, 1.7359945829457088e-16],
[-0.9655043033165742, -1.900239633186283, 2.3271223843945216e-16],
[-0.9655043033165742, -1.9079322155875946, 2.3365430808094653e-16],
[-0.9655043033165742, -1.9079322155875946, 2.3365430808094653e-16],
[-0.9655043033165742, -1.9999999999999993, 2.4492935982947054e-16],
[-0.873443299696082, -1.9999999999999993, 2.4492935982947054e-16],
[-0.8121426805193717, -1.9999999999999993, 2.4492935982947054e-16],
[-0.8121426805193717, -1.9999999999999993, 2.4492935982947054e-16],
[-0.6818502159230145, -1.9999999999999993, 2.4492935982947054e-16],
[-0.6741580855744985, -1.9846166434085502, 2.430454419884845e-16],
[-0.6436350288477489, -1.8466347607804396, 2.261475348984003e-16],
[-0.6436350288477489, -1.8466347607804396, 2.261475348984003e-16],
[-0.46742710987730773, -1.0956706451833076, 1.341809548543453e-16],
[-0.015265370812125336, -0.4521630952235609, 5.537400872580937e-17],
[0.8505965514700535, -0.0995162583047747, 1.2187226719606352e-17],
[0.8505965514700535, -0.0995162583047747, 1.2187226719606352e-17],
[0.9424274602182247, -0.06898461257493264, 8.448178498031151e-18],
[0.9655043033165771, -0.05359944777230632, 6.564039215042066e-18],
[0.9655043033165771, -9.041055885253818e-07, 1.1072100150788427e-22],
[0.9655043033165771, -9.041055885253818e-07, 1.1072100150788427e-22],
[0.9655043033165771, 0.05360894088098726, -6.565201785558075e-18],
[0.9195874927841087, 0.07668352371536691, -9.391023186536422e-18],
[0.9042041361926566, 0.08437565406388865, -1.033303746753056e-17],
[0.9042041361926566, 0.08437565406388865, -1.033303746753056e-17],
[0.5669424640764967, 0.21466811866024282, -2.6289262439625056e-17],
[-0.3602196253296055, 0.597599328430368, -7.318481047348581e-17],
[-0.6513276112490635, 1.8848612491755692, -2.3082892956397423e-16],
[-0.6513276112490635, 1.8848612491755692, -2.3082892956397423e-16],
[-0.6741607978912669, 1.9769231569016499, -2.421032616259886e-16],
[-0.6818456953950748, 1.9999999999999993, -2.4492935982947054e-16],
[-0.8121426805193745, 1.9999999999999993, -2.4492935982947054e-16],
[-0.8121426805193745, 1.9999999999999993, -2.4492935982947054e-16],
[-0.8734432996960849, 1.9999999999999993, -2.4492935982947054e-16],
[-0.9655043033165771, 1.9999999999999993, -2.4492935982947054e-16],
[-0.9655043033165771, 1.9079385443267172, -2.3365508312795747e-16],
[-0.9655043033165771, 1.9079385443267172, -2.3365508312795747e-16],
[-0.9655043033165771, 1.8925506672073253, -2.317706116819638e-16],
[-0.8965201427944373, 1.4098613508874478, -1.7265821906058763e-16],
[-0.5746427313753123, 0.8963994446983478, -1.0977727107072965e-16],
[-0.5746427313753123, 0.8963994446983478, -1.0977727107072965e-16],
[-0.4215193404007274, 0.6665911738500118, -8.163387473952938e-17],
[-0.18624755947997798, 0.41500616373985083, -5.0823597005043066e-17],
[0.13040412163655557, 0.15312971971370742, -1.875298211017232e-17],
[0.13040412163655557, 0.15312971971370742, -1.875298211017232e-17],
[0.22087977610729068, 0.07830458503587787, -9.589545942274956e-18],
[0.26830825118403845, 0.03783862709758074, -4.633895355918257e-18],
[0.26940357510472646, -0.001999429509370092, 2.4485949487708463e-19],
[0.26940357510472646, -0.001999429509370092, 2.4485949487708463e-19],
[0.2704885018111478, -0.041838842274707994, 5.123780427175211e-18],
[0.2252606197372555, -0.08104809344473542, 9.925528821409081e-18],
[0.13040412163655557, -0.15336252690278834, 1.878149276806496e-17],
]
self.set_points(pts)
self.scale(length / self.length)
@property
def length(self):
# The 5 factor was fine-tuned to provide a good length
return np.linalg.norm(self.vector) * 5
@property
def tip_point(self):
a = self.get_anchors()
return a[len(a)//2-2]
@property
def base(self):
a = self.get_anchors()
return a[-2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment