[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[ft] python_freetype Design: Vector & Matrix Operations
From: |
Lawrence D'Oliveiro |
Subject: |
[ft] python_freetype Design: Vector & Matrix Operations |
Date: |
Fri, 13 Feb 2015 12:51:20 +1300 |
Python has powerful operator-overloading capabilities, implemented in a
much simpler way than, say, C++. It makes sense to take advantage of
these where it can be useful in a language binding.
This is what I did in python_freetype
<https://github.com/ldo/python_freetype>. To multiply two matrices m1
and m2, you can directly write the Python expression “m1 * m2”. I even
use this for vector transformations: to transform a vector v by a
matrix m, you can simply write “m * v”.
While I was at it, I found a use for matrix division. Thus, to multiply
matrix m1 by the inverse of m2, you could write “m1 * m2.inv()”. Or, to
shorten it: “m1 / m2”.
Vector and Matrix methods are functional, not procedural: they always
return a new object, they do not modify the object they are called on.
Should angles be in degrees or radians? There never seems to be a
simple answer to this, since both have their advantages and
disadvantages. I have adopted the compromise convention that,
everywhere there is an angle argument, there is an accompanying boolean
called “degrees”, which should be True to indicate the angle is in
degrees, False for radians. For a function returning an angle, there
should be a corresponding “degrees” argument indicating the units that
the caller wants for the result.
Putting it all together, you can write expressions like
Matrix.rotation(45, True) * Matrix.rotation(45, True) * Vector(1, 0)
which applies a net 90° rotation to the vector, giving the result
Vector(0.000, 1.000)
And of course you can construct a Matrix and apply it directly to a
FreeType call, e.g.
a_face.glyph.outline.transform(Matrix.skewing(1, 0))
- [ft] python_freetype Design: Vector & Matrix Operations,
Lawrence D'Oliveiro <=