ladybug_geometry.geometry2d.polygon module

2D Polygon

class ladybug_geometry.geometry2d.polygon.Polygon2D(vertices)[source]

Bases: Base2DIn2D

2D polygon object.

Parameters

vertices – A list of Point2D objects representing the vertices of the polygon.

Properties:
  • vertices

  • segments

  • inside_angles

  • outside_angles

  • min

  • max

  • center

  • perimeter

  • area

  • is_clockwise

  • is_convex

  • is_self_intersecting

  • self_intersection_points

  • is_valid

ToString()

Overwrite .NET ToString.

boolean_difference(polygon, tolerance)[source]

Get a list of Polygon2D for the subtraction of another polygon from this one.

Parameters
  • polygon – A Polygon2D for which the difference with the current polygon will be computed.

  • tolerance – The minimum distance between points before they are considered distinct from one another.

Returns

A list of Polygon2D representing the difference of the two polygons. Will be an empty list if subtracting the polygons results in the complete elimination of this polygon. Will be the original polygon when there is no overlap between the polygons.

boolean_intersect(polygon, tolerance)[source]

Get a list of Polygon2D for the intersection of this Polygon and another.

Parameters
  • polygon – A Polygon2D for which the intersection with the current polygon will be computed.

  • tolerance – The minimum distance between points before they are considered distinct from one another.

Returns

A list of Polygon2D representing the intersection of the two polygons. Will be an empty list if no overlap exists between the polygons.

static boolean_intersect_all(polygons, tolerance)[source]

Get a list of Polygon2D for the intersection of several Polygon2D.

Using this method is more computationally efficient than calling the Polygon2D.boolean_intersect() method multiple times as this method will only compute the intersection of the segments once.

Note that the result will not differentiate hole polygons from boundary polygons and so it may be desirable to use the Polygon2D.is_polygon_inside method to distinguish whether a given polygon in the result represents a hole in another polygon in the result.

Also note that this method will return the original polygons when there is no overlap in the two.

Parameters
  • polygons – An array of Polygon2D for which the intersection will be computed.

  • tolerance – The minimum distance between points before they are considered distinct from one another.

Returns

A list of Polygon2D representing the intersection of all the polygons. Will be an empty list if no overlap exists between the polygons.

static boolean_split(polygon1, polygon2, tolerance)[source]

Split two Polygon2D with one another to get the intersection and difference.

Using this method is more computationally efficient than calling the Polygon2D.intersect() and Polygon2D.difference() methods individually as this method will only compute the intersection of the segments once.

Note that the result will not differentiate hole polygons from boundary polygons and so it may be desirable to use the Polygon2D.is_polygon_inside method to distinguish whether a given polygon in the result represents a hole in another polygon in the result.

Parameters
  • polygon1 – A Polygon2D for the first polygon that will be split with the second polygon.

  • polygon2 – A Polygon2D for the second polygon that will be split with the first polygon.

  • tolerance – The minimum distance between points before they are considered distinct from one another.

Returns

A tuple with three elements

  • intersection: A list of Polygon2D for the intersection of the two input polygons.

  • poly1_difference: A list of Polygon2D for the portion of polygon1 that does not overlap with polygon2. When combined with the intersection, this makes a split version of polygon1.

  • poly2_difference: A list of Polygon2D for the portion of polygon2 that does not overlap with polygon1. When combined with the intersection, this makes a split version of polygon2.

boolean_union(polygon, tolerance)[source]

Get a list of Polygon2D for the union of this Polygon and another.

Note that the result will not differentiate hole polygons from boundary polygons and so it may be desirable to use the Polygon2D.is_polygon_inside method to distinguish whether a given polygon in the result represents a hole in another polygon in the result.

Also note that this method will return the original polygons when there is no overlap in the two.

Parameters
  • polygon – A Polygon2D for which the union with the current polygon will be computed.

  • tolerance – The minimum distance between points before they are considered distinct from one another.

Returns

A list of Polygon2D representing the union of the two polygons.

static boolean_union_all(polygons, tolerance)[source]

Get a list of Polygon2D for the union of several Polygon2D.

