如果声明似乎正在跳过其他

时间:2013-09-13 20:44:41

标签: java if-statement geometry rectangles

我正在尝试编写一个程序,用于确定圆圈是否在内部/触摸矩形。用户放入圆的中心点和半径,以及矩形的两个对角点。

我不确定如何包含圆周长的所有点,以告知矩形中至少有一个点/接触矩形。有人知道怎么做吗?

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

5 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

由于您没有对每个条件使用else if,因此最后ifelse对语句将覆盖以前的所有if

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

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

答案 2 :(得分:0)

正如其他人所说,你需要一串if ... else if ... else if ... else来使你的逻辑正常运作。

但是,有一种更简单的方法。要测试圆的任何部分是否接触或位于矩形内,只需将圆形半径展开,然后测试圆的中心是在内部还是在展开的矩形上。由于您使用的是双坐标,因此您可以使用Rectangle.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);
}

答案 3 :(得分: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)

答案 4 :(得分:-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 if if if this:

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