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; }
@Test public void testIntersect() throws Exception { Viewport a = new Viewport(30, 100, 2, 2); Viewport b = new Viewport(31, 101, 2, 2); Viewport northeast = new Viewport(31, 101, 1, 1); boolean intersected = a.intersect(b); assertTrue("intersecting", intersected); assertEquals("intersection", northeast, a); }
@Test public void testIntersect_Empty() throws Exception { Viewport a = new Viewport(30, 100, 2, 2); Viewport b = new Viewport(31, 101, 0, 0); boolean aIntersectedB = a.intersect(b); boolean bIntersectedA = b.intersect(a); assertFalse("a intersecting b", aIntersectedB); assertFalse("b intersecting a", bIntersectedA); }
@Test public void testIntersect_Inside() throws Exception { Viewport a = new Viewport(30, 100, 3, 3); Viewport inside = new Viewport(31, 101, 1, 1); boolean intersected = a.intersect(inside); assertTrue("interesecting", intersected); assertEquals("inside, intersection is interior sector", inside, a); }
@Test public void testIntersect_East() throws Exception { Viewport a = new Viewport(30, 100, 3, 3); Viewport east = new Viewport(31, 102, 1, 2); Viewport expected = new Viewport(31, 102, 1, 1); boolean intersected = a.intersect(east); assertTrue("overlapping", intersected); assertEquals("intersection", a, expected); }
@Test public void testIntersect_West() throws Exception { Viewport a = new Viewport(30, 100, 3, 3); Viewport west = new Viewport(31, 99, 1, 2); Viewport expected = new Viewport(31, 100, 1, 1); boolean intersected = a.intersect(west); assertTrue("overlapping", intersected); assertEquals("intersection", a, expected); }
@Test public void testIntersect_North() throws Exception { Viewport a = new Viewport(30, 100, 3, 3); Viewport north = new Viewport(32, 101, 2, 1); Viewport expected = new Viewport(32, 101, 1, 1); boolean intersected = a.intersect(north); assertTrue("overlapping", intersected); assertEquals("intersection", a, expected); }
@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); }
@Test public void testIntersect_AdjacentEast() throws Exception { Viewport a = new Viewport(30, 100, 3, 3); Viewport adjacentEast = new Viewport(31, 103, 1, 1); Viewport copy = new Viewport(a); boolean intersected = a.intersect(adjacentEast); assertFalse("adjacent, no intersection", intersected); assertEquals("adjacent, no changed", a, copy); }
@Test public void testIntersect_AdjacentWest() throws Exception { Viewport a = new Viewport(30, 100, 3, 3); Viewport adjacentWest = new Viewport(31, 99, 1, 1); Viewport copy = new Viewport(a); boolean intersected = a.intersect(adjacentWest); assertFalse("adjacent, no intersection", intersected); assertEquals("adjacent, no changed", a, copy); }
@Test public void testIntersect_AdjacentNorth() throws Exception { Viewport a = new Viewport(30, 100, 3, 3); Viewport adjacentNorth = new Viewport(33, 101, 1, 1); Viewport copy = new Viewport(a); boolean intersected = a.intersect(adjacentNorth); assertFalse("adjacent, no intersection", intersected); assertEquals("adjacent, no changed", a, copy); }
@Test public void testIntersect_AdjacentSouth() throws Exception { Viewport a = new Viewport(30, 100, 3, 3); Viewport adjacentSouth = new Viewport(29, 101, 1, 1); Viewport copy = new Viewport(a); boolean intersected = a.intersect(adjacentSouth); assertFalse("adjacent, no intersection", intersected); assertEquals("adjacent, no changed", a, copy); }
@Test public void testIntersect_Doubles() throws Exception { Viewport a = new Viewport(30, 100, 2, 2); Viewport b = new Viewport(31, 101, 2, 2); Viewport northeast = new Viewport(31, 101, 1, 1); boolean intersected = a.intersect(b.x, b.y, b.width, b.height); assertTrue("intersecting", intersected); assertEquals("intersection", northeast, a); }
@Test public void testIntersect_DoublesEmpty() throws Exception { Viewport a = new Viewport(30, 100, 2, 2); Viewport b = new Viewport(31, 101, 0, 0); boolean aIntersectedB = a.intersect(b.x, b.y, b.width, b.height); boolean bIntersectedA = b.intersect(a.x, a.y, a.width, a.height); assertFalse("a intersecting b", aIntersectedB); assertFalse("b intersecting a", bIntersectedA); }
@Test public void testIntersect_DoublesInside() throws Exception { Viewport a = new Viewport(30, 100, 3, 3); Viewport inside = new Viewport(31, 101, 1, 1); boolean intersected = a.intersect(inside.x, inside.y, inside.width, inside.height); assertTrue("interesecting", intersected); assertEquals("inside, intersection is interior sector", inside, a); }
@Test public void testIntersect_DoublesEast() throws Exception { Viewport a = new Viewport(30, 100, 3, 3); Viewport east = new Viewport(31, 102, 1, 2); Viewport expected = new Viewport(31, 102, 1, 1); boolean intersected = a.intersect(east.x, east.y, east.width, east.height); assertTrue("overlapping", intersected); assertEquals("intersection", a, expected); }
@Test public void testIntersect_DoublesWest() throws Exception { Viewport a = new Viewport(30, 100, 3, 3); Viewport west = new Viewport(31, 99, 1, 2); Viewport expected = new Viewport(31, 100, 1, 1); boolean intersected = a.intersect(west.x, west.y, west.width, west.height); assertTrue("overlapping", intersected); assertEquals("intersection", a, expected); }
@Test public void testIntersect_DoublesNorth() throws Exception { Viewport a = new Viewport(30, 100, 3, 3); Viewport north = new Viewport(32, 101, 2, 1); Viewport expected = new Viewport(32, 101, 1, 1); boolean intersected = a.intersect(north.x, north.y, north.width, north.height); assertTrue("overlapping", intersected); assertEquals("intersection", a, expected); }
@Test public void testIntersect_DoublesSouth() 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.x, south.y, south.width, south.height); assertTrue("overlapping", intersected); assertEquals("intersection", a, expected); }
@Test public void testIntersect_DoublesAdjacentEast() throws Exception { Viewport a = new Viewport(30, 100, 3, 3); Viewport adjacentEast = new Viewport(31, 103, 1, 1); Viewport copy = new Viewport(a); boolean intersected = a.intersect(adjacentEast.x, adjacentEast.y, adjacentEast.width, adjacentEast.height); assertFalse("adjacent, no intersection", intersected); assertEquals("adjacent, no changed", a, copy); }
@Test public void testIntersect_DoublesAdjacentWest() throws Exception { Viewport a = new Viewport(30, 100, 3, 3); Viewport adjacentWest = new Viewport(31, 99, 1, 1); Viewport copy = new Viewport(a); boolean intersected = a.intersect(adjacentWest.x, adjacentWest.y, adjacentWest.width, adjacentWest.height); assertFalse("adjacent, no intersection", intersected); assertEquals("adjacent, no changed", a, copy); }
@Test public void testIntersect_DoublesAdjacentNorth() throws Exception { Viewport a = new Viewport(30, 100, 3, 3); Viewport adjacentNorth = new Viewport(33, 101, 1, 1); Viewport copy = new Viewport(a); boolean intersected = a.intersect(adjacentNorth.x, adjacentNorth.y, adjacentNorth.width, adjacentNorth.height); assertFalse("adjacent, no intersection", intersected); assertEquals("adjacent, no changed", a, copy); }
@Test public void testIntersect_DoublesAdjacentSouth() throws Exception { Viewport a = new Viewport(30, 100, 3, 3); Viewport adjacentSouth = new Viewport(29, 101, 1, 1); Viewport copy = new Viewport(a); boolean intersected = a.intersect(adjacentSouth.x, adjacentSouth.y, adjacentSouth.width, adjacentSouth.height); assertFalse("adjacent, no intersection", intersected); assertEquals("adjacent, no changed", a, copy); }