如何选择距参考点一定距离的随机点

How to chose a random point at a certain distance to a reference point

我有点A (pointA = (x1, y1)) 我需要随机选择一个点B (pointB = (x2, y2)) 这样 AB 之间的距离等于 K.

这是一道简单的数学题。

x2 = x1 - k + 2k*new Random().nextDouble()

现在你可以计算y2了。

我们用极坐标形式求解。

我们需要这些双打 distancex1y1

首先,我们需要以弧度为单位的角度:

double angle = Math.random()*2*Math.PI;

然后我们想得到我们点的 x 和 y 偏移量:

double xOff = Math.cos(angle)*distance;
double yOff = Math.sin(angle)*distance;

然后我们将这些添加到我们的第一点:

double x2 = x1 + xOff;
double y2 = y1 + yOff;

这将使您与第一个点相差 distance