Skip to content

Commit 482f57a

Browse files
committed
Exercise 1.1.31
1 parent 4575300 commit 482f57a

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

  • src/main/java/com/hezhou/algorithm/chapter1/section1
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.hezhou.algorithm.chapter1.section1;
2+
3+
import edu.princeton.cs.algs4.StdDraw;
4+
import edu.princeton.cs.algs4.StdRandom;
5+
6+
public class Ex31 {
7+
static class Point {
8+
double x;
9+
double y;
10+
11+
public Point(double x, double y) {
12+
this.x = x;
13+
this.y = y;
14+
}
15+
}
16+
17+
public static void main(String[] args) {
18+
int N = Integer.parseInt(args[0]);
19+
double p = Double.parseDouble(args[1]);
20+
21+
double radius = 0.5;
22+
StdDraw.circle(0.5, 0.5, radius);
23+
StdDraw.setPenRadius(0.05);
24+
StdDraw.setPenColor(StdDraw.RED);
25+
26+
double average = Math.PI * 2 / N;
27+
Point[] points = new Point[N];
28+
for (int i = 0; i < N; i++) {
29+
double angle = average * i;
30+
double x = radius + radius * Math.cos(angle);
31+
double y = radius + radius * Math.sin(angle);
32+
points[i] = new Point(x, y);
33+
StdDraw.point(x, y);
34+
}
35+
36+
StdDraw.setPenRadius(0.01);
37+
StdDraw.setPenColor(StdDraw.GRAY);
38+
for (int i = 0; i < N; i++) {
39+
for (int j = i + 1; j < N; j++) {
40+
if (StdRandom.bernoulli(p)) {
41+
StdDraw.line(points[i].x, points[i].y, points[j].x, points[j].y);
42+
}
43+
}
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)