Hoang Nguyen:
Estoy haciendo una misión de comprobar superposición entre rectángulos. Alguien me puede dar ideas de cómo hacer esto?
Por ejemplo: Plot1 dado (12,12,6,6) y Plot2 (12,12,3,2). El resultado es Plot2 enteramente dentro Plot1, pero no puede, bajo qué.
Nota: Por favor, corríjanme si hice algo mal en la parte del constructor.
public class Plot {
private int x; //default
private int y; //default
private int depth; //default
private int width; //default
Plot(){
x = 0;
y = 0;
depth = 1;
width = 1;
}
public Plot (Plot p) { //Copy constructor
this.x = p.x;
this.y = p.y;
this.width = p.width;
this.depth = p.depth;
}
public Plot(int x, int y, int width, int depth) {
this.x = x;
this.y = y;
this.width = width;
this.depth = depth;
}
public boolean overlaps(Plot plot) {
}
public boolean encompasses(Plot plot) {
}
// Getters and setters for all
}
anosreP:
Se puede utilizar el intersects()
método de java.awt.Rectangle
los solapamientos:
public boolean overlaps(Plot plot) {
return new Rectangle(x, y, width, depth).intersects(plot.getX(), plot.getY(), plot.getWidth(), plot.getDepth());
}
y contains()
para se compone de:
public boolean overlaps(Plot plot) {
return new Rectangle(x, y, width, depth).contains(plot.getX(), plot.getY(), plot.getWidth(), plot.getDepth());
}
Si getWidth()
en cambio se refiere a la longitud de arriba-abajo, simplemente invertir plot.getWidth()
y plot.getDepth
.
(Esto es suponiendo que la parcela tiene getters)