Algs4-1.3.31随机连接

1.3.31随机连接。编写一段程序,从命令行接受一个整数N和double值p(0到1之间)作为参数,在一个圆上画出大小为0.05且间距相等的N个点,然后将每对点按照概率p用灰线连接。
public class Test
{
    public static void main(String[] args)
    {
        int N=Integer.parseInt(args[0]);
        double P=Double.parseDouble(args[1]);
        double[][] points=new double[N][2];
                
        StdDraw.setXscale(0,500);
        StdDraw.setYscale(0,500);
       
        double Ox=250;
        double Oy=250;
        double r=200;
        //
        StdDraw.setPenRadius(0.005);
        StdDraw.setPenColor(StdDraw.BOOK_LIGHT_BLUE);
        StdDraw.point(Ox,Oy);
        StdDraw.circle(Ox,Oy,r);
        //
        StdDraw.setPenRadius(0.008);
        StdDraw.setPenColor(StdDraw.GRAY);
        for(int i=0;i<N;i++)
        {
            points[i][0]=Ox+Math.cos(i*2*Math.PI/N)*r;
            points[i][1]=Oy+Math.sin(i*2*Math.PI/N)*r;
        }
        //
        for(int i = 0; i < points.length; i++)
          for(int j = 0; j < points.length; j++)
             if(StdRandom.bernoulli(P))
                 StdDraw.line(points[i][0], points[i][1], points[j][0], points[j][1]);
      }
}
图片
图片
参考资料:

图片

图片

图片

图片

图片


原文地址:https://www.cnblogs.com/longjin2018/p/9848698.html