Using this method is more computationally efficient than calling the Polygon2D.boolean_union() method multiple times as this method will only compute the intersection of the segments once.

Note that the result will not differentiate hole polygons from boundary polygons and so it may be desirable to use the Polygon2D.is_polygon_inside method to distinguish whether a given polygon in the result represents a hole in another polygon in the result.

Also note that this method will return the original polygons when there is no overlap in the two.

Parameters
  • polygons – An array of Polygon2D for which the union will be computed.

  • tolerance – The minimum distance between points before they are considered distinct from one another.

Returns

A list of Polygon2D representing the union of all the polygons.

boolean_xor(polygon, tolerance)[source]

Get Polygon2D list for the exclusive disjunction of this polygon and another.

Note that this method is prone to merging holes that may exist in the result into the boundary to create a single list of joined vertices, which may not always be desirable. In this case, it may be desirable to do two separate boolean_difference calculations instead.

Also note that, when the result includes separate polygons for holes, it will not differentiate hole polygons from boundary polygons and so it may be desirable to use the Polygon2D.is_polygon_inside method to distinguish whether a given polygon in the result represents a hole within another polygon in the result.

Parameters
  • polygon – A Polygon2D for which the exclusive disjunction with the current polygon will be computed.

  • tolerance – The minimum distance between points before they are considered distinct from one another.

Returns

A list of Polygon2D representing the exclusive disjunction of the two polygons. Will be the original polygons when there is no overlap in the two.

static common_axes(polygons, direction, min_distance, merge_distance, angle_tolerance)[source]

Get LineSegment2Ds for the most common axes across a set of Polygon2Ds.

This is often useful as a step before aligning a set of polygons to these common axes.

Parameters
  • polygons – A list or tuple of Polygon2D objects for which common axes will be evaluated.

  • direction – A Vector2D object to represent the direction in which the common axes will be evaluated and generated

  • min_distance – The minimum distance at which common axes will be evaluated. This value should typically be a little larger than the model tolerance (eg. 5 to 20 times the tolerance) in order to ensure that possible common axes across the input polygons are not missed.

  • merge_distance – The distance at which common axes next to one another will be merged into a single axis. This should typically be 2-3 times the min_distance in order to avoid generating several axes that are immediately adjacent to one another. When using this method to generate axes for alignment, this merge_distance should be in the range of the alignment distance.

  • angle_tolerance – The max angle difference in radians that the polygon segments direction can differ from the input direction before the segments are not factored into this calculation of common axes.

  • Returns – A tuple with two elements.

  • common_axes (-) – A list of LineSegment2D objects for the common axes across the input polygons.

  • axis_values (-) – A list of integers that aligns with the common_axes and denotes how many segments of the input polygons each axis relates to. Higher numbers indicate that that the axis is more common among all of the possible axes.

distance_from_edge_to_point(point)[source]

Get the minimum distance between the edge of this shape and the input point.

Parameters

point – A Point2D object to which the minimum distance will be computed.

Returns

The distance to the input point. Will be zero if the point is inside the Polygon2D.

distance_to_point(point)[source]

Get the minimum distance between this shape and a point.

Points that are inside the Polygon2D will return a distance of zero. If the distance of an interior point to an edge is needed, the distance_from_edge_to_point method should be used.

Parameters

point – A Point2D object to which the minimum distance will be computed.

Returns

The distance to the input point. Will be zero if the point is inside the Polygon2D.

does_polygon_touch(polygon, tolerance)[source]

Test whether another Polygon2D touches, overlaps or is inside this polygon.

Parameters
  • polygon – A Polygon2D to test whether it touches this polygon.

  • tolerance – The minimum distance from an edge at which a point is considered to touch the edge.

Returns

A boolean denoting whether the polygon touches (True) or not (False).

duplicate()

Get a copy of this object.

classmethod from_array(point_array)[source]

Create a Polygon2D from a nested array of vertex coordinates.

Parameters

point_array – nested array of point arrays.

classmethod from_dict(data)[source]

Create a Polygon2D from a dictionary.

Parameters

data – A python dictionary in the following format

