试图在Java中测试矩形的交集,我做错了什么?

时间:2014-03-09 06:01:17

标签: java algorithm api intersection rectangles

这是我的代码:

public class Rectangles 
{

    private final double x;
    private final double y;

    private final double width;
    private final double height;



    public Rectangles(double x0, double y0, double w, double h)
    {   
        x = x0;
        y = y0;
        width = w;
        height = h;

    }
    public double area()
    {
        return width * height;
    }
    public double perimeter()
    {
        return 2*width + 2*height;
    }

    public boolean intersects(Rectangles b)
    {
        boolean leftof = ((b.x + b.width)<(x-width));
        boolean rightof = ((b.x-b.width)>(x+width));
        boolean above = ((b.y-b.height)>(y+height));
        boolean below = ((b.y+b.height)<(y-height));
        if (leftof==false && rightof==false && above==false && below==false)
            return false;
        else return true;

    }

    public void show()
    {
        StdDraw.setYscale((0),(y+height));
        StdDraw.setXscale((0), (x+width));
        StdDraw.setPenColor();
        StdDraw.rectangle(x,y,.5*width,.5*height);
    }

    public static void main(String[] args)
    {
        Rectangles a = new Rectangles(Double.parseDouble(args[0]),
                                            Double.parseDouble(args[1]),
                                    Double.parseDouble(args[2]),
                                    Double.parseDouble(args[3]));
        Rectangles b = new Rectangles(0,0,1,1);
        System.out.println(a.area());
        System.out.println(a.perimeter());
        System.out.println(a.intersects(b));
        a.show();
        b.show();


    }

}

我是新手。这是基于创建数据类型的实验室分配。除了System.out.println(a.intersects(b))对于绝对不应该相交的矩形返回true之外,一切都很顺利。更糟糕的是,show()创建的绘图显示它们在绝对不应该相交时相交。例如,(并告诉我,如果我完全错了)%java Rectangles 5 5 3 6肯定不会返回true,对吧?因为一个以5,5为中心且宽度为3的矩形绝对不会与以0,0为中心且宽度为1的矩形相交。

帮助表示赞赏。我会发布一张显示图像的照片,但它说我必须有更多的声望来发布图像。那好吧。它是交叉的矩形。

基于一些评论,我编辑了我的代码,它现在看起来像这样:

public class Rectangles 
{

    private final double x;
    private final double y;

    private final double width;
    private final double height;



    public Rectangles(double x0, double y0, double w, double h)
    {   
        x = x0;
        y = y0;
        width = w;
        height = h;

    }
    public double area()
    {
        return width * height;
    }
    public double perimeter()
    {
        return 2*width + 2*height;
    }

    public boolean intersects(Rectangles b)
    {
        boolean intersects =  ((b.width / 2) + (width / 2) < Math.abs(b.x - x) && 
                (b.height / 2) + (height / 2) < Math.abs(b.y - y));
        if (intersects==false)
            return false;
        else return true;

    }

    public void show()
    {
        StdDraw.setYscale((0),(y+height));
        StdDraw.setXscale((0), (x+width));
        StdDraw.setPenColor();
        StdDraw.rectangle(x,y,.5*width,.5*height);
    }

    public static void main(String[] args)
    {
        Rectangles a = new Rectangles(Double.parseDouble(args[0]),
                                    Double.parseDouble(args[1]),
                                    Double.parseDouble(args[2]),
                                    Double.parseDouble(args[3]));
        Rectangles b = new Rectangles(1.0,1.0,1.0,1.0);
        System.out.println(a.area());
        System.out.println(a.perimeter());
        System.out.println(b.intersects(a));
        a.show();
        b.show();



    }

}

我仍然得到相交的时髦答案,并且由于某种原因,我的绘图总是有交叉的矩形。我不知道我做错了什么。更改代码后我尝试了%java Rectangles 5 5 3 6并且它说它们相交并且还绘制了交叉矩形的图像。发生了什么事?

3 个答案:

答案 0 :(得分:2)

我修好了。

public class Rectangles 
{

private final double x;
private final double y;

private final double width;
private final double height;



public Rectangles(double x0, double y0, double w, double h)
{   
    x = x0;
    y = y0;
    width = w;
    height = h;

}
public double area()
{
    return width * height;
}
public double perimeter()
{
    return 2*width + 2*height;
}

public boolean intersects(Rectangles b)
{
    boolean leftof = ((b.x + (0.5*b.width))<(x-(0.5*width)));
    boolean rightof = ((b.x-(0.5*b.width))>(x+(0.5*width)));
    boolean above = ((b.y-(0.5*b.height))>(y+(0.5*height)));
    boolean below = ((b.y+(0.5*b.height))<(y-(0.5*height)));
    if (leftof==true || rightof==true || above==true || below==true)
        return false;
    else return true;

}

public void show()
{
    double j = Math.max((x+(0.5*height)), (y+(0.5*height)));
    StdDraw.setYscale((0),j+1);
    StdDraw.setXscale((0),j+1);
    StdDraw.setPenColor();
    StdDraw.rectangle(x,y,.5*width,.5*height);
}

public static void main(String[] args)
{
    Rectangles a = new Rectangles(Double.parseDouble(args[0]),
                                        Double.parseDouble(args[1]),
                                Double.parseDouble(args[2]),
                                Double.parseDouble(args[3]));
    Rectangles b = new Rectangles(2,2,2,2);
    System.out.println(a.area());
    System.out.println(a.perimeter());
    System.out.println(a.intersects(b));
    a.show();



 }

}

答案 1 :(得分:0)

交叉公式中存在错误,请尝试此操作

   ((x < b.x && (x + width) > b.x) || (x > b.x && x < (b.x + b.width))) &&
   ((y < b.y && (y + height) > b.y) || (y > b.y && y < (b.y + b.height)))

答案 2 :(得分:0)

如果我们考虑几何,

(b.width / 2) + (width / 2) < abs(b.x - x) && 
(b.height / 2) + (height / 2) < abs(b.y - y)

应该足够且易于理解。