new class RoundedRectangle and SVG support for it

This commit is contained in:
Ben Hambrecht 2018-04-12 23:19:09 +02:00
parent cf5d461aa6
commit ca8135a90f
2 changed files with 61 additions and 29 deletions

View file

@ -728,6 +728,41 @@ class Square(Rectangle):
) )
class RoundedRectangle(Rectangle):
CONFIG = {
"corner_radius" : 0.5,
"close_new_points" : True
}
def generate_points(self):
y, x = self.height / 2., self.width / 2.
r = self.corner_radius
arc_ul = ArcBetweenPoints(x * LEFT + (y - r) * UP, (x - r) * LEFT + y * UP, angle = -TAU/4)
arc_ur = ArcBetweenPoints((x - r) * RIGHT + y * UP, x * RIGHT + (y - r) * UP, angle = -TAU/4)
arc_lr = ArcBetweenPoints(x * RIGHT + (y - r) * DOWN, (x - r) * RIGHT + y * DOWN, angle = -TAU/4)
arc_ll = ArcBetweenPoints(x * LEFT + (y - r) * DOWN, (x - r) * LEFT + y * DOWN, angle = TAU/4) # sic! bug in ArcBetweenPoints?
points = arc_ul.points
points = np.append(points,np.array([y * UP]), axis = 0)
points = np.append(points,np.array([y * UP]), axis = 0)
points = np.append(points,arc_ur.points, axis = 0)
points = np.append(points,np.array([x * RIGHT]), axis = 0)
points = np.append(points,np.array([x * RIGHT]), axis = 0)
points = np.append(points,arc_lr.points, axis = 0)
points = np.append(points,np.array([y * DOWN]), axis = 0)
points = np.append(points,np.array([y * DOWN]), axis = 0)
points = np.append(points,arc_ll.points[::-1], axis = 0) # sic! see comment above
points = np.append(points,np.array([x * LEFT]), axis = 0)
points = np.append(points,np.array([x * LEFT]), axis = 0)
points = np.append(points,np.array([x * LEFT + (y - r) * UP]), axis = 0)
points = points[::-1]
self.set_points(points)
class Grid(VMobject): class Grid(VMobject):
CONFIG = { CONFIG = {
"height": 6.0, "height": 6.0,

View file

@ -9,6 +9,7 @@ from utils.color import *
from constants import * from constants import *
from mobject.geometry import Circle from mobject.geometry import Circle
from mobject.geometry import Rectangle from mobject.geometry import Rectangle
from mobject.geometry import RoundedRectangle
from utils.bezier import is_closed from utils.bezier import is_closed
from utils.config_ops import digest_config from utils.config_ops import digest_config
from utils.config_ops import digest_locals from utils.config_ops import digest_locals
@ -35,7 +36,7 @@ class SVGMobject(VMobject):
"file_name": None, "file_name": None,
"unpack_groups": True, # if False, creates a hierarchy of VGroups "unpack_groups": True, # if False, creates a hierarchy of VGroups
"stroke_width": 0, "stroke_width": 0,
"fill_opacity": 1, "fill_opacity": 1.0,
# "fill_color" : LIGHT_GREY, # "fill_color" : LIGHT_GREY,
"propagate_style_to_family": True, "propagate_style_to_family": True,
} }
@ -159,55 +160,51 @@ class SVGMobject(VMobject):
fill_color = rect_element.getAttribute("fill") fill_color = rect_element.getAttribute("fill")
stroke_color = rect_element.getAttribute("stroke") stroke_color = rect_element.getAttribute("stroke")
stroke_width = rect_element.getAttribute("stroke-width") stroke_width = rect_element.getAttribute("stroke-width")
print "fill_color =", fill_color corner_radius = rect_element.getAttribute("rx")
print "stroke_color =", stroke_color
print "stroke_width =", stroke_width
# input preprocessing # input preprocessing
if fill_color in ["", "none", "#FFF", "#FFFFFF"] or Color(fill_color) == Color(WHITE): if fill_color in ["", "none", "#FFF", "#FFFFFF"] or Color(fill_color) == Color(WHITE):
print "no fill"
opacity = 0 opacity = 0
fill_color = BLACK # shdn't be necessary but avoids error msgs fill_color = BLACK # shdn't be necessary but avoids error msgs
if fill_color in ["#000", "#000000"]: if fill_color in ["#000", "#000000"]:
print "flipping fill color"
fill_color = WHITE fill_color = WHITE
if stroke_color in ["", "none", "#FFF", "#FFFFFF"] or Color(stroke_color) == Color(WHITE): if stroke_color in ["", "none", "#FFF", "#FFFFFF"] or Color(stroke_color) == Color(WHITE):
stroke_width = 0 stroke_width = 0
stroke_color = BLACK stroke_color = BLACK
print "no stroke color"
if stroke_color in ["#000", "#000000"]: if stroke_color in ["#000", "#000000"]:
print "flipping stroke color"
stroke_color = WHITE stroke_color = WHITE
if stroke_width in ["", "none", "0"]: if stroke_width in ["", "none", "0"]:
print "no stroke width"
stroke_width = 0 stroke_width = 0
# is there sth to draw? # is there sth to draw?
if opacity == 0 and stroke_width == 0: if opacity == 0 and stroke_width == 0:
print "nothing to draw"
return return
print "after preprocessing:" if corner_radius in ["", "0", "none"]:
print "fill_color =", fill_color corner_radius = 0
print "stroke_color =", stroke_color
print "stroke_width =", stroke_width
print "opacity = ", opacity
corner_radius = float(corner_radius)
if corner_radius == 0:
mob = Rectangle(
width = float(rect_element.getAttribute("width")),
height = float(rect_element.getAttribute("height")),
stroke_width = stroke_width,
stroke_color = stroke_color,
fill_color = fill_color,
fill_opacity = opacity
)
else:
mob = RoundedRectangle(
width = float(rect_element.getAttribute("width")),
height = float(rect_element.getAttribute("height")),
stroke_width = stroke_width,
stroke_color = stroke_color,
fill_color = fill_color,
fill_opacity = opacity,
corner_radius = corner_radius
)
# if rect_element.hasAttribute("fill"):
# color_attr = str(rect_element.getAttribute("fill"))
# if color_attr == "none":
# return
# elif Color(color_attr) == Color(WHITE):
# return
mob = Rectangle(
width=float(rect_element.getAttribute("width")),
height=float(rect_element.getAttribute("height")),
stroke_width = stroke_width,
stroke_color = stroke_color,
fill_color = fill_color,
fill_opacity = opacity
)
mob.shift(mob.get_center() - mob.get_corner(UP + LEFT)) mob.shift(mob.get_center() - mob.get_corner(UP + LEFT))
return mob return mob