|
| 1 | +package com.thealgorithms.lineclipping; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 6 | + |
| 7 | +import com.thealgorithms.lineclipping.utils.Line; |
| 8 | +import com.thealgorithms.lineclipping.utils.Point; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | + |
| 11 | +/** |
| 12 | + * @author shikarisohan |
| 13 | + * @since 10/4/24 |
| 14 | + */ |
| 15 | +class CohenSutherlandTest { |
| 16 | + |
| 17 | + // Define the clipping window (1.0, 1.0) to (10.0, 10.0) |
| 18 | + CohenSutherland cs = new CohenSutherland(1.0, 1.0, 10.0, 10.0); |
| 19 | + |
| 20 | + @Test |
| 21 | + void testLineCompletelyInside() { |
| 22 | + // Line fully inside the clipping window |
| 23 | + Line line = new Line(new Point(2.0, 2.0), new Point(8.0, 8.0)); |
| 24 | + Line clippedLine = cs.cohenSutherlandClip(line); |
| 25 | + |
| 26 | + assertNotNull(clippedLine, "Line should not be null."); |
| 27 | + assertEquals(line, clippedLine, "Line inside the window should remain unchanged."); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + void testLineCompletelyOutside() { |
| 32 | + // Line completely outside and above the clipping window |
| 33 | + Line line = new Line(new Point(11.0, 12.0), new Point(15.0, 18.0)); |
| 34 | + Line clippedLine = cs.cohenSutherlandClip(line); |
| 35 | + |
| 36 | + assertNull(clippedLine, "Line should be null because it's completely outside."); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void testLinePartiallyInside() { |
| 41 | + // Line partially inside the clipping window |
| 42 | + Line line = new Line(new Point(5.0, 5.0), new Point(12.0, 12.0)); |
| 43 | + Line expectedClippedLine = new Line(new Point(5.0, 5.0), new Point(10.0, 10.0)); // Clipped at (10, 10) |
| 44 | + Line clippedLine = cs.cohenSutherlandClip(line); |
| 45 | + |
| 46 | + assertNotNull(clippedLine, "Line should not be null."); |
| 47 | + assertEquals(expectedClippedLine, clippedLine, "Line should be clipped correctly."); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void testLineOnBoundary() { |
| 52 | + // Line exactly on the boundary of the clipping window |
| 53 | + Line line = new Line(new Point(1.0, 5.0), new Point(10.0, 5.0)); |
| 54 | + Line clippedLine = cs.cohenSutherlandClip(line); |
| 55 | + |
| 56 | + assertNotNull(clippedLine, "Line should not be null."); |
| 57 | + assertEquals(line, clippedLine, "Line on the boundary should remain unchanged."); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void testDiagonalLineThroughClippingWindow() { |
| 62 | + // Diagonal line crossing from outside to outside through the window |
| 63 | + Line line = new Line(new Point(0.0, 0.0), new Point(12.0, 12.0)); |
| 64 | + Line expectedClippedLine = new Line(new Point(1.0, 1.0), new Point(10.0, 10.0)); // Clipped at both boundaries |
| 65 | + Line clippedLine = cs.cohenSutherlandClip(line); |
| 66 | + |
| 67 | + assertNotNull(clippedLine, "Line should not be null."); |
| 68 | + assertEquals(expectedClippedLine, clippedLine, "Diagonal line should be clipped correctly."); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void testVerticalLineClipping() { |
| 73 | + // Vertical line crossing the top and bottom of the clipping window |
| 74 | + Line line = new Line(new Point(5.0, 0.0), new Point(5.0, 12.0)); |
| 75 | + Line expectedClippedLine = new Line(new Point(5.0, 1.0), new Point(5.0, 10.0)); // Clipped at yMin and yMax |
| 76 | + Line clippedLine = cs.cohenSutherlandClip(line); |
| 77 | + |
| 78 | + assertNotNull(clippedLine, "Line should not be null."); |
| 79 | + assertEquals(expectedClippedLine, clippedLine, "Vertical line should be clipped correctly."); |
| 80 | + } |
| 81 | +} |
0 commit comments