add Transform.transformVector method
Adds a transformVector and transformVectors methods to the Transform class. Useful for things like https://github.com/googlefonts/ufo2ft/pull/515
This commit is contained in:
parent
3047ce0006
commit
9c37862087
@ -171,6 +171,31 @@ class Transform(NamedTuple):
|
||||
xx, xy, yx, yy, dx, dy = self
|
||||
return [(xx*x + yx*y + dx, xy*x + yy*y + dy) for x, y in points]
|
||||
|
||||
def transformVector(self, v):
|
||||
"""Transform an (dx, dy) vector, treating translation as zero.
|
||||
|
||||
Example:
|
||||
>>> t = Transform(2, 0, 0, 2, 10, 20)
|
||||
>>> t.transformVector((3, -4))
|
||||
(6, -8)
|
||||
>>>
|
||||
"""
|
||||
(dx, dy) = v
|
||||
xx, xy, yx, yy = self[:4]
|
||||
return (xx*dx + yx*dy, xy*dx + yy*dy)
|
||||
|
||||
def transformVectors(self, vectors):
|
||||
"""Transform a list of (dx, dy) vector, treating translation as zero.
|
||||
|
||||
Example:
|
||||
>>> t = Transform(2, 0, 0, 2, 10, 20)
|
||||
>>> t.transformVectors([(3, -4), (5, -6)])
|
||||
[(6, -8), (10, -12)]
|
||||
>>>
|
||||
"""
|
||||
xx, xy, yx, yy = self[:4]
|
||||
return [(xx*dx + yx*dy, xy*dx + yy*dy) for dx, dy in vectors]
|
||||
|
||||
def translate(self, x=0, y=0):
|
||||
"""Return a new transformation, translated (offset) by x, y.
|
||||
|
||||
|
@ -23,6 +23,14 @@ class TransformTest(object):
|
||||
[(0, 0), (0, 100), (100, 100), (100, 0)]
|
||||
) == [(0, 0), (0, 300), (200, 300), (200, 0)]
|
||||
|
||||
def test_transformVector(self):
|
||||
t = Transform(2, 0, 0, 3, -10, 30)
|
||||
assert t.transformVector((-4, 5)) == (-8, 15)
|
||||
|
||||
def test_transformVectors(self):
|
||||
t = Transform(2, 0, 0, 3, -10, 30)
|
||||
assert t.transformVectors([(-4, 5), (-6, 7)]) == [(-8, 15), (-12, 21)]
|
||||
|
||||
def test_translate(self):
|
||||
t = Transform()
|
||||
assert t.translate(20, 30) == Transform(1, 0, 0, 1, 20, 30)
|
||||
|
Loading…
x
Reference in New Issue
Block a user