-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathProblem$25.java
73 lines (59 loc) · 2.18 KB
/
Problem$25.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package chapter_fourteen;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Polygon;
import javafx.stage.Stage;
/**
* *14.25 (Random points on a circle) Modify Programming Exercise 4.6 to create five
* random points on a circle, form a polygon by connecting the points clockwise,
* and display the circle and the polygon, as shown in Figure 14.51b.
*
*
* @author Sharaf Qeshta
* */
public class Problem$25 extends Application
{
public static void main(String[] args)
{
launch(args);
}
@Override
public void start(Stage primaryStage)
{
Pane pane = new Pane();
double randomCenterX = ((Math.random() * 1000) % 300) + 100; // 100 -> 399
double randomCenterY = ((Math.random() * 1000) % 300) + 100; // 100 -> 399
double randomRadius = ((Math.random() * 100) % 200) + 10; // 10 -> 209
Circle circle = new Circle(randomCenterX, randomCenterY, randomRadius);
circle.setFill(Color.WHITE);
circle.setStroke(Color.BLACK);
double[][] points = new double[5][2];
for (int i = 0; i < points.length;i++)
{
double randomAngle = (Math.random() * 500) % 360;
double x = randomRadius * Math.cos(Math.toRadians(randomAngle)) + randomCenterX;
double y = randomRadius * Math.sin(Math.toRadians(randomAngle)) + randomCenterY;
points[i][0] = x;
points[i][1] = y;
}
Polygon polygon = new Polygon();
polygon.getPoints().addAll(
points[0][0], points[0][1],
points[1][0], points[1][1],
points[2][0], points[2][1],
points[3][0], points[3][1],
points[4][0], points[4][1],
points[0][0], points[0][1]
);
polygon.setFill(Color.WHITE);
polygon.setStroke(Color.BLACK);
pane.getChildren().addAll(circle, polygon);
Scene scene = new Scene(pane, 500, 500);
primaryStage.setTitle("Problem$25");
primaryStage.setScene(scene);
primaryStage.show();
}
}