How to draw a polygon from a set of unordered points

前端 未结 3 459
囚心锁ツ
囚心锁ツ 2021-01-01 01:19

Currently, I am using a convex hull algorithm to get the outer most points from a set of points randomly placed. What I aim to do is draw a polygon from the set of points re

3条回答
  •  春和景丽
    2021-01-01 02:03

    This is not a full solution but a guide in the right direction. I faced a very similar problem just recently and I found a reddit post with an answer (https://www.reddit.com/r/DnDBehindTheScreen/comments/8efeta/a_random_star_chart_generator/dxvlsyt/) suggesting to use Delaunay triangulation which basically returns a solution with all possible triangles made within the data points you have. Once you have all possible triangles, which by definition you know won't result on any overlapped lines, you can chose which lines you use which result on all nodes being connected.

    I was coding my solution on python and fortunately there's lots of scientific libraries on python. I was working on a random sky chart generator which would draw constellations out of those stars. In order to get all possible triangles (and draw them, just for fun), before going into the algorithm to draw the actual constellations, all I had to do was this:

        # 2D array of the coordinates of every star generated randomly before
        points = list(points_dict.keys())
            
        from scipy.spatial import Delaunay
        tri = Delaunay(points)
        
        # Draw the debug constellation with the full array of lines
        debug_constellation = Constellation(quadrants = quadrants, name_display_style = config.constellation_name_display_style)
        for star in available_stars:
            debug_constellation.add_star(star)
        for triangle in tri.simplices:
            star_ids = []
            for index in triangle:
                star_ids.append(points_dict[points[index]].id)
            debug_constellation.draw_segment(star_ids, is_closed = True)
    
        # Code to generate the image follows below
    

    You can see the full implementation here: fake_sky_chart_generator/fake_libs/constellation_algorithms/delaunay.py

    This is the result:

提交回复
热议问题