[Python] Generating random point inside irregular polygon

Discussão em 'Python' iniciado por Stack, Julho 23, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to implement the algorithm provided in this link to generate n-randomly located points in a convex polygon using Python. I have been able to complete steps 1 and 3 successfully, however don't quite understand how to complete step 2. Step 2 involves first:

    • finding the fraction of the total area that each triangle takes up, and store that in a list.
    • generating a random double number from 0 - 1

    Using these two ingredients, the user states the following, and I quote:


    subtract each value in the list until the next subtraction would make it negative
    ​

    I find the above statement highly ambiguous. I understand that conceptually the numbers in the list represent some kind of likelihood for that item in the list to get picked, but I don't understand how to do the above, what needs to be done, or why it means that the weighting of triangle area is taken into consideration.

    What can I try next?

    Edit


    I also found this blog post which contains additional information, though I'm not quite convinced yet. Perhaps this has already been implemented somewhere?

    Continue reading...

Compartilhe esta Página