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 °= bis 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 ofoare all zero.abs(o)- is the Euclidean norm (square root of the sum of component squares) ifois a Point or a Matrix. For rectangles, the area is returned (result ofgetArea()).+o- is a copy ofo.-o- is a copy ofowith 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,bmust bea-like.a * b,a / b- does the following for matrix-likesb:- If
ais a point or a rectangle, thena.transform(b), resp.a.transform(~b)is executed. - If
ais a matrix, thena * b, resp.a * ~bis executed.
- If
a & b- intersection rectangle:amust be a rectangle andbrect-like.a | b- union rectangle:amust be a rectangle, andbcan be point-like or rect-like.b in a- ifbis a number, thenb in tuple(a)is returned. Ifbis point-like or rect-like, thenamust be a rectangle, and the result ofa.contains(b)is returned.a == b- is true ifabs(a - b) == 0andtype(a) == type(b)(but maybe we haveid(a) != id(b)).