10
10
from ...animation .growing import GrowFromCenter
11
11
from ...constants import *
12
12
from ...mobject .geometry import Line
13
+ from ...mobject .svg .svg_path import SVGPathMobject
13
14
from ...mobject .svg .tex_mobject import MathTex , Tex
14
15
from ...mobject .types .vectorized_mobject import VMobject
16
+ from ...utils .color import BLACK
15
17
from ...utils .space_ops import get_norm
16
18
17
19
18
- class Brace (MathTex ):
20
+ class Brace (SVGPathMobject ):
19
21
"""Takes a mobject and draws a brace adjacent to it.
20
22
21
23
Passing a direction vector determines the direction from which the
@@ -51,35 +53,43 @@ def __init__(
51
53
mobject ,
52
54
direction = DOWN ,
53
55
buff = 0.2 ,
54
- width_multiplier = 2 ,
55
- max_num_quads = 15 ,
56
- min_num_quads = 0 ,
56
+ sharpness = 2 ,
57
+ stroke_width = 0 ,
58
+ fill_opacity = 1. 0 ,
57
59
background_stroke_width = 0 ,
60
+ background_stroke_color = BLACK ,
58
61
** kwargs
59
62
):
60
- self . width_multiplier = width_multiplier
61
- self . max_num_quads = max_num_quads
62
- self . min_num_quads = min_num_quads
63
+ path_string_template = "m0.01216 0c-0.01152 0-0.01216 6.103e-4 -0.01216 0.01311v0.007762c0.06776 0.122 0.1799 0.1455 0.2307 0.1455h{0}c0.03046 3.899e-4 0.07964 0.00449 0.1246 0.02636 0.0537 0.02695 0.07418 0.05816 0.08648 0.07769 0.001562 0.002538 0.004539 0.002563 0.01098 0.002563 0.006444-2e-8 0.009421-2.47e-5 0.01098-0.002563 0.0123-0.01953 0.03278-0.05074 0.08648-0.07769 0.04491-0.02187 0.09409-0.02597 0.1246-0.02636h{0}c0.05077 0 0.1629-0.02346 0.2307-0.1455v-0.007762c-1.78e-6 -0.0125-6.365e-4 -0.01311-0.01216-0.01311-0.006444-3.919e-8 -0.009348 2.448e-5 -0.01091 0.002563-0.0123 0.01953-0.03278 0.05074-0.08648 0.07769-0.04491 0.02187-0.09416 0.02597-0.1246 0.02636h{1}c-0.04786 0-0.1502 0.02094-0.2185 0.1256-0.06833-0.1046-0.1706-0.1256-0.2185-0.1256h{1}c-0.03046-3.899e-4 -0.07972-0.004491-0.1246-0.02636-0.0537-0.02695-0.07418-0.05816-0.08648-0.07769-0.001562-0.002538-0.004467-0.002563-0.01091-0.002563z"
64
+ default_min_width = 0.90552
65
+
63
66
self .buff = buff
67
+
64
68
angle = - np .arctan2 (* direction [:2 ]) + np .pi
65
69
mobject .rotate (- angle , about_point = ORIGIN )
66
70
left = mobject .get_corner (DOWN + LEFT )
67
71
right = mobject .get_corner (DOWN + RIGHT )
68
72
target_width = right [0 ] - left [0 ]
73
+ linear_section_length = max (
74
+ 0 , (target_width * sharpness - default_min_width ) / 2
75
+ )
69
76
70
- # Adding int(target_width) qquads gives approximately the right width
71
- num_quads = np .clip (
72
- int (self .width_multiplier * target_width ),
73
- self .min_num_quads ,
74
- self .max_num_quads ,
77
+ path = path_string_template .format (
78
+ linear_section_length , - linear_section_length
75
79
)
76
- tex_string = "\\ underbrace{%s}" % (num_quads * "\\ qquad" )
77
- MathTex .__init__ (
78
- self , tex_string , background_stroke_width = background_stroke_width , ** kwargs
80
+
81
+ SVGPathMobject .__init__ (
82
+ self ,
83
+ path_string = path ,
84
+ stroke_width = stroke_width ,
85
+ fill_opacity = fill_opacity ,
86
+ background_stroke_width = background_stroke_width ,
87
+ background_stroke_color = background_stroke_color ,
88
+ ** kwargs
79
89
)
80
- self .tip_point_index = np .argmin (self .get_all_points ()[:, 1 ])
81
90
self .stretch_to_fit_width (target_width )
82
91
self .shift (left - self .get_corner (UP + LEFT ) + self .buff * DOWN )
92
+
83
93
for mob in mobject , self :
84
94
mob .rotate (angle , about_point = ORIGIN )
85
95
@@ -104,10 +114,8 @@ def get_tex(self, *tex, **kwargs):
104
114
return tex_mob
105
115
106
116
def get_tip (self ):
107
- # Very specific to the LaTeX representation
108
- # of a brace, but it's the only way I can think
109
- # of to get the tip regardless of orientation.
110
- return self .get_all_points ()[self .tip_point_index ]
117
+ # Returns the position of the seventh point in the path, which is the tip.
118
+ return self .points [28 ] # = 7*4
111
119
112
120
def get_direction (self ):
113
121
vect = self .get_tip () - self .get_center ()
0 commit comments