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

这是我的代码:

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)

boolean rightof = ((b.x-b.width)>(x+width));

boolean above = ((b.y-b.height)>(y+height));

boolean below = ((b.y+b.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并且它说它们相交并且还绘制了交叉矩形的图像.到底是怎么回事?

最佳答案 我修好了它.

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

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

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值