The class shape represents 2D geometric elements with floating (or subpixel) precision. Any shape is given by a set of base points and an optional transformation matrix. The following shapes are currently defined:
- Line (a direct connection between two points)
- Rectangle (an rectangle that is mainly defined by the top-left and bottom-right corner point)
- Square (similar to a rectangle, however the side lengths are equal. It is defined by the center point and the side lengths)
- Ellipse (an ellipse that is defined by the top-left and bottom-right corner of its outer rectangle)
- Circle (similar to an ellipse, however the side lengths are equal. It is defined by the center point and the side lengths)
- Polygon (polygon with n points)
Examples for the creation of shapes are:
point = shape(shape.Point, (0,0)) line = shape(shape.Line, (0,0), (100,50)) rect = shape(shape.Rectangle, (20,20), (70,100)) #top-left, bottom-right square = shape(shape.Square, (30,-50), 20) #center, side-length ellipse = shape(shape.Ellipse, (-50,-70), (-20, 0)) #top-left, bottom-right circle = shape(shape.Circle, (-30, 100), 40) #center, side-length
If the optional transformation matrix (2x3 float64 matrix) is set, the shape can be translated and/or rotated. Please consider, that any rotation is currently not supported in any plot. Rectangles, squares, ellipses or circles are always defined, such that their main axes are parallel to the x- and y-axis. Use the rotation to choose another principal orientation. The base points of the shape are never affected by any transformation matrix. Only the contour points can be requested with the applied coordinate transformation (if desired).
It is possible to obtain a
region from any shape with a valid area (points and lines don’t have an area). The region is always
a pixel-precise structure. Regions can be combined using union or intersection operators.
The demo script demoShapes.py show further examples about the usage of shape objects.
shape([type, param1, param2, index, name]) → creates a shape object of a specific type.¶
Depending on the type, the following parameters are allowed:
- shape.Invalid: -
- shape.Point: point
- shape.Line: start-point, end-point
- shape.Rectangle: top left point, bottom right point
- shape.Square: center point, side-length
- shape.Ellipse: top left point, bottom right point of bounding box
- shape.Circle: center point, radius
- shape.Polygon : 2xM float64 array with M points of polygon
parameters point, start-point, ... can be all array-like types (e.g. dataObject, list, tuple, np.array) that can be mapped to float64 and have two elements.
During construction, all shapes are aligned with respect to the x- and y-axis. Set a 2d transformation (attribute ‘transform’) to rotate and move it.
contour([applyTrafo = True, tol = -1.0]) → return contour points as a 2xNumPoints float64 dataObject¶
If a transformation matrix is set, the base points can be transformed if ‘applyTrafo’ is True. For point, line and rectangle based shapes, the contour is directly given. For ellipses and circles, a polygon is approximated to the form of the ellipse and returned as contour. The approximation is done by line segments. Use ‘tol’ to set the maximum distance between each line segment and the real shape. If ‘tol’ is -1.0, ‘tol’ is set to 1% of the smallest diameter.
normalized() -> return a normalized shape (this has only an impact on rectangles, squares, ellipses and circles)¶
The normalized shape guarantees that the bounding box of the shape never has a non-negative width or height. Therefore, the order or position of the two corner points (base points) is switched or changed, if necessary. Shapes different than rectangle, square, circle or ellipse are not affected by this and are returned as they are.
region() → Return a region object from this shape.¶
The region object only contains valid regions if the shape has an area > 0. A region object is an integer based object (pixel raster), therefore the shapes are rounded to the nearest fixed-point coordinates.
rotateDeg(array-like object) → Rotate shape by given angle in degree (counterclockwise).¶
rotateRad(array-like object) → Rotate shape by given angle in radians (counterclockwise).¶
translate(array-like object) → Translate shape by given (dx,dy) value.¶
Get/set angle of optional rotation matrix in degree (counter-clockwise).
Get/set angle of optional rotation matrix in radians (counter-clockwise).
Get area of shape (points and lines have an empty area).
Return base points of this shape: M base points of shape as 2xM float64 dataObject
The base points are untransformed points that describe the shape dependent on its type:
- shape.Point: one point
- shape.Line : start point, end point
- shape.Rectangle, shape.Square : top left point, bottom right point
- shape.Ellipse, shape.Circle : top left point, bottom right point of bounding box
- shape.Polygon : points of polygon, the last and first point are connected, too.
Get/set center point
The center point is the point (type: point), or the center of the line (type: line) or bounding box of a rectangle, square, ellipse or circle.
Get/set bitmask with flags for this shape: Shape.MoveLock, Shape.RotateLock, Shape.ResizeLock
Get/set height of shape.
A height can only be set for a square or for a rectangle and is defined with respect to the base coordinate system.
Get/set index of shape. The default is -1, however if the shape is a geometric shape of a plot, an auto-incremented index is assigned once the shape is drawn or set. If >= 0 it is possible to modify an existing shape with the same index.
Get/set name (label) of the shape.
Get/set first point (of bounding box)
The first point is the first point (type: point or line) or the upper left point of the bounding box (type: rectangle, square, ellipse, circle). The point always considers a possible 2D coordinate transformation matrix.
Get/set second point of bounding box
The second point is the bottom right point of the bounding box (type: rectangle, square, ellipse, circle). The point always considers a possible 2D coordinate transformation matrix.
Get/set radius of shape.
A radius can only be set for a circle (float value) or for an ellipse (a, b) as half side-length in x- and y-direction of the base coordinate system.
Get/set the affine, non scaled 2D transformation matrix (2x3, float64, [2x2 Rot, 2x1 trans])
Get type of shape, e.g. Shape.Line, Shape.Point, Shape.Rectangle, Shape.Ellipse, Shape.Circle, Shape.Square
Return True if shape is a valid geometric shape, else False
Get/set width of shape.
A width can only be set for a square or for a rectangle and is defined with respect to the base coordinate system.