0

我正在尝试编写一个程序来确定一个圆圈是否在/触摸一个矩形。用户为圆和半径输入中心点,为矩形输入两个对角点。

我不确定如何包括圆的圆周的所有点,以说明至少有一个点在/接触矩形。任何人都知道如何做到这一点?

当我运行我当前的程序时,我会故意输入一个矩形内的圆点,并且应该使用我放置的 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); 
4

5 回答 5

1

您的 else 语句仅以最后一个 if 语句为条件。因此,如果最后一个 if 语句为 false,则执行 else 语句。您可能想要:

if ...
else if ...
else if ...
else

仅当所有先前的“if”语句都为假时才执行 else 。

于 2013-09-13T20:48:51.400 回答
0

一些伪代码:

将圆圈移到原点以使事情更简单。将矩形移动相同的量。

p1x = p1x - cx
p2x = p2x - cx
p1y = p1y - cy
p2y - p2y - cy

x^2 + y^2 = r^2
y = +- sqrt( r^2 - x^2)

For x = -r to r

    y = + sqrt( r^2 - x^2 )

    if ( Inbounds(x,y) )return true;

    y = - sqrt(  r^2 - x^2 )

    if ( Inbounds(x,y) )return true;

End For

为了提高精度,您可以执行以下操作:

对于 x = -r 到 r 步长 0.01 (使用双精度并递增 0.01 )

于 2013-09-13T21:07:00.163 回答
0

因为您没有else if对每个条件都使用,所以 lastifelsepair 语句将覆盖之前的所有ifs。

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";

确保使用else if每个备选方案,以确保只有一个块被执行。

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

(此更正将解决当前的问题,但整个算法仍然不正确。)

于 2013-09-13T20:49:35.717 回答
0

正如其他人所说,您需要一个链条if ... else if ... else if ... else才能使您的逻辑正常工作。

但是,有一种更简单的方法。要测试圆的任何部分是否接触或位于矩形内,只需将矩形扩大圆半径,然后测试圆的中心是在扩大的矩形内还是在扩大的矩形上。由于您使用的是双坐标,因此您可以使用 aRectangle.Double来完成所有繁重的工作:

public static void main(String[] args) {
    double cx, cy, r, p1x, p1y, p2x, p2y;

    // first input cx, cy, r, p1x, p1y, p2x, and p2y

    // construct a zero-width/height rectangle at p1
    Rectangle2D.Double p1 = new Rectangle2D.Double(p1x, p1y, 0, 0);

    // construct another one at p1
    Rectangle2D.Double p2 = new Rectangle2D.Double(p2x, p2y, 0, 0);

    // construct the union of the two
    Rectangle2D.Double rect = p1.createUnion(p2);

    // expand the rectangle
    rect.setBounds(rect.x - r, rect.y - r, rect.w + 2 * r, rect.h + 2 * r);

    // test for containment
    if (rect.contains(cx, cy) {
        a = "Circle is inside of Rectangle";
    } else {
        a = "Circle is outside of Rectangle";
    }
    System.out.println(a);
}
于 2013-09-13T21:04:57.577 回答
-1

因为你没有单独对待每个案例else if,所以如果条件是你覆盖 a 如果 if 条件为真,你的 else if 与最后一个 if 语句有关,而不是全部。

我建议将每个结果连接到这样的变量a,以查看哪些条件有效:

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

或者,如果这不是您想要的,请在您的所有 if 语句中添加 else if,如下所示:

if (cx >= p1x && cx <= p2x)
    a = "Circle is inside of Rectangle";
else if (cx >= p1x && cx <= p2x)
    a = "Circle is inside of Rectangle";
else if (cx+r >= p1x && cx+r <= p2x)
    a = "Circle is inside of Rectangle";
else if (cx-r >= p1x && cx-r <= p2x)
    a = "Circle is inside of Rectangle";
else if (cy >= p1y && cy <= p2y)
    a = "Circle is inside of Rectangle";
else if (cy >= p1y && cy <= p2y)
    a = "Circle is inside of Rectangle";
else if (cy+r >= p1y && cy+r <= p2y)
    a = "Circle is inside of Rectangle";
else if (cy-r >= p1y && cy-r <= p2y)
    a = "Circle is inside of Rectangle";
else
    a = "Circle is outside of Rectangle";
于 2013-09-13T20:54:50.133 回答