|
| 1 | +package ch_15; |
| 2 | + |
| 3 | + |
| 4 | +import javafx.application.Application; |
| 5 | +import javafx.geometry.Point2D; |
| 6 | +import javafx.scene.Scene; |
| 7 | +import javafx.scene.layout.Pane; |
| 8 | +import javafx.scene.shape.Circle; |
| 9 | +import javafx.scene.shape.Line; |
| 10 | +import javafx.scene.text.Text; |
| 11 | +import javafx.stage.Stage; |
| 12 | + |
| 13 | +/** |
| 14 | + * 15.21 (Drag points) Draw a circle with three random points on the circle. Connect |
| 15 | + * the points to form a triangle. Display the angles in the triangle. Use the mouse |
| 16 | + * to drag a point along the perimeter of the circle. As you drag it, the triangle and |
| 17 | + * angles are redisplayed dynamically, as shown in Figure 15.30b. For computing |
| 18 | + * angles in a triangle, see Listing 4.1. |
| 19 | + * <p> |
| 20 | + * The parametric equation for a circle is |
| 21 | + * x = cx + r * cos(a) |
| 22 | + * y = cy + r * sin(a) |
| 23 | + * where r is the radius, (cx, cy) the center of the circle, and a, an angle in radians. |
| 24 | + */ |
| 25 | +public class Exercise15_21 extends Application { |
| 26 | + private double radius = 10; |
| 27 | + private int centerX = 200; |
| 28 | + private int centerY = 125; |
| 29 | + private int radiusMainCircle = 100; |
| 30 | + private Pane pane = new Pane(); |
| 31 | + private Circle outlineCircle = new Circle(centerX, centerY, radiusMainCircle); |
| 32 | + private Circle[] points = new Circle[3]; |
| 33 | + private Line line1 = new Line(); |
| 34 | + private Line line2 = new Line(); |
| 35 | + private Line line3 = new Line(); |
| 36 | + private Text[] text = {new Text(), new Text(), new Text()}; |
| 37 | + |
| 38 | + @Override |
| 39 | + public void start(Stage primaryStage) { |
| 40 | + draw(); |
| 41 | + pane.getChildren().addAll(points[0], points[1], points[2], |
| 42 | + line1, line2, line3, text[0], text[1], text[2]); |
| 43 | + Scene scene = new Scene(pane, 400, 250); |
| 44 | + primaryStage.setTitle(getClass().getName()); |
| 45 | + primaryStage.setScene(scene); |
| 46 | + primaryStage.show(); |
| 47 | + |
| 48 | + points[0].setOnMouseDragged(e -> { |
| 49 | + if (points[0].contains(e.getX(), e.getY())) { |
| 50 | + // Recompute and display angles |
| 51 | + points[0].setCenterX(e.getX()); |
| 52 | + points[0].setCenterY(e.getY()); |
| 53 | + setLines(); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + points[1].setOnMouseDragged(e -> { |
| 58 | + if (points[1].contains(e.getX(), e.getY())) { |
| 59 | + // Recompute and display angles |
| 60 | + points[1].setCenterX(e.getX()); |
| 61 | + points[1].setCenterY(e.getY()); |
| 62 | + setLines(); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + points[2].setOnMouseDragged(e -> { |
| 67 | + if (points[2].contains(e.getX(), e.getY())) { |
| 68 | + // Recompute and display angles |
| 69 | + points[2].setCenterX(e.getX()); |
| 70 | + points[2].setCenterY(e.getY()); |
| 71 | + setLines(); |
| 72 | + } |
| 73 | + }); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + void draw() { |
| 78 | + points[0] = new Circle(40, 40, 10); |
| 79 | + points[1] = new Circle(140, 40, 10); |
| 80 | + points[2] = new Circle(60, 140, 10); |
| 81 | + setLines(); |
| 82 | + } |
| 83 | + |
| 84 | + private void setLines() { |
| 85 | + line1.setStartX(points[0].getCenterX()); |
| 86 | + line1.setStartY(points[0].getCenterY()); |
| 87 | + line1.setEndX(points[1].getCenterX()); |
| 88 | + line1.setEndY(points[1].getCenterY()); |
| 89 | + line2.setStartX(points[0].getCenterX()); |
| 90 | + line2.setStartY(points[0].getCenterY()); |
| 91 | + line2.setEndX(points[2].getCenterX()); |
| 92 | + line2.setEndY(points[2].getCenterY()); |
| 93 | + line3.setStartX(points[1].getCenterX()); |
| 94 | + line3.setStartY(points[1].getCenterY()); |
| 95 | + line3.setEndX(points[2].getCenterX()); |
| 96 | + line3.setEndY(points[2].getCenterY()); |
| 97 | + |
| 98 | + // Compute angles |
| 99 | + double a = new Point2D(points[2].getCenterX(), points[2].getCenterY()). |
| 100 | + distance(points[1].getCenterX(), points[1].getCenterY()); |
| 101 | + double b = new Point2D(points[2].getCenterX(), points[2].getCenterY()). |
| 102 | + distance(points[0].getCenterX(), points[0].getCenterY()); |
| 103 | + double c = new Point2D(points[1].getCenterX(), points[1].getCenterY()). |
| 104 | + distance(points[0].getCenterX(), points[0].getCenterY()); |
| 105 | + double[] angle = new double[3]; |
| 106 | + angle[0] = Math.acos((a * a - b * b - c * c) / (-2 * b * c)); |
| 107 | + angle[1] = Math.acos((b * b - a * a - c * c) / (-2 * a * c)); |
| 108 | + angle[2] = Math.acos((c * c - b * b - a * a) / (-2 * a * b)); |
| 109 | + |
| 110 | + for (int i = 0; i < 3; i++) { |
| 111 | + text[i].setX(points[i].getCenterX()); |
| 112 | + text[i].setY(points[i].getCenterY() - radius); |
| 113 | + text[i].setText(String.format("%.2f", Math.toDegrees(angle[i]))); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments