Operator Algebra for Geometry Objects
Instances of classes Point, IRect, Rect and Matrix are collectively also called “geometry” objects.
We have defined operators for these classes that allow dealing with them (almost) like ordinary numbers in terms of addition, subtraction, multiplication, division, and some others.
This chapter is a synopsis of what is possible.
General Remarks
- Operators can be either binary (i.e. involving two objects) or unary.
- The result of binary operatorions is either a new object of the same class as the left operand or a bool.
- The result of unary operations is either a bool, a float or the same object type.
- All binary operators fully support in-place operations, i.e. if the operator is called “°”, then something like
a °= b
is equivalent toa = a ° b
. - The following binary operators are defined for all classes:
+, -, *, /
. They have a similar meaning as the corresponding numerical ones. - Rectangles have two additional binary operators
&, |
, details below. - For binary operations, the second operand may have a different type as the left one. Often, Python sequences (lists, tuples, arrays) are also allowed here. We allude to this fact by saying “point-like object” when we mean, that a Point is possible as well as a sequence of two numbers. Similar applies to “rect-like” (sequence length 4) or “matrix-like” (sequence length 6).
Unary Operations
bool(o)
- is false if and only if the components ofo
are all zero.abs(o)
- is the Euclidean norm (square root of the sum of component squares) ifo
is a Point or a Matrix. For rectangles, the area is returned (result ofgetArea()
).+o
- is a copy ofo
.-o
- is a copy ofo
with negated components.~m
- is the inverse of Matrixm
. The other geometry objects are not invertible w/r to multiplication.
Binary Operations
For the operators +, -, *, /
, the second operand may be a number, which will be applied component-wise.
a + b
,a - b
- component-wise execution,b
must bea
-like.a * b
,a / b
- does the following for matrix-likesb
:- If
a
is a point or a rectangle, thena.transform(b)
, resp.a.transform(~b)
is executed. - If
a
is a matrix, thena * b
, resp.a * ~b
is executed.
- If
a & b
- intersection rectangle:a
must be a rectangle andb
rect-like.a | b
- union rectangle:a
must be a rectangle, andb
can be point-like or rect-like.b in a
- ifb
is a number, thenb in tuple(a)
is returned. Ifb
is point-like or rect-like, thena
must be a rectangle, and the result ofa.contains(b)
is returned.a == b
- is true ifabs(a - b) == 0
andtype(a) == type(b)
(but maybe we haveid(a) != id(b)
).