Domanda

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 is going well except that System.out.println(a.intersects(b)) is returning true for rectangles that definitely should not intersect. Worse still, the drawing created by show() is showing that they intersect when they definitely should not. For example, (and tell me if I'm completely wrong) %java Rectangles 5 5 3 6 should definitely not return true, right? because a rectangle centered at 5,5 whose width is three would definitely not intersect with a rectangle centered at 0,0 whose width is one.

help is appreciated. I would post a pic of the image displayed, but it says I have to have more reputation to post images. oh well. It was intersecting rectangles.

based on some comments, I edited my code and it now looks like this:

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



    }

}

I am still getting funky answers for intersects, and for some reason my drawings always have intersecting rectangles. I don't know what I'm doing wrong. After changing code I tried %java Rectangles 5 5 3 6 and it said they intersect and also drew an image of intersecting rectangles. What is going on?

È stato utile?

Soluzione

I fixed it.

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



 }

}

Altri suggerimenti

There is an error in formula for intersection, try this one

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

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 easier to understand.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top