@Test public void testIntersect_South() throws Exception { Viewport a = new Viewport(30, 100, 3, 3); Viewport south = new Viewport(29, 101, 2, 1); Viewport expected = new Viewport(30, 101, 1, 1); boolean intersected = a.intersect(south); assertTrue("overlapping", intersected); assertEquals("intersection", a, expected); }
public boolean intersect(int x, int y, int width, int height) { if (this.width > 0 && this.height > 0 && width > 0 && height > 0 && this.x < (x + width) && x < (this.x + this.width) && this.y < (y + height) && y < (this.y + this.height)) { if (this.x < x) { this.width -= x - this.x; this.x = x; } if (this.y < y) { this.height -= y - this.y; this.y = y; } if ((this.x + this.width) > (x + width)) { this.width = x + width - this.x; } if ((this.y + this.height) > (y + height)) { this.height = y + height - this.y; } return true; } return false; }
Viewport { public boolean intersect(int x, int y, int width, int height) { if (this.width > 0 && this.height > 0 && width > 0 && height > 0 && this.x < (x + width) && x < (this.x + this.width) && this.y < (y + height) && y < (this.y + this.height)) { if (this.x < x) { this.width -= x - this.x; this.x = x; } if (this.y < y) { this.height -= y - this.y; this.y = y; } if ((this.x + this.width) > (x + width)) { this.width = x + width - this.x; } if ((this.y + this.height) > (y + height)) { this.height = y + height - this.y; } return true; } return false; } }
Viewport { public boolean intersect(int x, int y, int width, int height) { if (this.width > 0 && this.height > 0 && width > 0 && height > 0 && this.x < (x + width) && x < (this.x + this.width) && this.y < (y + height) && y < (this.y + this.height)) { if (this.x < x) { this.width -= x - this.x; this.x = x; } if (this.y < y) { this.height -= y - this.y; this.y = y; } if ((this.x + this.width) > (x + width)) { this.width = x + width - this.x; } if ((this.y + this.height) > (y + height)) { this.height = y + height - this.y; } return true; } return false; } Viewport();  Viewport(int x, int y, int width, int height);  Viewport(Viewport viewport); }
Viewport { public boolean intersect(int x, int y, int width, int height) { if (this.width > 0 && this.height > 0 && width > 0 && height > 0 && this.x < (x + width) && x < (this.x + this.width) && this.y < (y + height) && y < (this.y + this.height)) { if (this.x < x) { this.width -= x - this.x; this.x = x; } if (this.y < y) { this.height -= y - this.y; this.y = y; } if ((this.x + this.width) > (x + width)) { this.width = x + width - this.x; } if ((this.y + this.height) > (y + height)) { this.height = y + height - this.y; } return true; } return false; } Viewport();  Viewport(int x, int y, int width, int height);  Viewport(Viewport viewport); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); Viewport set(int x, int y, int width, int height); Viewport set(Viewport viewport); Viewport setEmpty(); boolean isEmpty(); boolean intersects(int x, int y, int width, int height); @SuppressWarnings("UnnecessaryLocalVariable") boolean intersects(Viewport viewport); boolean intersect(int x, int y, int width, int height); @SuppressWarnings("UnnecessaryLocalVariable") boolean intersect(Viewport viewport); boolean contains(int x, int y); }
Viewport { public boolean intersect(int x, int y, int width, int height) { if (this.width > 0 && this.height > 0 && width > 0 && height > 0 && this.x < (x + width) && x < (this.x + this.width) && this.y < (y + height) && y < (this.y + this.height)) { if (this.x < x) { this.width -= x - this.x; this.x = x; } if (this.y < y) { this.height -= y - this.y; this.y = y; } if ((this.x + this.width) > (x + width)) { this.width = x + width - this.x; } if ((this.y + this.height) > (y + height)) { this.height = y + height - this.y; } return true; } return false; } Viewport();  Viewport(int x, int y, int width, int height);  Viewport(Viewport viewport); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); Viewport set(int x, int y, int width, int height); Viewport set(Viewport viewport); Viewport setEmpty(); boolean isEmpty(); boolean intersects(int x, int y, int width, int height); @SuppressWarnings("UnnecessaryLocalVariable") boolean intersects(Viewport viewport); boolean intersect(int x, int y, int width, int height); @SuppressWarnings("UnnecessaryLocalVariable") boolean intersect(Viewport viewport); boolean contains(int x, int y); public int x; public int y; public int width; public int height; }