How to generate a random convex polygon?

前端 未结 6 1233
南旧
南旧 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: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.

提交回复
热议问题