{
    "type": "Polygon2D",
    "vertices": [(0, 0), (10, 0), (0, 10)]
}
classmethod from_rectangle(base_point, height_vector, base, height)[source]

Initialize Polygon2D from rectangle parameters.

Initializing a polygon this way has the added benefit of having its properties quickly computed.

Parameters
  • base_point – A Point2D for the lower left vertex of the polygon.

  • height_vector – A vector denoting the direction of the rectangle height.

  • base – A number indicating the length of the base of the rectangle.

  • height – A number indicating the length of the height of the rectangle.

classmethod from_regular_polygon(number_of_sides, radius=1, base_point=Point2D(0.0, 0.0))[source]

Initialize Polygon2D from regular polygon parameters.

Parameters
  • number_of_sides – An integer for the number of sides on the regular polygon. This number must be greater than 2.

  • radius – A number indicating the distance from the polygon’s center where the vertices of the polygon will lie. The default is set to 1.

  • base_point – A Point2D for the center of the regular polygon. The default is the Origin at (0, 0).

classmethod from_shape_with_hole(boundary, hole)[source]

Initialize a Polygon2D from a boundary shape with a hole inside of it.

This method will convert the shape into a single concave polygon by drawing a line from the hole to the outer boundary.

Parameters
  • boundary – A list of Point2D objects for the outer boundary of the polygon inside of which the hole is contained.

  • hole – A list of Point2D objects for the hole.

classmethod from_shape_with_holes(boundary, holes)[source]

Initialize a Polygon2D from a boundary shape with holes inside of it.

This method will convert the shape into a single concave polygon by drawing lines from the holes to the outer boundary.

Parameters
  • boundary – A list of Point2D objects for the outer boundary of the polygon inside of which all of the holes are contained.

  • holes – A list of lists with one list for each hole in the shape. Each hole should be a list of at least 3 Point2D objects.

classmethod from_shape_with_holes_fast(boundary, holes)[source]

Initialize a Polygon2D from a boundary shape with holes using a fast method.

This method is similar in principle to the from_shape_with_holes method but it uses David Eberly’s algorithm for finding a bridge between the holes and outer polygon. This is extremely fast in comparison to the methods used by from_shape_with_holes but is not always the prettiest or the shortest pathway through the holes. Granted, it is very practical for shapes with lots of holes (eg. 100 holes) and will run in a fraction of the time for this case.

Parameters
  • boundary – A list of Point2D objects for the outer boundary of the polygon inside of which all of the holes are contained.

  • holes – A list of lists with one list for each hole in the shape. Each hole should be a list of at least 3 Point2D objects.

static gap_crossing_boundary(polygons, min_separation, tolerance)[source]

Get the boundary around several Polygon2D, crossing gaps of min_separation.

This method is less reliable than the joined_intersected_boundary because higher values of min_separation that are greater than the lengths of polygon segments can cause important details of the polygons to disappear. However, when used appropriately, it can provide a boundary that jumps across gaps to give resulting polygons that effectively bound all of the input polygons.

Parameters
  • polygons – The polygons to be joined into a boundary. These polygons should have colinear vertices removed and they should not contain degenerate polygons at the tolerance. The remove_colinear_vertices method can be used to pre-process the input polygons to ensure they meet these criteria.

  • min_separation – A number for the minimum distance between Polygon2D that is considered a meaningful separation. In other words, this is the maximum distance of the gap across

  • tolerance – The maximum difference between coordinate values of two vertices at which they can be considered equivalent.

Returns

A list of Polygon2D that represent the boundary around the input polygons. Note that some of these Polygon2D may represent ‘holes’ within others and it may be necessary to assess this when interpreting the result.

static group_by_overlap(polygons, tolerance)[source]

Group Polygon2Ds that overlap one another within the tolerance.

This is useful as a pre-step before running Polygon2D.boolean_union_all() in order to assess whether unionizing is necessary and to ensure that it is only performed among the necessary groups of polygons.

This method will return the minimal number of overlapping polygon groups thanks to a recursive check of whether groups can be merged.

Parameters
  • polygons – A list of Polygon2D to be grouped by their overlapping.

  • tolerance – The minimum distance from the edge of a neighboring polygon at which a point is considered to overlap with that polygon.

