If-Anweisung scheint zu "else" zu springen

Ich versuche ein Programm zu schreiben, das entscheidet, ob sich ein Kreis innerhalb eines Rechtecks ​​befindet oder dieses berührt. Der Benutzer gibt den Mittelpunkt für den Kreis und den Radius sowie zwei diagonale Punkte für das Rechteck ein.

Ich bin mir nicht sicher, wie ich alle Punkte des Kreisumfangs einbeziehen soll, um festzustellen, dass sich mindestens ein Punkt im Rechteck befindet bzw. dieses berührt. Weiß jemand genau, wie das geht?

Wenn ich mein aktuelles Programm ausführe, gebe ich absichtlich Punkte eines Kreises in ein Rechteck ein und sollte mit den von mir gesetzten if-Anweisungen arbeiten, aber es gibt die falsche Antwort aus.

import java.util.Scanner;
public class lab4 {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    double cx, cy, x, y, r, p1x, p1y, p2x, p2y, max;//input 
    String a;

    System.out.print("Enter cx: ");
    cx = in.nextDouble();
    System.out.print("Enter cy: ");
    cy = in.nextDouble();    
    System.out.print("Enter r: ");
    r = in.nextDouble();

    System.out.println("Enter x value of point 1:");
    p1x = in.nextDouble();
    System.out.println("Enter y value of point 1:");
    p1y = in.nextDouble();
    System.out.println("Enter x value of point 2:");
    p2x = in.nextDouble();
    System.out.println("Enter y value of point 2:");
    p2y = in.nextDouble();


    max = p2x;
    if (p1x > max)
        max = p1x;

    max = p2y;
    if (p1y > max)
        max = p1y;

    if (cx >= p1x && cx <= p2x)
        a = "Circle is inside of Rectangle";
    if (cx >= p1x && cx <= p2x)
        a = "Circle is inside of Rectangle";
    if (cx+r >= p1x && cx+r <= p2x)
        a = "Circle is inside of Rectangle";
    if (cx-r >= p1x && cx-r <= p2x)
        a = "Circle is inside of Rectangle";
    if (cy >= p1y && cy <= p2y)
        a = "Circle is inside of Rectangle";
    if (cy >= p1y && cy <= p2y)
        a = "Circle is inside of Rectangle";
    if (cy+r >= p1y && cy+r <= p2y)
        a = "Circle is inside of Rectangle";
    if (cy-r >= p1y && cy-r <= p2y)
        a = "Circle is inside of Rectangle";
    else
        a = "Circle is outside of Rectangle";

    System.out.println(a); 

Antworten auf die Frage(5)

Ihre Antwort auf die Frage