这题大概的意思就是先生成一个正方形,然后再取点,如果取到了,就退出,没有就继续
import java.util.Random;
/**
* @author xnl
* @Description:
* @date: 2022/6/5 22:11
*/
public class Solution {
public static void main(String[] args) {
}
double x, y, radius;
Random random;
public Solution(double radius, double x_center, double y_center) {
random = new Random();
this.radius = radius;
this.x = x_center;
this.y = y_center;
}
public double[] randPoint() {
while (true){
double xc = random.nextDouble() * (radius * 2) - radius;
double yc = random.nextDouble() * (radius * 2) - radius;
if (xc * xc + yc * yc <= radius * radius){
return new double[]{xc + x , yc+ y};
}
}
}
}
这篇博客介绍了如何使用Java实现一个算法,该算法在给定半径的正方形内随机生成点,直到生成的点落在正方形内为止。主要涉及随机数生成和几何距离判断。
701

被折叠的 条评论
为什么被折叠?