Returns

A list of lists where each sub-list represents a group of polygons that all overlap with one another.

static group_by_touching(polygons, tolerance)[source]

Group Polygon2Ds that touch or overlap one another within the tolerance.

This is useful to group geometries together before extracting a bounding rectangle or convex hull around multiple polygons.

This method will return the minimal number of polygon groups thanks to a recursive check of whether groups can be merged.

Parameters
  • polygons – A list of Polygon2D to be grouped by their touching.

  • tolerance – The minimum distance from the edge of a neighboring polygon at which a point is considered to touch that polygon.

Returns

A list of lists where each sub-list represents a group of polygons that all touch or overlap with one another.

intersect_line_infinite(ray)[source]

Get the intersections between this polygon and a Ray2D extended infinitely.

Parameters

ray – A Ray2D or to intersect. This will be extended in both directions infinitely for the intersection.

Returns

A list with Point2D objects for the intersections. List will be empty if no intersection exists.

intersect_line_ray(line_ray)[source]

Get the intersections between this polygon and a Ray2D or LineSegment2D.

Parameters

line_ray – A LineSegment2D or Ray2D or to intersect.

Returns

A list with Point2D objects for the intersections. List will be empty if no intersection exists.

static intersect_polygon_segments(polygon_list, tolerance)[source]

Intersect the line segments of a Polygon2D array to ensure matching segments.

Specifically, this method checks a list of polygons in a pairwise manner to see if one contains a vertex along an edge segment of the other within the given tolerance. If so, the method creates a co-located vertex at that point, partitioning the edge segment into two edge segments. Point ordering is reserved within each Polygon2D and the order of Polygon2Ds within the input polygon_list is also preserved.

Parameters
  • polygon_list – List of Polygon2Ds which will have their segments intersected with one another.

  • tolerance – Distance within which two points are considered to be co-located.

Returns

The input list of Polygon2D objects with extra vertices inserted

where necessary.

static intersect_segments(polygon1, polygon2, tolerance)[source]

Intersect the line segments of two Polygon2Ds to ensure matching segments.

Specifically, this method checks two adjacent polygons to see if one contains a vertex along an edge segment of the other within the given tolerance. If so, it creates a co-located vertex at that point, partitioning the edge segment into two edge segments. Point ordering is preserved.

Parameters
  • polygon1 – First polygon to check.

  • polygon2 – Second polygon to check.

  • tolerance – Distance within which two points are considered to be co-located.

Returns

Two polygon objects with extra vertices inserted if necessary.

is_equivalent(other, tolerance)[source]

Boolean for equivalence between this polygon and another (within tolerance).

The order of the polygon vertices do not have to start from the same vertex for equivalence to be true, but must be in the same counterclockwise or clockwise order.

Parameters
  • other – Polygon2D for comparison.

  • tolerance – float representing point equivalence.

Returns

True if equivalent else False

is_point_inside(point, test_vector=Vector2D(1.0, 0.0))[source]

Test whether a Point2D lies inside or outside the polygon.

This method is the fastest way to tell if a point is inside a polygon when the given point lies inside the boundary rectangle of the polygon. However, while this method gives the correct result in 99.9% of cases, there are a few fringe cases where it will not give the correct result. Specifically these are:

1 - When the test_ray intersects perfectly with a polygon vertex.
    For example, this case with an X-unit (1, 0) test_vector:
                        _____________
                       |      .      |
                       |            /
                       |___________/
2 - When there are two polygon vertices that are colinear with the point
    along the test_ray. For example, this case with an X-unit test_vector:
                          _____
                         |  .  |____
                         |__________|

Use the is_point_inside_check method if a result that covers these fringe cases is needed. Oftentimes, it is more practical to use a test_vector with a low probability of encountering the fringe cases than to use the (much longer) is_point_inside_check method.

Parameters
  • point – A Point2D for which the inside/outside relationship will be tested.

  • test_vector – Optional vector to set the direction in which intersections with the polygon edges will be evaluated to determine if the point is inside. Default is a slight variation of the X-unit vector with a low probability of encountering the unsupported fringe cases.

