我正在尝试编写一个程序来确定一个圆圈是否在/触摸一个矩形。用户为圆和半径输入中心点,为矩形输入两个对角点。
我不确定如何包括圆的圆周的所有点,以说明至少有一个点在/接触矩形。任何人都知道如何做到这一点?
当我运行我当前的程序时,我会故意输入一个矩形内的圆点,并且应该使用我放置的 if 语句,但它会打印出错误的答案。
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);