Generate a random point within a circle (uniformly)

前端 未结 21 2726
逝去的感伤
逝去的感伤 2020-11-22 15:45

I need to generate a uniformly random point within a circle of radius R.

I realize that by just picking a uniformly random angle in the interval [0 ... 2π),

21条回答
  •  醉梦人生
    2020-11-22 16:14

    Note the point density in proportional to inverse square of the radius, hence instead of picking r from [0, r_max], pick from [0, r_max^2], then compute your coordinates as:

    x = sqrt(r) * cos(angle)
    y = sqrt(r) * sin(angle)
    

    This will give you uniform point distribution on a disk.

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

提交回复
热议问题