How do I generate a random point inside of a polygon?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I’d like to generate a random point inside of a polygon that is defined by an array of points.

  2. What is the issue? Most solutions I have found use some sort of external library that ROBLOX does not have, and plus I am not very good at maths, so making an algorithm to generate a random point inside of a polygon is way beyond my capabilities.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried to look for solutions on the Developer Hub, but most of them only tell you if a point is inside of a polygon, they do not tell you how to generate a random point inside of a polygon.

The reason why I want to do this is because I want to spawn mobs in an area defined by a polygon.

If I made any mistakes in my post, please tell me.

2 Likes

As long as the polygon is not a triangle*, you can do the following to generate a random point within a CONVEX polygon:

  1. Define a number of steps/iterations. For this example we can use local steps = 10.
  2. Select a random point from your array of points. Let’s call this point A.
  3. Loop the following steps times: (In this case, 10 times)
    a. Select a random point from your array of points. This point is now point B.
    b. Calculate the midpoint between point A and point B.
    c. This midpoint is now your new point A. Rinse and repeat.
  4. After iterating the above steps times, you should be left with a random point in your grid.

NOTE: This point may be biased toward the center of the polygon. Upping the step count should relieve this bias.

*Using a triangle would actually end up generating a Sierpinski Triangle :stuck_out_tongue:

6 Likes