Trying to check rectangles for intersections in Java, what am I doing wrong?

here is my code:

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();


    }

}

I am new to this. This is from a lab assignment based on creating data types. Everything goes well, except that System.out.println (a.intersects (b)) returns true for rectangles that definitely should not intersect. Worse, the picture created by show () shows that they intersect when they definitely shouldn't. For example, (and tell me if I'm completely mistaken)% java Rectangles 5 5 3 6 will definitely not return, right? because a rectangle with a center of 5.5, whose width is three, definitely does not intersect with a rectangle with a center of 0.0, whose width is one.

. , , . . .

, , :

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 , , . ?

+4
3

.

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();



 }

}
+2

,

   ((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)))
0

If we think geometrically,

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

should be enough and more understandable.

0
source

Source: https://habr.com/ru/post/1530802/


All Articles