that this formula fails for points with identical x-coordinates, so this will ha
ID: 3874470 • Letter: T
Question
that this formula fails for points with identical x-coordinates, so this will have to be a special case is the to determine the slope between two points (yy te t and (, )2 code.) Since Ja iais nast the decimal point before you compare them. lope values to a reasonable accuracy such as four ss called Rectangle that represents a rectangular two-dimensional region. Your Rectangle object Re 18. Write a class hould have the following methods: public Rectangle(int Constructs a new Rectangle whose top-left corner is specified by the given coordinates and with the given vidth and height. Throw an IllegalArgument Except ion on a negative width or height. public int getHeight() Returns this Rectangle's height. public int getwidth() Returns this Rectangle's width. public int getx() Returns this Rectangle's x-coordinate. public int getY() x, int y, int width, int height)Explanation / Answer
public class Rectangle {
private int x = 0, y = 0, width = 0, height = 0;
public Rectangle(int x, int y, int width, int height) {
if(width < 0 || height < 0)
throw new IllegalArgumentException();
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
@Override
public String toString() {
return String.format("[Rectangle %s-%s,%s-%s %sx%s]", getMinX(), getMaxX(), getMinY(), getMaxY(), getWidth(), getHeight());
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int getMinX() {
return x;
}
public int getMinY() {
return y;
}
public int getMaxX() {
return width == 0 ? 0 : x + width - 1;
}
public int getMaxY() {
return height == 0 ? 0 : y + height - 1;
}
public boolean isEmpty() {
return width == 0 || height == 0;
}
public boolean contains(int x, int y) {
return (! isEmpty()) && (x >= getMinX() && y >= getMinY() && x <= getMaxX() && y <= getMaxY());
}
public boolean contains(Rectangle rect) {
return (! rect.isEmpty()) && contains(rect.getMinX(), rect.getMinY()) && contains(rect.getMaxX(), rect.getMaxY());
}
public boolean add(int x, int y) {
if (contains(x, y)) {
return false;
}
if (isEmpty()) {
this.x = x;
this.y = y;
this.width = 1;
this.height = 1;
return true;
}
int dx = x - getMinX();
if (dx < 0) {
this.x = x;
this.width -= dx;
} else if (dx > width) {
this.width = dx + 1;
}
int dy = y - getMinY();
if (dy < 0) {
this.y = y;
this.height -= dy;
} else if (dy > height) {
this.height = dy + 1;
}
return true;
}
public boolean add(Rectangle rect) {
if (rect.isEmpty()) {
return false;
}
boolean changed1 = add(rect.getMinX(), rect.getMinY());
boolean changed2 = add(rect.getMaxX(), rect.getMaxY());
return changed1 || changed2;
// why not
// return add(rect.getMinX(), rect.getMinY()) || add(rect.getMaxX(), rect.getMaxY())
}
public Rectangle union(Rectangle rect) {
Rectangle union = new Rectangle();
union.add(this);
union.add(rect);
return union;
}
public Rectangle intersection(Rectangle rect) {
Rectangle intersection = new Rectangle();
int minX = Math.max(getMinX(), rect.getMinX());
int minY = Math.max(getMinY(), rect.getMinY());
int maxX = Math.min(getMaxX(), rect.getMaxX());
int maxY = Math.min(getMaxY(), rect.getMaxY());
if (minX <= maxX && minY <= maxY) {
intersection.add(minX, minY);
intersection.add(maxX, maxY);
}
return intersection;
}
public boolean intersects(Rectangle rect) {
if (isEmpty() || rect.isEmpty()) {
return false;
}
return ! intersection(rect).isEmpty();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.