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