Order orthogonal polygon python

Binh Lee

How can I order a list of orthogonal polygon points?

For example, I have a list of orthorgonal polygon points

data = [(2, 0), (5, 0), (5, 7), (4, 7), (4, 5), (3, 5),(3, 3), (2, 3), (2, 2), (3, 2), (3, 7), (2, 7)]

Not in order. I want to order it in a counter-clockwise way like this:

out = [(2,0),(5,0),(5,7),(4,7),(4,5),(3,5),(3,7),(2,7),(2,3),(3,3),(3,2),(2,2)]

I had tried to use deflate _hull already but it did not correct. Is there any algorithm to solve this problem ?

I get this:

enter image description here

But expected :

enter image description here

blhsing

You can use the following recursive function:

def sort_ortho_poly(points, current=None, start=None, go_x=True):
    # initialize the starting point at the bottom left, which should have the least sum of x and y
    if not current:
        start = current = min(points, key=sum)
    # if we're going x-wards, v would be the y index (1), h would be the x index (0), and vice versa 
    v, h = go_x, not go_x
    # remove the current point from the list of points so the next recursion would be processing the remaining points
    remaining = points[:]
    remaining.remove(current)
    # if there is no more remaining point
    if not remaining:
        # we've found a path if we are able to connect back to the starting point, or else we don't
        return [current] if start[v] == current[v] else []
    # try each point in the remaining points that goes in the right direction from the current point
    for next in [p for p in remaining if p[v] == current[v]]:
        # recursively find a valid path from the remaining points after flipping the direction
        path = sort_ortho_poly(remaining, next, start, not go_x)
        # if we get a path that does go back to the starting point, we have to make sure the path is valid
        if path:
            # the current edge (e1, e2)
            e1, e2 = current, next
            # make sure e1 is lower than or left of e2
            if e1[h] > e2[h]:
                e1, e2 = e2, e1
            # for each edge (p1, p2) in the path, including the final edge connecting to the starting point
            for p1, p2 in zip(path, path[1:] + [start]):
                # make sure p1 is lower than or left of p2
                if p1[0] == p2[0] and p1[1] > p2[1] or p1[1] == p2[1] and p1[0] > p2[0]:
                    p1, p2 = p2, p1
                # if the edge is in the same line as the current edge
                if p1[v] == p2[v] == e1[v]:
                    # make sure the two edges don't overlap
                    if e1[h] < p1[h] < e2[h] or e1[h] < p2[h] < e2[h] or p1[h] < e1[h] < p2[h] or p1[h] < e2[h] < p2[h]:
                        break
                # if the edge is perpendicular to the current edge, make sure they don't cross over
                elif p1[h] == p2[h] and e1[h] < p1[h] < e2[h] and p1[v] < e1[v] < p2[v]:
                    break
            else:
                # the path is valid! we append the path to the current point and return
                return [current, *path]
    # return empty if it's a dead end
    return []

so that:

data = [(2, 0), (5, 0), (5, 7), (4, 7), (4, 5), (3, 5),(3, 3), (2, 3), (2, 2), (3, 2), (3, 7), (2, 7)]
print(sort_ortho_poly(data))

would output:

[(2, 0), (5, 0), (5, 7), (4, 7), (4, 5), (3, 5), (3, 7), (2, 7), (2, 3), (3, 3), (3, 2), (2, 2)]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Question: Finding overlapping rectangles in an orthogonal Polygon (Java)

From Dev

Daubechies orthogonal wavelet in python

From Dev

Orthogonal distance regression in python: meaning of returned values

From Dev

Orthogonal distance regression in python: meaning of returned values

From Dev

How to detect if all the rows of a non-square matrix are orthogonal in python

From Dev

Python - drawing a polygon

From Dev

Point in Polygon with geoJSON in Python

From Dev

Can I set the order of points in a shapely Polygon?

From Dev

Detect corner coordinates of a non-convex polygon in clockwise order MATLAB

From Dev

How to judge that the vertices's order of polygon is clockwise or counterclockwise?

From Dev

generating two orthogonal vectors that are orthogonal to a particular direction

From Dev

Polygon crop/clip using Python / PIL

From Dev

Python - area of irregular polygon results in negative value?

From Dev

Using python to find points in a user designated polygon

From Dev

Python/Matplotlib Inverse Fill of Polar Plot Polygon

From Dev

How do I draw a polygon in python?

From Dev

Polygon crop/clip using Python / PIL

From Dev

Python/Matplotlib Inverse Fill of Polar Plot Polygon

From Dev

Python Work out area of a polygon on a spherical surface

From Dev

Producing polygon offsets using Clipper lib in python

From Dev

Python PIL: Make pixels outside a polygon transparent

From Dev

Orthogonal and Combinatorial testing techniques

From Dev

orthogonal projection with numpy

From Dev

Numerical evaluation of orthogonal polynomials

From Dev

Converting perspective projection to orthogonal projection

From Dev

Orthogonal Projection of Point onto Line

From Dev

Orthogonal Projection of Point onto Line

From Dev

zgeev giving eigenvectors which are not orthogonal

From Dev

how to order vertices in a non-convex polygon (how to find one of many solutions)

Related Related

  1. 1

    Question: Finding overlapping rectangles in an orthogonal Polygon (Java)

  2. 2

    Daubechies orthogonal wavelet in python

  3. 3

    Orthogonal distance regression in python: meaning of returned values

  4. 4

    Orthogonal distance regression in python: meaning of returned values

  5. 5

    How to detect if all the rows of a non-square matrix are orthogonal in python

  6. 6

    Python - drawing a polygon

  7. 7

    Point in Polygon with geoJSON in Python

  8. 8

    Can I set the order of points in a shapely Polygon?

  9. 9

    Detect corner coordinates of a non-convex polygon in clockwise order MATLAB

  10. 10

    How to judge that the vertices's order of polygon is clockwise or counterclockwise?

  11. 11

    generating two orthogonal vectors that are orthogonal to a particular direction

  12. 12

    Polygon crop/clip using Python / PIL

  13. 13

    Python - area of irregular polygon results in negative value?

  14. 14

    Using python to find points in a user designated polygon

  15. 15

    Python/Matplotlib Inverse Fill of Polar Plot Polygon

  16. 16

    How do I draw a polygon in python?

  17. 17

    Polygon crop/clip using Python / PIL

  18. 18

    Python/Matplotlib Inverse Fill of Polar Plot Polygon

  19. 19

    Python Work out area of a polygon on a spherical surface

  20. 20

    Producing polygon offsets using Clipper lib in python

  21. 21

    Python PIL: Make pixels outside a polygon transparent

  22. 22

    Orthogonal and Combinatorial testing techniques

  23. 23

    orthogonal projection with numpy

  24. 24

    Numerical evaluation of orthogonal polynomials

  25. 25

    Converting perspective projection to orthogonal projection

  26. 26

    Orthogonal Projection of Point onto Line

  27. 27

    Orthogonal Projection of Point onto Line

  28. 28

    zgeev giving eigenvectors which are not orthogonal

  29. 29

    how to order vertices in a non-convex polygon (how to find one of many solutions)

HotTag

Archive