Returns

A boolean denoting whether the point lies inside (True) or outside (False).

is_point_inside_bound_rect(point, test_vector=Vector2D(1.0, 0.0))[source]

Test whether a Point2D lies roughly inside or outside the polygon.

This function is virtually identical to the is_point_inside method but will first do a check to see if the point lies inside the polygon bounding rectangle. As such, it is a faster approach when one expects many of tested points to lie far away from the polygon.

Parameters
  • point – A Point2D for which the inside/ outside relationship will be tested.

  • test_vector – Optional vector to set the direction in which intersections with the polygon edges will be evaluated to determine if the point is inside. Default is the X unit vector.

Returns

A boolean denoting whether the point lies inside (True) or outside (False).

is_point_inside_check(point)[source]

Test whether a Point2D lies inside the polygon with checks for fringe cases.

This method uses the same calculation as the the is_point_inside method but it includes additional checks for the fringe cases noted in the is_point_inside description. Using this method means that it will always yield the right result for all convex polygons and concave polygons with one concave turn (provided that they do not have colinear vertices). This is suitable for nearly all practical purposes and the only cases that could yield an incorrect result are when a point is co-linear with two or more polygon edges along the X vector like so:

 _____     _____     _____
|  .  |___|     |___|     |
|_________________________|

While this method covers most fringe cases, it will not test for whether a point lies perfectly on the edge of the polygon so it assesses whether a point lies inside the polygon up to Python floating point tolerance (16 digits). If distinguishing edge conditions from inside/ outside is important, the point_relationship method should be used.

Parameters

point – A Point2D for which the inside/ outside relationship will be tested.

Returns

A boolean denoting whether the point lies inside (True) or outside (False).

is_point_on_edge(point, tolerance)[source]

Test whether a Point2D lies on the boundary edges of the polygon.

Parameters
  • point – A Point2D for which the edge relationship will be tested.

  • tolerance – The minimum distance from the edge at which a point is considered to lie on the edge.

Returns

A boolean denoting whether the point lies on the polygon edges (True) or not on the edges (False).

is_polygon_inside(polygon)[source]

Test whether another Polygon2D lies completely inside this polygon.

Parameters

polygon – A Polygon2D to test whether it is completely inside this one.

Returns

A boolean denoting whether the polygon lies inside (True) or not (False).

is_polygon_outside(polygon)[source]

Test whether another Polygon2D lies completely outside this polygon.

Parameters

polygon – A Polygon2D to test whether it is completely outside this one.

Returns

A boolean denoting whether the polygon lies outside (True) or not (False).

is_rectangle(angle_tolerance)[source]

Test whether this Polygon2D is a rectangle given an angle tolerance.

Note that this method will return False if the Polygon2D does not have four vertices, even if they are duplicated or colinear.

Parameters

angle_tolerance – The max angle in radians that the corners of the rectangle can differ from a right angle before it is not considered a rectangle.

static joined_intersected_boundary(polygons, tolerance)[source]

Get the boundary around several Polygon2D that are touching one another.

This method is faster and more reliable than the gap_crossing_boundary but requires that the Polygon2D be touching one another within the tolerance.

Parameters
  • polygons – The polygons to be joined into a boundary. These polygons should have colinear vertices removed and they should not contain degenerate polygons at the tolerance. The remove_colinear_vertices method can be used to pre-process the input polygons to ensure they meet these criteria.

  • tolerance – The tolerance at which the polygons are to be intersected and then joined to give a resulting boundary.

Returns

A list of Polygon2D that represent the boundary around the input polygons. Note that some of these Polygon2D may represent ‘holes’ within others and it may be necessary to assess this when interpreting the result.

move(moving_vec)[source]

Get a polygon that has been moved along a vector.

Parameters

moving_vec – A Vector2D with the direction and distance to move the polygon.

offset(distance, check_intersection=False)[source]

Offset the polygon by a given distance inwards or outwards.

Note that the resulting shape may be self-intersecting if the distance is large enough and the is_self_intersecting property may be used to identify these shapes.

Parameters
  • distance – The distance inwards that the polygon will be offset. Positive values will always be offset inwards while negative ones will be offset outwards.

  • check_intersection – A boolean to note whether the resulting operation should be checked for self intersection and, if so, None will be returned instead of the self-intersecting polygon.

static overlapping_bounding_rect(polygon1, polygon2, tolerance)[source]

Check if the bounding rectangles of two polygons overlap within a tolerance.

This is particularly useful as a check before performing computationally intense processes between two polygons like intersection or boolean operations. Checking the overlap of the bounding boxes is extremely quick with this method’s use of the the Separating Axis Theorem.

Parameters
  • polygon1 – The first polygon to check.

  • polygon2 – The second polygon to check.

  • tolerance – Distance within which two points are considered to be co-located.

static perimeter_core_by_offset(polygon, distance, holes=None)[source]

Compute perimeter and core sub-polygons using a simple offset method.

This method will only return polygons when the distance is shallow enough that the perimeter offset does not intersect itself or turn inward on itself. Otherwise, the method will simple return None. This means that there will only ever be one core polygon.

Parameters
  • polygon – A Polygon2D to split into perimeter and core sub-polygons.

  • distance – Distance in model units to offset perimeter sub-polygon.

  • holes – A list of Polygon2D objects representing holes in the polygon. (Default: None).

Returns

A tuple with two items.

  • perimeter_sub_polys – A list of perimeter sub-polygons as Polygon2D

    objects. Will be None if the offset distance is too deep.

  • core_sub_polys – A list of core sub-polygons as Polygon2D objects. In the

    event of a core sub-polygon with a hole, a list with be returned with the first item being a boundary and successive items as hole polygons. Will be None if the offset distance is too deep.

point_relationship(point, tolerance)[source]

Test whether a Point2D lies inside, outside or on the boundary of the polygon.

Compared to other methods like is_point_inside this method is slow. However, it covers all edge cases, including the literal edge of the polygon.

Parameters
  • point – A Point2D for which the relationship to the polygon will be tested.

  • tolerance – The minimum distance from the edge at which a point is considered to lie on the edge.

Returns

An integer denoting the relationship of the point.

This will be one of the following:

  • -1 = Outside polygon

  • 0 = On the edge of the polygon

  • +1 = Inside polygon

pole_of_inaccessibility(tolerance)[source]

Get the pole of inaccessibility for the polygon.

The pole of inaccessibility is the most distant internal point from the polygon outline. It is not to be confused with the centroid, which represents the “center of mass” of the polygon and may be outside of the polygon if the shape is concave. The poly of inaccessibility is useful for optimal placement of a text label on a polygon.

