Polygon module¶
-
class
Polygon.Polygon(vertices, reorder=False)¶ Bases:
objectThis class represents a polygon as a list of vertex points ordered in clockwise order.
- Attributes
- verticesList of Point
Store the vertices in a clockwise list
Methods
area()Finds the area of the polygon
cut(p1, p2)Cuts a polygon along two points
cut_gap(p1, p2, gap)Cuts a polygon along two points
cut_out(interior_polygon)Cuts a larger polygon into parts without an interior polygon part
cut_out_2(interior_polygon)Cuts out a shape from a larger polygon if the interior polygon is a scaled down version of the larger polygon
cut_out_gap_2(interior_polygon, scalar)Cuts out a shape from a larger polygon if the interior polygon is a scaled down version of the larger polygon with a gap
easy_cut(p, ang, gap)Cuts a polygon into two parts based on a point and an angle, with a gap for the width of the cut
find_ext_point(furthest_point, ang, initial_p)Find an extreme point given a furthest_point, an angle, and an initial point
find_intersecting_edge(p, ang)Given a line segment this method returns an edge that that line segment intersects
find_intersecting_edges(p, ext_p)Given a line segment this method returns edges that that line segment intersects, this is used because of an edge case in the find_intersecting_edges
Finds a point that has the furthest x value among all points in the polygon and has the furthest y value among all points in the polygon
furthest_point(point)Finds the furthest point in this polygon from a give point
Finds the centroid of the polygon.
Finds the perimeter of the polygon
Gets the vertices of the polygon
in_segment(p1, p2, p3)Returns true if p3 is in the line segment from p1 to p2.
inside(larger_polygon)Checks a given polygon to see if it is in a larger polygon
intersection(p1, p2, p3, p4)Finds the point of intersection given twp line segments
is_bordering(other)Checks to see if a given polygon borders this polygon.
is_contained(point)Returns true if Point point is located on the edge of or in this Polygon.
Returns true if the polygon is convex and has no intersecting lines.
Check a given polygon to see if it is a rectangle
Finds the longest side and length
Moves a polygon’s center to a given point p
move_polygon_by_center(left_right_distance, …)Moves a polygon’s center by left_right_distance left or right, and up_down_distance up or down.
on_edge(p)Checks to see if a given point p is on any edge of the polygon
rectangle_inside(p1, p2)Finds a rectangle given an edge of the polygon
scale_of_polygon(scalar)Scales a polygon by a scalar and returns a new scaled polygon centered on the original polygon’s center
set_vertices(vertices[, reorder])Sets the vertices of the polygon
split(p, ang, gap)splits a polygon along a point and an angle relative to edge of p
to_points(polygons)Takes a list of polygons and converts it to a list of points
to_polygon(vertices)Returns a polygon given a list of lists, i.e. [[1, 2], [3, 4], [5, 6]] => polygon with those vertices.
-
area()¶ Finds the area of the polygon
- Returns
- float
The area of the polygon
-
cut(p1, p2)¶ Cuts a polygon along two points
- Parameters
- p1Point
First Point
- p2Point
Second Point
- Returns
- List of Polygons
Two new polygons formed by the cut
-
cut_gap(p1, p2, gap)¶ Cuts a polygon along two points
- Parameters
- p1Point
First Point
- p2Point
Second Point
- gapfloat
width of the cut
- Returns
- List of Polygons
Two new polygons formed by the cut
-
cut_out(interior_polygon)¶ Cuts a larger polygon into parts without an interior polygon part
- Parameters
- interior_polygonPolygon
The polygon to cut out
- Returns
- list of Polygons
the parts of the original polygon without the interior polygon
-
cut_out_2(interior_polygon)¶ Cuts out a shape from a larger polygon if the interior polygon is a scaled down version of the larger polygon
- Parameters
- interior_polygonPolygon
the scaled down version of the larger polygon to cut out
- Returns
- list of Polygons
the cut out parts on the exterior of the larger polygon
-
cut_out_gap_2(interior_polygon, scalar)¶ Cuts out a shape from a larger polygon if the interior polygon is a scaled down version of the larger polygon with a gap
- Parameters
- interior_polygonPolygon
the scaled down version of the larger polygon to cut out
- scalarfloat
the scalar to put on the interior polygon to make the gap
- Returns
- list of Polygons
the cut out parts on the exterior of the larger polygon
-
easy_cut(p, ang, gap)¶ Cuts a polygon into two parts based on a point and an angle, with a gap for the width of the cut
- Parameters
- pPoint
The point to cut from
- angfloat
The angle to cut on (in radians)
- gapfloat
The cut width
- Returns
- list of Polygons
The two cut parts of the polygon
-
static
find_ext_point(furthest_point, ang, initial_p)¶ Find an extreme point given a furthest_point, an angle, and an initial point
- Parameters
- furthest_pointPoint
the furthest point for reference to find an extreme point
- angfloat
the angle towards the extreme point
- initial_pPoint
the initial point for the line towards the extreme point
- Returns
- Point
the extreme point
-
find_intersecting_edge(p, ang)¶ Given a line segment this method returns an edge that that line segment intersects
- Parameters
- ppoint
the first point of the line segment
- angfloat
the angle in radians that the ray from p extends from
- Returns
- list of Points
the edge on the polygon that the line segment crosses
-
find_intersecting_edges(p, ext_p)¶ Given a line segment this method returns edges that that line segment intersects, this is used because of an edge case in the find_intersecting_edges
- Parameters
- ppoint
the first point of the line segment
- ext_pPoint
the second point of the line segment
- Returns
- list of list of points
the edge on the polygon that the line segment crosses
-
furthest_conglomerate_point(p)¶ Finds a point that has the furthest x value among all points in the polygon and has the furthest y value among all points in the polygon
- Parameters
- pPoint
the point to find the furthest point from
-
furthest_point(point)¶ Finds the furthest point in this polygon from a give point
- Parameters
- pointPoint
The point to compare for every polygon vertices
- Returns
- Point
The furthest point from the given point in the polygon
-
get_center()¶ Finds the centroid of the polygon.
- Returns
- Point
Point representing the centroid of the polygon
Notes
We are using the general formula for a centroid, expressed below.
\[\begin{split}C_x = \frac{1}{6A} \sum_{i=0}^{n-1}\left(\left(x_i + x_{i+1}\right) \times \left(x_1 y_{i+1} - x_{i+1} y_i \right) \right) \\ C_y = \frac{1}{6A} \sum_{i=0}^{n-1}\left(\left(y_i + y_{i+1}\right) \times \left(x_1 y_{i+1} - x_{i+1} y_i \right) \right)\end{split}\]Where A is defined as the area using the shoelace method.
\[A = \frac{1}{2} \sum_{i=0}^{n-1}\left(x_i y_{i+1} - x_{i+1} y_i\right)\]More information can be found here.
-
get_perimeter()¶ Finds the perimeter of the polygon
- Returns
- float
The perimeter of the polygon
-
get_vertices()¶ Gets the vertices of the polygon
- Returns
- List of Points
The vertices
-
static
in_segment(p1, p2, p3)¶ Returns true if p3 is in the line segment from p1 to p2.
- Parameters
- p1Point
First point of the line segment
- p2Point
Second point of the line segment
- p3Point
The point to see if it is in the line segment
- Returns
- bool
True if p3 is in the line segment from p1 to p2
Notes
After checking for vertical and horizontal lines, we know that the p3 is only on the line segment between p1 and p2 if the distance from p1 to p2 thorough p3 is the same as the distance from p1 directly to p2.
This method is only accurate to approximately eight decimal places, after which internal rounding can affect the result. >>> print(Polygon.in_segment(Point(0, 0), Point(5, 5), Point(1, 1.00000001))) True
Example of an incorrect, but very close test.
-
inside(larger_polygon)¶ Checks a given polygon to see if it is in a larger polygon
- Parameters
- larger_polygonPolygon
The larger polygon that could contain the given polygon
- Returns
- bool
True if the given polygon is in larger_polygon
-
static
intersection(p1, p2, p3, p4)¶ Finds the point of intersection given twp line segments
- Parameters
- p1Point
The first point in the first line segment
- p2Point
The second point in the first line segment
- p3Point
The first point in the second line segment
- p4Point
The second point in the second line segment
- Returns
- Point
The intersection
-
is_bordering(other)¶ Checks to see if a given polygon borders this polygon.
- Parameters
- otherPolygon
The polygon we need to check for a border
- Returns
- bool
True if the two share and edge, false otherwise.
Notes
I have not been able to find a way to do this in less than \(O(n^2)\) complexity, which is acceptable so long as we do not compare every polygon to every other polygon in the city. That will tank performance.
This will not catch intersecting polygons unless one vertex sits on an edge of the other.
-
is_contained(point)¶ Returns true if Point point is located on the edge of or in this Polygon.
- Parameters
- pointPoint
The point to check if in this polygon
- Returns
- bool
True if contained in the polygon
Notes
This method utilises the Ray-casting algorithm. A horizontal ray from point increasing to infinity will intersect with the polygon an odd number of times if it is located inside the polygon and an even number of times if point is located outside the polygon.
You can find more information about the algorithm here: https://rosettacode.org/wiki/Ray-casting_algorithm
-
is_convex()¶ Returns true if the polygon is convex and has no intersecting lines.
- Returns
- bool
Returns true if the polygon is convex and has no intersecting lines, or false if not.
Notes
This method will return false if the number of vertices in the polygon is less than 3 or if any of the vertices repeat.
-
is_rectangle()¶ Check a given polygon to see if it is a rectangle
- Returns
- bool
True if the polygon is a rectangle
-
longest_side_and_length()¶ Finds the longest side and length
- Returns
- list
A list where the first element is the longest distance, the the next two elements are the points on the edge
-
move_center_to(p)¶ Moves a polygon’s center to a given point p
- Parameters
- pPoint
The new center for the polygon
-
move_polygon_by_center(left_right_distance, up_down_distance)¶ Moves a polygon’s center by left_right_distance left or right, and up_down_distance up or down.
- Parameters
- left_right_distancefloat
the distance to move the center of the polygon by left or right
- up_down_distancefloat
the distance to move the center of the polygon by up or down
-
on_edge(p)¶ Checks to see if a given point p is on any edge of the polygon
- Parameters
- pPoint
The point to check to see if it is on any edge of the polygon
- Returns
- bool
True if p is on any edge of the polygon
-
rectangle_inside(p1, p2)¶ Finds a rectangle given an edge of the polygon
- Parameters
- p1Point
one edge vertices
- p2Point
second edge vertices
- Returns
- Polygon
The rectangle inside the polygon
-
scale_of_polygon(scalar)¶ Scales a polygon by a scalar and returns a new scaled polygon centered on the original polygon’s center
- Parameters
- scalarfloat
The scalar to scale the polygon by (could be positive or negative for shrink or grow)
- Returns
- Polygon
The new scaled polygon centered on the original polygon’s center
-
set_vertices(vertices, reorder=False)¶ Sets the vertices of the polygon
- Parameters
- verticesList of points
The new vertices
- reorderbool
If ture, reorder the vertices into clockwise order
-
split(p, ang, gap)¶ splits a polygon along a point and an angle relative to edge of p
- Parameters
- pPoint
The starting point of the cut
- angfloat
The angle of the line to cut down in radians
- gapfloat
width of the cut between the split polygon
- Returns
- List of Polygons
A list of polygons from the resulting split
-
static
to_points(polygons)¶ Takes a list of polygons and converts it to a list of points
- Parameters
- polygonsList of Polygons
list of Polygons
- Returns
- List of Points
List of all vertices in the list of Polygons
-
static
to_polygon(vertices)¶ Returns a polygon given a list of lists, i.e. [[1, 2], [3, 4], [5, 6]] => polygon with those vertices
- Parameters
- verticeslist of lists
the vertices of the polygon in the form of a list of lists
-
Polygon.clockwise_order(vertices)¶ Given a list of vertices, return them ordered in clockwise order
- Parameters
- verticesList
The vertices of a polygon
- Returns
- List
The vertices ordered in clockwise order.
-
Polygon.print_list(list_name, points)¶