-
Notifications
You must be signed in to change notification settings - Fork 668
Algebra with Matrices, Rectangles and Points
For the Matrix, Rect, IRect and Point classes algebraic operations are possible to some extent. Following we will describe what they are and how they can be used.
Matrix objects m, m1, m2 can be added, subtracted, negated, inverted or multiplied. The result of these operations always is a new matrix. In addition the "absolute value" of a matrix can be calculated - its Euclidean norm - which is a non-negative floating point number.
-
m1 + m2is defined as the matrix[m1.a + m2.a, m1.b + m2.b, ..., m1.f + m2.f]. -
m1 - m2analogous to addition. -
m1 * m2is defined bym1.concat(m2). -
-mis defined by[-m.a, -m.b, ..., -m.f]. -
~mis defined byinvert(m). Ifmis not invertible (degenerate), then~m = [0, 0, 0, 0, 0, 0]. -
abs(m)is defined bymath.sqrt(m.a**2 + m.b**2 + ... + m.f**2).
Following are some examples to illustrate the above.
- check if a matrix is invertible:
if not ~m: print("m is not invertible") - check whether two matrices are (almost) euqal:
if abs(m1 - m2) < 1E-6: print("m1 almost equal m2") - do a complex matrix calculation:
m = m1*(m2 + ~m3)*m4
Rect and IRect objects can be added and subtracted to / from other rectangles or numbers, they can be negated and multiplied with matrices. In addition, inclusion | and intersection & of rectangles with other objects are supported as binary operations. The result of these operations always is a new rectangle with the same type (Rect or IRect) as the left operand.
-
r1 + r2andr1 - r2are defined component-wise as with matrices. Any of the two operands can be aRector anIRect. In addition,r2can also be a number. -
-ris defined component-wise (RectorIRect). -
r * mis a new rectangle resulting fromrtransformed with matrixm. Works forRectandIRect. -
r1 | r2(inclusion) is the smallest rectangle containing both,r1(RectorIRect) andr2(Rect,IRectorPoint). -
r1 & r2(intersection) is the largest rectangle that is contained in both,r1(RectorIRect) andr2(RectorIRect).
Example ("rectangle hull"):
If plist = [p0, p1, ..., pn] is a list of Point objects then r computed by
r = fitz.Rect(p0, p0)
for p in plist:
r = r | p
is the smallest rectangle containing all points in the list. Note: if you want an IRect perform a r.round() afterwards.
Point objects can be added, subtracted and negated just like rectangles, yielding new point objects.
-
p1 + p2,p1 - p2,-pare defined component-wise like the rectangle operations.p2can also be a number. -
abs(p)is the Eucldean normmath.sqrt(p.x**2 + p.y**2)as with matrices. -
p * mis a new point resulting fromptransformed with matrixm.
HOWTO Button annots with JavaScript
HOWTO work with PDF embedded files
HOWTO extract text from inside rectangles
HOWTO extract text in natural reading order
HOWTO create or extract graphics
HOWTO create your own PDF Drawing
Rectangle inclusion & intersection
Metadata & bookmark maintenance