-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRectangleIntersection.java
More file actions
152 lines (103 loc) · 5.34 KB
/
RectangleIntersection.java
File metadata and controls
152 lines (103 loc) · 5.34 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.geometry.Pos;
import javafx.scene.paint.Color;
public class Exercise_16_09 extends Application{
protected Rectangle rectangle = getRectangle();
protected Pane paneForRectangles = new Pane(rectangle);
protected TablePane table = new TablePane();
@Override
//Start method for application class overridden
public void start(Stage primaryStage){
//pane containint table for rectangle display made
paneForRectangles.getChildren().addAll(
table.getRectangle1(), table.getRectangle2());
//Buttons for user adjustments made
Button btRedrawCircles = new Button("Redraw Rectangles");
//VBox made to stack child nodes of rectangles over each other
VBox pane = new VBox();
pane.setAlignment(Pos.CENTER);
pane.getChildren().addAll(intersect(), paneForRectangles,
table, btRedrawCircles);
//Handlers for child nodes created and getters set
btRedrawCircles.setOnAction(e ->{
table.redraw();
pane.getChildren().remove(0);
pane.getChildren().add(0, intersect());
});
//Table getter with rectangle locations
table.getRectangle1().setOnMouseDragged(e ->{
if (table.getRectangle1().contains(e.getX(), e.getY())){
table.setRectangle1X(e.getX() - (table.getRectangle1Width() / 2));
table.setRectangle1Y(e.getY() - (table.getRectangle1Height() / 2));
table.setTfRectangle1X(String.valueOf(e.getX()
- (table.getRectangle1Width() / 2)));
table.setTfRectangle1Y(String.valueOf(e.getY()
- (table.getRectangle1Height() / 2)));
}
pane.getChildren().remove(0);
pane.getChildren().add(0, intersect());
});
//Table getter with rectangle 2 locations input
table.getRectangle2().setOnMouseDragged(e ->{
if (table.getRectangle2().contains(e.getX(), e.getY())){
table.setRectangle2X(e.getX() - (table.getRectangle2Width() / 2));
table.setRectangle2Y(e.getY() - (table.getRectangle2Height() / 2));
table.setTfRectangle2X(String.valueOf(e.getX()
- (table.getRectangle2Width() / 2)));
table.setTfRectangle2Y(String.valueOf(e.getY()
- (table.getRectangle2Height() / 2)));
}
pane.getChildren().remove(0);
pane.getChildren().add(0, intersect());
});
//Scene made in stage to display both rectangle 1 & 2
Scene scene = new Scene(pane);
//Stage title/name setter
primaryStage.setTitle("Java 16.9");
//Scene placed into stage for final display
primaryStage.setScene(scene);
//Scene displayed
primaryStage.show();
}
//Detection of intersection noted and displayed to user
private Text intersect(){
return new Text("Two rectangles intersect? " +
(isIntersect() ? "yes" : "no"));
}
/* Main method describing whether or not there is an intersection
of rectangles in the scene */
private boolean isIntersect(){
return
(getDistance(table.getRectangle2X(), table.getRectangle1X() +
table.getRectangle1Width()) <
table.getRectangle1Width() + table.getRectangle2Width() &&
(getDistance(table.getRectangle2Y(), table.getRectangle1Y() +
table.getRectangle1Height()) <
table.getRectangle1Height() + table.getRectangle2Height())) &&
(getDistance(table.getRectangle1X(), table.getRectangle2X() +
table.getRectangle2Width()) <
table.getRectangle1Width() + table.getRectangle2Width() &&
(getDistance(table.getRectangle1Y(), table.getRectangle2Y() +
table.getRectangle2Height()) <
table.getRectangle1Height() + table.getRectangle2Height()));
}
//Distance then returned to user through p2-p1 function
private double getDistance(double p1, double p2){
return Math.sqrt(Math.pow(p2 - p1, 2));
}
//Rectangle size and color displayed via JavaFX
private Rectangle getRectangle(){
Rectangle r = new Rectangle(0, 0, 280, 110);
r.setStroke(Color.WHITE);
r.setFill(Color.WHITE);
//Program ended
return r;
}
}