How to generate a random convex polygon?

前端 未结 6 1170
南旧
南旧 2021-02-05 11:19

I\'m trying to devise a method for generating random 2D convex polygons. It has to have the following properties:

  • coordinates should be integers;
  • the poly
相关标签:
6条回答
  • 2021-02-05 11:26

    I've made the ruby port as well thanks to both @Mangara's answer and @Azat's answer:

    #!/usr/bin/env ruby
    # frozen_string_literal: true
    
    module ValtrAlgorithm
      module_function def random_polygon(length)
        raise ArgumentError, "length should be > 2" unless length > 2
    
        min_x, *xs, max_x = Array.new(length) { rand }.sort
        min_y, *ys, max_y = Array.new(length) { rand }.sort
        # Divide the interior points into two chains and
        # extract the vector components.
        vec_xs = to_random_vectors(xs, min_x, max_x)
        vec_ys = to_random_vectors(ys, min_y, max_y).
          # Randomly pair up the X- and Y-components
          shuffle
        # Combine the paired up components into vectors
        vecs = vec_xs.zip(vec_ys).
          # Sort the vectors by angle, in a counter clockwise fashion. Remove the
          # `-` to make it clockwise.
          sort_by { |x, y| - Math.atan2(y, x) }
    
        # Lay them end-to-end
        point_x = point_y = 0
        min_polygon_x = min_polygon_y = 0
        points = []
        vecs.each do |vec_x, vec_y|
          points.append([vec_x, vec_y])
          point_x += vec_x
          point_y += vec_y
          min_polygon_x = [min_polygon_x, point_x].min
          min_polygon_y = [min_polygon_y, point_y].min
        end
        shift_x = min_x - min_polygon_x
        shift_y = min_y - min_polygon_y
        result = points.map { |point_x, point_y| [point_x + shift_x, point_y + shift_y] }
        # Append first point to make it a valid linear ring
        result << result.first
      end
    
      private def to_random_vectors(coordinates, min, max)
        last_min = last_max = min
        ary = []
        coordinates.each do |coordinate|
          if rand > 0.5
            ary << coordinate - last_min
            last_min = coordinate
          else
            ary << last_max - coordinate
            last_max = coordinate
          end
        end
        ary << max - last_min << last_max - max
      end
    end
    
    0 讨论(0)
  • 2021-02-05 11:29

    This isn't quite complete, but it may give you some ideas.

    Bail out if N < 3. Generate a unit circle with N vertices, and rotate it random [0..90] degrees.

    Randomly extrude each vertex outward from the origin, and use the sign of the cross product between each pair of adjacent vertices and the origin to determine convexity. This is the step where there are tradeoffs between speed and quality.

    After getting your vertices set up, find the vertex with the largest magnitude from the origin. Divide every vertex by that magnitude to normalize the polygon, and then scale it back up by (C/2). Translate to (C/2, C/2) and cast back to integer.

    0 讨论(0)
  • 2021-02-05 11:34

    Following @Mangara answer there is JAVA implementation, if someone is interested in Python port of it

    import random
    from math import atan2
    
    
    def to_convex_contour(vertices_count,
                          x_generator=random.random,
                          y_generator=random.random):
        """
        Port of Valtr algorithm by Sander Verdonschot.
    
        Reference:
            http://cglab.ca/~sander/misc/ConvexGeneration/ValtrAlgorithm.java
    
        >>> contour = to_convex_contour(20)
        >>> len(contour) == 20
        True
        """
        xs = [x_generator() for _ in range(vertices_count)]
        ys = [y_generator() for _ in range(vertices_count)]
        xs = sorted(xs)
        ys = sorted(ys)
        min_x, *xs, max_x = xs
        min_y, *ys, max_y = ys
        vectors_xs = _to_vectors_coordinates(xs, min_x, max_x)
        vectors_ys = _to_vectors_coordinates(ys, min_y, max_y)
        random.shuffle(vectors_ys)
    
        def to_vector_angle(vector):
            x, y = vector
            return atan2(y, x)
    
        vectors = sorted(zip(vectors_xs, vectors_ys),
                         key=to_vector_angle)
        point_x = point_y = 0
        min_polygon_x = min_polygon_y = 0
        points = []
        for vector_x, vector_y in vectors:
            points.append((point_x, point_y))
            point_x += vector_x
            point_y += vector_y
            min_polygon_x = min(min_polygon_x, point_x)
            min_polygon_y = min(min_polygon_y, point_y)
        shift_x, shift_y = min_x - min_polygon_x, min_y - min_polygon_y
        return [(point_x + shift_x, point_y + shift_y)
                for point_x, point_y in points]
    
    
    def _to_vectors_coordinates(coordinates, min_coordinate, max_coordinate):
        last_min = last_max = min_coordinate
        result = []
        for coordinate in coordinates:
            if _to_random_boolean():
                result.append(coordinate - last_min)
                last_min = coordinate
            else:
                result.append(last_max - coordinate)
                last_max = coordinate
        result.extend((max_coordinate - last_min,
                       last_max - max_coordinate))
        return result
    
    
    def _to_random_boolean():
        return random.getrandbits(1)
    
    0 讨论(0)
  • 2021-02-05 11:48

    Your initial approach is correct - calculating the convex hull is the only way you will satisfy randomness, convexity and integerness.

    The only way I can think of optimizing your algorithm to get "more points" out is by organizing them around a circle instead of completely randomly. Your points should more likely be near the "edges" of your square than near the center. At the center, the probability should be ~0, since the polygon must be convex.

    One simple option would be setting a minimum radius for your points to appear - maybe C/2 or C*0.75. Calculate the center of the C square, and if a point is too close, move it away from the center until a minimum distance is reached.

    0 讨论(0)
  • 2021-02-05 11:49

    A simple algorithm would be:

    1. Start with random line (a two vertices and two edges polygon)
    2. Take random edge E of the polygon
    3. Make new random point P on this edge
    4. Take a line L perpendicular to E going through point P. By calculating intersection between line T and lines defined by the two edges adjacent to E, calculate the maximum offset of P when the convexity is not broken.
    5. Offset the point P randomly in that range.
    6. If not enough points, repeat from 2.
    0 讨论(0)
  • 2021-02-05 11:50

    Here is the fastest algorithm I know that generates each convex polygon with equal probability. The output has exactly N vertices, and the running time is O(N log N), so it can generate even large polygons very quickly.

    • Generate two lists, X and Y, of N random integers between 0 and C. Make sure there are no duplicates.
    • Sort X and Y and store their maximum and minimum elements.
    • Randomly divide the other (not max or min) elements into two groups: X1 and X2, and Y1 and Y2.
    • Re-insert the minimum and maximum elements at the start and end of these lists (minX at the start of X1 and X2, maxX at the end, etc.).
    • Find the consecutive differences (X1[i + 1] - X1[i]), reversing the order for the second group (X2[i] - X2[i + 1]). Store these in lists XVec and YVec.
    • Randomize (shuffle) YVec and treat each pair XVec[i] and YVec[i] as a 2D vector.
    • Sort these vectors by angle and then lay them end-to-end to form a polygon.
    • Move the polygon to the original min and max coordinates.

    An animation and Java implementation is available here: Generating Random Convex Polygons.

    This algorithm is based on a paper by Pavel Valtr: “Probability that n random points are in convex position.” Discrete & Computational Geometry 13.1 (1995): 637-643.

    0 讨论(0)
提交回复
热议问题