Dies ist die Übung 3.28 aus dem Buch Introduction to Java Programming - Comprehensive Edition. Der Code testet, ob die beiden Rechtecke eingerückt sind, ob eines innerhalb des anderen liegt und ob eines außerhalb des anderen liegt. Wenn keine dieser Bedingungen erfüllt ist, überschneiden sich die beiden Rechtecke.
**3.28 (Geometrie: zwei Rechtecke) Schreiben Sie ein Programm, das den Benutzer auffordert, die x-, y-Koordinaten, Breite und Höhe von zwei Rechtecken auffordert und feststellt ob das zweite Rechteck innerhalb des ersten liegt oder sich mit dem ersten überschneidet, wie in in Abbildung 3.9. Testen Sie Ihr Programm, um alle Fälle abzudecken. Hier sind die Beispieldurchläufe:
Geben Sie die x- und y-Koordinaten, die Breite und die Höhe des Mittelpunkts von r1 ein: 2,5 4 2,5 43 Geben Sie den Mittelpunkt, die x- und y-Koordinaten, die Breite und die Höhe von r2 ein: 1.5 5 0.5 3 r2 liegt innerhalb von r1
Geben Sie die x- und y-Koordinaten, die Breite und die Höhe des Mittelpunkts von r1 ein: 1 2 3 5.5 Geben Sie die x- und y-Koordinaten, die Breite und die Höhe des Mittelpunkts von r2 ein: 3 4 4.5 5 r2 überlappt r1
Geben Sie die x- und y-Koordinaten, die Breite und die Höhe des Mittelpunkts von r1 ein: 1 2 3 3 Geben Sie die x- und y-Koordinaten, die Breite und die Höhe des Mittelpunkts von r2 ein: 40 45 3 2 r2 überschneidet sich nicht mit r1
import java.util.Scanner;
public class ProgrammingEx3_28 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out
.print("Enter r1's center x-, y-coordinates, width, and height:");
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double w1 = input.nextDouble();
double h1 = input.nextDouble();
w1 = w1 / 2;
h1 = h1 / 2;
System.out
.print("Enter r2's center x-, y-coordinates, width, and height:");
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double w2 = input.nextDouble();
double h2 = input.nextDouble();
w2 = w2 / 2;
h2 = h2 / 2;
// Calculating range of r1 and r2
double x1max = x1 + w1;
double y1max = y1 + h1;
double x1min = x1 - w1;
double y1min = y1 - h1;
double x2max = x2 + w2;
double y2max = y2 + h2;
double x2min = x2 - w2;
double y2min = y2 - h2;
if (x1max == x2max && x1min == x2min && y1max == y2max
&& y1min == y2min) {
// Check if the two are identicle
System.out.print("r1 and r2 are indentical");
} else if (x1max <= x2max && x1min >= x2min && y1max <= y2max
&& y1min >= y2min) {
// Check if r1 is in r2
System.out.print("r1 is inside r2");
} else if (x2max <= x1max && x2min >= x1min && y2max <= y1max
&& y2min >= y1min) {
// Check if r2 is in r1
System.out.print("r2 is inside r1");
} else if (x1max < x2min || x1min > x2max || y1max < y2min
|| y2min > y1max) {
// Check if the two overlap
System.out.print("r2 does not overlaps r1");
} else {
System.out.print("r2 overlaps r1");
}
}
}
5 Stimmen
Ich würde denken, dass die Lösung für Ihr Problem nicht darin besteht tout Multiplikation.
0 Stimmen
Für den Fall, dass Sie eine Antwort für ein gedrehtes Rechteck benötigen, habe ich eine Antwort mit allen Schritten erstellt: stackoverflow.com/questions/62028169/ (es ist in Javascript, kann aber leicht in C++ reproduziert werden)