-
Notifications
You must be signed in to change notification settings - Fork 433
/
Copy pathTestCohenSutherland.java
102 lines (88 loc) · 3.3 KB
/
TestCohenSutherland.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package technology.tabula;
import org.junit.Before;
import org.junit.Test;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import static org.junit.Assert.*;
public class TestCohenSutherland {
private Rectangle2D clipWindow;
private CohenSutherlandClipping algorithm;
private static final double DELTA = 0.001;
@Before
public void set() {
clipWindow = new Rectangle(10, 10, 50, 50);
algorithm = new CohenSutherlandClipping(clipWindow);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
// TODO: How to parameterize the tests?
@Test
public void theLineIsCompletelyInside() {
Line2D.Float line = new Line2D.Float(20, 20, 30, 30);
assertTrue(algorithm.clip(line));
assertEquals(20, line.x1, DELTA);
assertEquals(20, line.y1, DELTA);
assertEquals(30, line.x2, DELTA);
assertEquals(30, line.y2, DELTA);
}
@Test
public void theLineIsCompletelyOnTheLeft() {
float x1 = 3, y1 = 13, x2 = 6, y2 = 16;
Line2D.Float line = new Line2D.Float(x1, y1, x2, y2);
assertFalse(algorithm.clip(line));
assertEquals(x1, line.x1, DELTA);
assertEquals(y1, line.y1, DELTA);
assertEquals(x2, line.x2, DELTA);
assertEquals(y2, line.y2, DELTA);
}
@Test
public void theLineIsCompletelyOnTheUp() {
float x1 = 15, y1 = 5, x2 = 25, y2 = 2;
Line2D.Float line = new Line2D.Float(x1, y1, x2, y2);
assertFalse(algorithm.clip(line));
assertEquals(x1, line.x1, DELTA);
assertEquals(y1, line.y1, DELTA);
assertEquals(x2, line.x2, DELTA);
assertEquals(y2, line.y2, DELTA);
}
@Test
public void theLineIsCompletelyOnTheRight() {
float x1 = 65, y1 = 15, x2 = 70, y2 = 20;
Line2D.Float line = new Line2D.Float(x1, y1, x2, y2);
assertFalse(algorithm.clip(line));
assertEquals(x1, line.x1, DELTA);
assertEquals(y1, line.y1, DELTA);
assertEquals(x2, line.x2, DELTA);
assertEquals(y2, line.y2, DELTA);
}
@Test
public void theLineIsCompletelyOnTheBottom() {
float x1 = 15, y1 = 65, x2 = 25, y2 = 70;
Line2D.Float line = new Line2D.Float(x1, y1, x2, y2);
assertFalse(algorithm.clip(line));
assertEquals(x1, line.x1, DELTA);
assertEquals(y1, line.y1, DELTA);
assertEquals(x2, line.x2, DELTA);
assertEquals(y2, line.y2, DELTA);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
@Test
public void lineCrossesTopLeftCorner() {
float x1 = 5, y1 = 25, x2 = 25, y2 = 5;
Line2D.Float line = new Line2D.Float(x1, y1, x2, y2);
assertTrue(algorithm.clip(line));
assertEquals(10, line.x1, DELTA);
assertEquals(20, line.y1, DELTA);
assertEquals(20, line.x2, DELTA);
assertEquals(10, line.y2, DELTA);
}
@Test
public void lineCrossesPartiallyTopLeftCorner() {
float x1 = 15, y1 = 15, x2 = 25, y2 = 5;
Line2D.Float line = new Line2D.Float(x1, y1, x2, y2);
assertTrue(algorithm.clip(line));
assertEquals(x1, line.x1, DELTA);
assertEquals(y1, line.y1, DELTA);
assertEquals(20, line.x2, DELTA);
assertEquals(10, line.y2, DELTA);
}
}