You are here: Start » PyAVL » Operators on Geometry Types

Operators on Geometry Types

PyAVL lets the geometry types, Location, Matrix and the shape types be combined with the Python operators +, -, *, / and @. Where Aurora Vision Library already provides the operation, the operator calls the same function, so the result and the errors are identical to those of the function.

The operators never modify their operands. Each operation returns a new object. A combination that is not listed on this page raises the Python TypeError exception.

The types of PyAVL and PyAVL-DL are interchangeable. An object created with AVL can be combined with an object created with AVLDL.

Points and Vectors

The table applies to the 2D types (Point2D, Vector2D) and to the 3D types (Point3D, Vector3D). Both operands must have the same dimension.

ExpressionResultMeaning
point + vectorPointThe point moved by the vector.
point - vectorPointThe point moved by the opposite vector.
point + pointPointThe sum of the coordinates.
point - pointPointThe difference of the coordinates.
point * number, number * pointPointThe coordinates multiplied by the number (scaling about the origin).
point / numberPointThe coordinates divided by the number.
vector + vector, vector - vectorVectorThe sum or the difference of the vectors.
vector * number, number * vectorVectorThe vector multiplied by the number.
vector / numberVectorThe vector divided by the number.
-vectorVectorThe opposite vector.
vector @ vectorfloatThe dot product of the vectors.

The operators * and / accept a Python int or float. Dividing by zero raises AVL.DomainError. Multiplying two vectors with * is not defined; use @ for the dot product.

import AVL

p = AVL.Point2D(10, 20)
v = AVL.Vector2D(3, -4)

moved = p + v                      # Point2D (13, 16)
doubled = v * 2                    # Vector2D (6, -8)
halved = v / 2                     # Vector2D (1.5, -2)
projection = v @ AVL.Vector2D(1, 0)   # 3.0, the dot product
difference = AVL.Point2D(5, 5) - p    # Point2D (-5, -15)

Locations

Location holds integer coordinates, so only Python integers are accepted as the second operand. The result is a Location.

ExpressionMeaning
location + location, location - locationThe sum or the difference of the coordinates.
location + int, int + location, location - intThe integer is added to or subtracted from both coordinates.
location * int, int * locationBoth coordinates multiplied by the integer.
-locationBoth coordinates negated.

A floating-point operand, for example location + 1.5, raises TypeError. A result that does not fit the 32-bit integer range raises OverflowError.

Translating Shapes

Adding a vector to a shape moves the shape; subtracting a vector moves it in the opposite direction. The operands of + can be given in either order (shape + vector or vector + shape). A shape cannot be subtracted from a vector, so vector - shape raises TypeError.

ShapesVectorFunction used
RegionVector2DTranslateRegion
BoxVector2DTranslateBox
Rectangle2DVector2DTranslateRectangle
Circle2DVector2DTranslateCircle
Ellipse2DVector2DThe center is moved by the vector.
Segment2DVector2DTranslateSegment
Line2DVector2DTranslateLine
Arc2DVector2DTranslateArc
PathVector2DTranslatePath
Box3DVector3DTranslateBox3D
Circle3DVector3DTranslateCircle3D
Sphere3DVector3DThe center is moved by the vector.
Segment3DVector3DTranslateSegment3D
Line3DVector3DTranslateLine3D
Plane3DVector3DTranslatePlane

A 2D shape requires a Vector2D and a 3D shape requires a Vector3D. Mixing them, for example Box + Vector3D, raises TypeError.

Notes

  • Regions and boxes are defined on the pixel grid, so they can only be moved by whole pixels. The components of the vector are rounded to the nearest integer. A component that is not finite or does not fit the integer range raises AVL.DomainError. A translated region keeps its frame size.
  • Lines in 3D are stored in a normalized form, as in TranslateLine3D. The point of a translated Line3D is the point of the line closest to the origin, so it may differ from the original point plus the vector along the direction of the line.
  • Planes without a defined orientation (all of the first three coefficients equal to zero) cannot be translated and raise AVL.DomainError. The same applies to a Circle3D in such a plane.
import AVL

delta = AVL.Vector2D(5, -3)

box = AVL.Box(10, 20, 30, 40)
shifted_box = box + delta              # Box (15, 17, 30, 40)

circle = AVL.Circle2D(50, 50, 10)
moved_circle = circle - delta          # center (45, 53)

region = AVL.Region()
AVL.CreateBoxRegion(box, 100, 100, region)
moved_region = region + delta          # the same as AVL.TranslateRegion(region, 5, -3, ...)

sphere = AVL.Sphere3D(1, 2, 3, 4)
moved_sphere = sphere + AVL.Vector3D(10, 0, 0)   # center (11, 2, 3)

Transforming 3D Points and Vectors

A Matrix can be applied to a 3D vector or point with the @ operator. See Working with Matrices.

Errors

ExceptionCause
TypeErrorThe operator is not defined for the operand types, for example vector * vector, Location + float, Box + Vector3D or vector - shape.
AVL.DomainErrorThe operands are of the right types but their values are not valid, for example a division by zero, a translation of a region by a value that is not finite, or an indefinite plane.
OverflowErrorThe result of an operation on a Location does not fit the integer range.

See Also

Previous: Deep Learning (PyAVL-DL) Next: Working with Matrices