add scale() for curves, and scaled() for paths

pull/54/head
Orion Elenzil 2018-05-22 12:45:04 -07:00
parent 71b4403375
commit d21a66aff0
1 changed files with 41 additions and 0 deletions

View File

@ -206,6 +206,28 @@ def translate(curve, z0):
raise TypeError("Input `curve` should be a Path, Line, " raise TypeError("Input `curve` should be a Path, Line, "
"QuadraticBezier, CubicBezier, or Arc object.") "QuadraticBezier, CubicBezier, or Arc object.")
def scale(curve, s, origin=None):
"""Scales the curve by the factor s around the origin such that
scale(curve, s, origin).point(t) = ((curve.point(t) - origin) * s) + origin"""
def _scale_point(z, s, origin):
return ((z - origin) * s) + origin
if origin == None:
origin = 0 + 0j
if isinstance(curve, Path):
return Path(*[scale(seg, s, origin) for seg in curve])
elif is_bezier_segment(curve):
return bpoints2bezier([_scale_point(bpt, s, origin) for bpt in curve.bpoints()])
elif isinstance(curve, Arc):
new_start = _scale_point(curve.start, s, origin)
new_end = _scale_point(curve.end, s, origin)
return Arc(new_start, radius=curve.radius * s, rotation=curve.rotation,
large_arc=curve.large_arc, sweep=curve.sweep, end=new_end)
else:
raise TypeError("Input `curve` should be a Path, Line, "
"QuadraticBezier, CubicBezier, or Arc object.")
def bezier_unit_tangent(seg, t): def bezier_unit_tangent(seg, t):
"""Returns the unit tangent of the segment at t. """Returns the unit tangent of the segment at t.
@ -637,6 +659,10 @@ class Line(object):
that self.translated(z0).point(t) = self.point(t) + z0 for any t.""" that self.translated(z0).point(t) = self.point(t) + z0 for any t."""
return translate(self, z0) return translate(self, z0)
def scaled(self, factor, origin=None):
"""Returns a copy of self scaled by the scalar `factor`, about the complex point `origin`."""
return scale(self, factor, origin=origin)
class QuadraticBezier(object): class QuadraticBezier(object):
# For compatibility with old pickle files. # For compatibility with old pickle files.
@ -881,6 +907,10 @@ class QuadraticBezier(object):
that self.translated(z0).point(t) = self.point(t) + z0 for any t.""" that self.translated(z0).point(t) = self.point(t) + z0 for any t."""
return translate(self, z0) return translate(self, z0)
def scaled(self, factor, origin=None):
"""Returns a copy of self scaled by the scalar `factor`, about the complex point `origin`."""
return scale(self, factor, origin=origin)
class CubicBezier(object): class CubicBezier(object):
# For compatibility with old pickle files. # For compatibility with old pickle files.
@ -1121,6 +1151,10 @@ class CubicBezier(object):
that self.translated(z0).point(t) = self.point(t) + z0 for any t.""" that self.translated(z0).point(t) = self.point(t) + z0 for any t."""
return translate(self, z0) return translate(self, z0)
def scaled(self, factor, origin=None):
"""Returns a copy of self scaled by the scalar `factor`, about the complex point `origin`."""
return scale(self, factor, origin=origin)
class Arc(object): class Arc(object):
def __init__(self, start, radius, rotation, large_arc, sweep, end, def __init__(self, start, radius, rotation, large_arc, sweep, end,
@ -1686,6 +1720,9 @@ class Arc(object):
that self.translated(z0).point(t) = self.point(t) + z0 for any t.""" that self.translated(z0).point(t) = self.point(t) + z0 for any t."""
return translate(self, z0) return translate(self, z0)
def scaled(self, factor, origin=None):
"""Returns a copy of self scaled by the scalar `factor`, about the complex point `origin`."""
return scale(self, factor, origin=origin)
def is_bezier_segment(x): def is_bezier_segment(x):
return (isinstance(x, Line) or return (isinstance(x, Line) or
@ -2242,3 +2279,7 @@ class Path(MutableSequence):
"""Returns a copy of self shifted by the complex quantity `z0` such """Returns a copy of self shifted by the complex quantity `z0` such
that self.translated(z0).point(t) = self.point(t) + z0 for any t.""" that self.translated(z0).point(t) = self.point(t) + z0 for any t."""
return translate(self, z0) return translate(self, z0)
def scaled(self, factor, origin=None):
"""Returns a copy of self scaled by the scalar `factor`, about the complex point `origin`."""
return scale(self, factor, origin=origin)