The algorithm here is a port of the polylabel library from MapBox assembled by Michal Hatak. (https://github.com/Twista/python-polylabel).

Parameters

tolerance – The precision to which the pole of inaccessibility will be computed.

polygon_relationship(polygon, tolerance)[source]

Test whether another Polygon2D lies inside, outside or overlaps this one.

This method is not usually the fastest for understanding the relationship between polygons but it accurately accounts for tolerance such that the case of the two polygons sharing edges will not determine the outcome. Only when the polygon has vertices that are truly outside this polygon within the tolerance will the relationship become outside (or intersecting if one of the vertices is already inside within the tolerance).

In the case of the input polygon being identical to the current polygon, the relationship will be Inside.

Parameters
  • polygon – A Polygon2D for which the relationship to the current polygon will be tested.

  • tolerance – The minimum distance from the edge at which a point is considered to lie on the edge.

Returns

An integer denoting the relationship of the polygon.

This will be one of the following:

  • -1 = Outside this polygon

  • 0 = Overlaps (intersects) this polygon

  • +1 = Inside this polygon

rectangular_approximation()[source]

Get a rectangular Polygon2D with the same area and aspect ratio as this one.

This is useful when an interface requires a rectangular input but the user-defined geometry can be any shape. The resulting rectangle will share the same center as this one.

reflect(normal, origin)[source]

Get a polygon reflected across a plane with the input normal and origin.

Parameters
  • normal – A Vector2D representing the normal vector for the plane across which the polygon will be reflected. THIS VECTOR MUST BE NORMALIZED.

  • origin – A Point2D representing the origin from which to reflect.

remove_colinear_vertices(tolerance)[source]

Get a version of this polygon without colinear or duplicate vertices.

Parameters

tolerance – The minimum distance that a vertex can be from a line before it is considered colinear.

remove_duplicate_vertices(tolerance)[source]

Get a version of this polygon without duplicate vertices.

Parameters

tolerance – The minimum distance between a two vertices at which they are considered co-located or duplicated.

reverse()[source]

Get a copy of this polygon where the vertices are reversed.

rotate(angle, origin)[source]

Get a polygon that is rotated counterclockwise by a certain angle.

Parameters
  • angle – An angle for rotation in radians.

  • origin – A Point2D for the origin around which the point will be rotated.

scale(factor, origin=None)[source]

Scale a polygon by a factor from an origin point.

Parameters
  • factor – A number representing how much the polygon should be scaled.

  • origin – A Point2D representing the origin from which to scale. If None, it will be scaled from the World origin (0, 0).

static snap_polygons(polygons, tolerance)[source]

Snap several Polygon2D to each other if they differ less than the tolerance.

This is useful to run before performing operations where small tolerance differences are likely to cause issues, such as in boolean operations.

Parameters
  • polygons – A list of Polygon2D, which will be snapped to each other.

  • tolerance – The minimum distance at which points will be snapped.

Returns

A list of the input polygon2D that have been snapped to one another.

snap_to_grid(grid_increment)[source]

Snap this polygon’s vertices to the nearest grid node defined by an increment.

Parameters

grid_increment – A positive number for dimension of each grid cell. This typically should be equal to the tolerance or larger but should not be larger than the smallest detail of the polygon that you wish to resolve.

Returns

A version of this polygon that is snapped to the grid.

snap_to_polygon(polygon, tolerance)[source]

Snap another Polygon2D to this one for differences smaller than the tolerance.

This is useful to run before performing operations where small tolerance differences are likely to cause issues, such as in boolean operations.

Parameters
  • polygon – A Polygon2D which will be snapped to the current polygon.

  • tolerance – The minimum distance at which points will be snapped.

Returns

A version of the polygon that is snapped to this Polygon2D.

to_array()[source]

Get a list of lists where each sub-list represents a Point2D vertex.

to_dict()[source]

Get Polygon2D as a dictionary.

property area

The area of the polygon.

property center

A Point2D for the center of the bounding rectangle around this geometry.

property inside_angles

Tuple of angles in radians for the interior angles of the polygon.

These are aligned with the vertices such that the first angle corresponds to the inside angle at the first vertex, the second at the second vertex, and so on.

property is_clockwise

Boolean for whether the polygon vertices are in clockwise order.

property is_convex

Boolean noting whether the polygon is convex (True) or non-convex (False).

property is_self_intersecting

Boolean noting whether the polygon has self-intersecting edges.

Note that this property is relatively computationally intense to obtain compared to properties like area and is_convex. Also, most CAD programs forbid geometry with self-intersecting edges. So it is recommended that this property only be used in quality control scripts where the origin of the geometry is unknown.

property is_valid

Boolean noting whether the polygon is valid (having a non-zero area).

Note that polygons are still considered valid if they have self-intersecting edges, or duplicate/colinear vertices. The s_self_intersecting property identifies self-intersecting edges, and the remove_colinear_vertices method will remove duplicate/colinear vertices.

property max

A Point2D for the maximum bounding rectangle vertex around this geometry.

property min

A Point2D for the minimum bounding rectangle vertex around this geometry.

property outside_angles

Tuple of angles in radians for the exterior angles of the polygon.

These are aligned with the vertices such that the first angle corresponds to the inside angle at the first vertex, the second at the second vertex, and so on.

property perimeter

The perimeter of the polygon.

property segments

Tuple of all line segments in the polygon.

property self_intersection_points

A tuple of Point2Ds for the locations where the polygon intersects itself.

This will be an empty tuple if the polygon is not self-intersecting and it is generally recommended that the Polygon2D.is_self_intersecting property be checked before using this property.

property vertices

Tuple of all vertices in this geometry.