How to find a random point in a quadrangle?

后端 未结 9 1677
忘掉有多难
忘掉有多难 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:25

    The "brute force" approach is simply to loop through until you have a valid coordinate. In pseudocode:

    left   = min(pa.x, pb.x, pc.x, pd.x)
    right  = max(pa.x, pb.x, pc.x, pd.x)
    bottom = min(pa.y, pb.y, pc.y, pd.y)
    top    = max(pa.y, pb.y, pc.y, pd.y)
    do {
        x = left   + fmod(rand, right-left)
        y = bottom + fmod(rand, top-bottom)
    } while (!isin(x, y, pa, pb, pc, pd));
    

    You can use a stock function pulled from the net for "isin". I realize that this isn't the fastest-executing thing in the world, but I think it'll work.

提交回复
热议问题