How to find a random point in a quadrangle?

后端 未结 9 1678
忘掉有多难
忘掉有多难 2021-02-08 12:53

I have to be able to set a random location for a waypoint for a flight sim. The maths challenge is straightforward:

\"To find a single random location within a quadrangl

9条回答
  •  星月不相逢
    2021-02-08 13:22

    I believe there are two suitable ways to solve this problem.

    The first mentioned by other posters is to find the smallest bounding box that encloses the rectangle, then generate points in that box until you find a point which lies inside the rectangle.

      Find Bounding box (x,y,width, height)
      Pick Random Point x1,y1 with ranges [x to x+width] and [y to y+height]
      while (x1 or y1 is no inside the quadrangle){
           Select new x1,y1
      }
    

    Assuming your quadrangle area is Q and the bounding box is A, the probability that you would need to generate N pairs of points is 1-(Q/A)^N, which approaches 0 inverse exponentially.

    I would reccommend the above approach, espesially in two dimensions. It is very fast to generate the points and test.

    If you wanted a gaurentee of termination, then you can create an algorithm to only generate points within the quadrangle (easy) but you must ensure the probablity distribution of the points are even thoughout the quadrangle.

    http://mathworld.wolfram.com/TrianglePointPicking.html

    Gives a very good explination

提交回复
热议问题