geometry
2D geometry primitives and implementation of some geometric algorithms.
- class backpack.geometry.Point(x, y)
Bases:
objectA point on the 2D plane.
- static ccw(pt1, pt2, pt3)
Determines if the three points form a counterclockwise angle. If two points are equal, or the three points are collinear, this method returns True.
- distance(other)
Calculates the distance between this and an other point.
- classmethod from_value(value)
Deserializes a Point from different formats.
Supported formats:
sequence containing exactly two numbers
dictionary containing numbers under ‘x’ and ‘y’ keys
Point instance (returns the same instance)
- Parameters
value – the value to be converted
Return: The new Point instance
Raises: ValueError if the conversion was not successful.
- class backpack.geometry.Line(pt1, pt2)
Bases:
objectA line segment.
- Parameters
- intersects(other)
Determines if this line segment intersects an other one.
- classmethod from_value(value)
Deserializes a Line from different formats.
Supported formats:
sequence containing exactly two values that can be deserialized with Point.from_value
dictionary containing such Point values under ‘pt1’ and ‘pt2’ keys
Line instance (returns the same instance)
- Parameters
value – the value to be converted
Returns: the Line instance
Raises: ValueError if the value could not be converted to a Line.
- class backpack.geometry.Rectangle(pt1, pt2)
Bases:
objectAn axis aligned rectangle.
- Parameters
- has_inside(pt)
Determines if a point is inside this rectangle.
- property tlbr: Tuple[float, float, float, float]
Returns this rectangles coordinates as a top-left-bottom-right tuple.
- property tlhw: Tuple[float, float, float, float]
Returns this rectangles coordinates as a top-left-width-height tuple.
- classmethod from_value(value)
Converts a tuple in the form of ((0.1, 0.2), (0.3, 0.4)) to a Rectangle.
- Parameters
value – the tuple
Returns: the Rectangle instance
Raises: ValueError if the tuple could not be converted to a Rectangle.
- classmethod from_tlbr(tlbr)
Converts a top-left-bottom-right sequence of floats to a Rectangle.
- Parameters
tlbr (Tuple[float, float, float, float]) – A top-left-bottom-right sequence of floats.
- Return type
Returns: the Rectangle instance
Raises: ValueError if the sequence could not be converted to a Rectangle.
- classmethod from_tlhw(tlhw)
Converts a top-left-width-height sequence of floats to a Rectangle.
- Parameters
- Return type
Returns: the Rectangle instance
Raises: ValueError if the sequence could not be converted to a Rectangle.
- class backpack.geometry.PolyLine(points, closed=True)
Bases:
objectA
PolyLineis a connected series of line segments.- Parameters
- property is_convex: bool
Determines if the polygon formed from this
PolyLineis convex.The result of this method is undefined for complex (self-intersecting) polygons.
- Returns
True if the polygon is convex, False otherwise.
- has_inside(point)
Determines if a point is inside this closed
PolyLine.This implementation uses the ray casting algorithm.
- classmethod from_value(value, closed=True)
Converts a tuple in the form of ((0.1, 0.2), (0.3, 0.4), …) to a PolyLine.
- Parameters
value – the tuple
closed – flags if the newly created PolyLine should be closed or not.
Returns: the PolyLine instance
Raises: ValueError if the tuple could not be converted to a PolyLine.