我的任务是检查矩形内是否有两个点,检查矩形是否在另一个矩形内,还检查矩形是否与另一个矩形重叠
我完成了前两个步骤,但我无法解决重叠方法。
这就是我所做的。
public class MyRectangle2D {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
MyRectangle2D rect = new MyRectangle2D();
//returns area of default rectangle
System.out.println(rect.getArea());
//returns perimeter of default rectangle
System.out.println(rect.getPerimeter());
//returns true if default (0,0) is inside rectangle
System.out.println(rect.contains(0,0));
//returns false if point is not in triangle
System.out.println(rect.contains(10,10));
MyRectangle2D r1 = new MyRectangle2D(10,2,6,4);
System.out.println("The area of your rectangle is " + r1.getArea() +
" \nthe perimeter of your rectangle is " + r1.getPerimeter());
System.out.println("Testing Rectangle 2 with points (1,8)...");
//following lines test the contain(x,y) method
System.out.println(r1.contains(1,8) ? "Point (1,8) is on Rectangle" : "Point(1,8) is not on Rectangle");
System.out.println("Testing Rectangle 2 with points (10,1)...");
System.out.println(r1.contains(10,1) ? "Point (10,1) is on Rectangle" : "Point(10,1) is not on Rectangle");
//following lines test contains(rect) method
System.out.println(r1.contains(new MyRectangle2D(9,1,1,1)) ? "Rectangle is inside" : "Not inside");
System.out.println(r1.contains(new MyRectangle2D(9,1,10,2)) ? "Rectangle is inside" : "Rectangle is not inside");
//following lines test if rectangles overlap
System.out.println(r1.overlaps(new MyRectangle2D(4,2,8,4)));
}
double x;
double y;
double width;
double height;
private MyRectangle2D(){
x = 0;
y = 0;
width = 1;
height = 1;
}
private MyRectangle2D(double newX, double newY,
double newWidth, double newHeight){
setX(newX);
setY(newY);
setHeight(newHeight);
setWidth(newWidth);
}
double getX(){
return x;
}
double getY(){
return y;
}
void setX(double newA){
x = newA;
}
void setY(double newA){
y = newA;
}
double getWidth(){
return width;
}
double getHeight(){
return height;
}
void setWidth(double newA){
width = newA;
}
void setHeight(double newA){
height = newA;
}
double getArea(){
double area = width * height;
return area;
}
double getPerimeter(){
double perimeter = (2 * width) + (2 * height);
return perimeter;
}
boolean contains(double x, double y){
boolean inside = false;
if(x<(getWidth()/2 + getX()) && x>getX()-(getWidth()/2)){
if(y<(getY() + getHeight()/2) && y>getY()-(getHeight()/2))
inside = true;
}
return inside;
}
boolean contains(MyRectangle2D r){
boolean inside = false;
if(r.getX()<(getWidth()/2 + getX()) && r.getX()>getX()-(getWidth()/2)){
if(r.getY()<(getY() + getHeight()/2) && r.getY()>getY()-(getHeight()/2)){
if(r.getWidth()<=getWidth() && r.getHeight()<=getHeight())
inside = true;
}
}
return inside;
}
boolean overlaps(MyRectangle2D r){
boolean inside = false;
if(contains(r.getX()+r.getWidth(),r.getY()+r.getHeight()))
inside = true;
if(contains(r.getX()-r.getWidth(),r.getY()-r.getHeight()))
inside = true;
return inside;
}
}
答案 0 :(得分:1)
这将找到矩形是否与另一个矩形重叠,不要忘记将其标记为答案,并在可能时投票。
public boolean overlaps (MyRectangle2D r) {
return x < r.x + r.width && x + width > r.x && y < r.y + r.height && y + height > r.y;
}