23
23
public class CohenSutherland {
24
24
25
25
// Region codes for the 9 regions
26
- final int INSIDE = 0 ; // 0000
27
- final int LEFT = 1 ; // 0001
28
- final int RIGHT = 2 ; // 0010
29
- final int BOTTOM = 4 ; // 0100
30
- final int TOP = 8 ; // 1000
26
+ private final int inside = 0 ; // 0000
27
+ private final int left = 1 ; // 0001
28
+ private final int right = 2 ; // 0010
29
+ private final int bottom = 4 ; // 0100
30
+ private final int top = 8 ; // 1000
31
31
32
32
// Define the clipping window
33
- double xMin , yMin , xMax , yMax ;
33
+ double xMin ;
34
+ double yMin ;
35
+ double xMax ;
36
+ double yMax ;
34
37
35
38
public CohenSutherland (double xMin , double yMin , double xMax , double yMax ) {
36
39
this .xMin = xMin ;
@@ -41,24 +44,34 @@ public CohenSutherland(double xMin, double yMin, double xMax, double yMax) {
41
44
42
45
// Compute the region code for a point (x, y)
43
46
private int computeCode (double x , double y ) {
44
- int code = INSIDE ;
47
+ int code = inside ;
45
48
46
49
if (x < xMin ) // to the left of rectangle
47
- code |= LEFT ;
50
+ {
51
+ code |= left ;
52
+ }
48
53
else if (x > xMax ) // to the right of rectangle
49
- code |= RIGHT ;
54
+ {
55
+ code |= right ;
56
+ }
50
57
if (y < yMin ) // below the rectangle
51
- code |= BOTTOM ;
58
+ {
59
+ code |= bottom ;
60
+ }
52
61
else if (y > yMax ) // above the rectangle
53
- code |= TOP ;
62
+ {
63
+ code |= top ;
64
+ }
54
65
55
66
return code ;
56
67
}
57
68
58
69
// Cohen-Sutherland algorithm to return the clipped line
59
70
public Line cohenSutherlandClip (Line line ) {
60
- double x1 = line .start .x , y1 = line .start .y ;
61
- double x2 = line .end .x , y2 = line .end .y ;
71
+ double x1 = line .start .x ;
72
+ double y1 = line .start .y ;
73
+ double x2 = line .end .x ;
74
+ double y2 = line .end .y ;
62
75
63
76
int code1 = computeCode (x1 , y1 );
64
77
int code2 = computeCode (x2 , y2 );
@@ -74,25 +87,26 @@ public Line cohenSutherlandClip(Line line) {
74
87
break ;
75
88
} else {
76
89
// Some segment of the line is inside the rectangle
77
- double x = 0 , y = 0 ;
90
+ double x = 0 ;
91
+ double y = 0 ;
78
92
79
93
// Pick an endpoint that is outside the rectangle
80
94
int codeOut = (code1 != 0 ) ? code1 : code2 ;
81
95
82
96
// Find the intersection point using the line equation
83
- if ((codeOut & TOP ) != 0 ) {
97
+ if ((codeOut & top ) != 0 ) {
84
98
// Point is above the rectangle
85
99
x = x1 + (x2 - x1 ) * (yMax - y1 ) / (y2 - y1 );
86
100
y = yMax ;
87
- } else if ((codeOut & BOTTOM ) != 0 ) {
101
+ } else if ((codeOut & bottom ) != 0 ) {
88
102
// Point is below the rectangle
89
103
x = x1 + (x2 - x1 ) * (yMin - y1 ) / (y2 - y1 );
90
104
y = yMin ;
91
- } else if ((codeOut & RIGHT ) != 0 ) {
105
+ } else if ((codeOut & right ) != 0 ) {
92
106
// Point is to the right of the rectangle
93
107
y = y1 + (y2 - y1 ) * (xMax - x1 ) / (x2 - x1 );
94
108
x = xMax ;
95
- } else if ((codeOut & LEFT ) != 0 ) {
109
+ } else if ((codeOut & left ) != 0 ) {
96
110
// Point is to the left of the rectangle
97
111
y = y1 + (y2 - y1 ) * (xMin - x1 ) / (x2 - x1 );
98
112
x = xMin ;
0 commit comments