Skip to content

Commit 1fe32ae

Browse files
author
Moksedur Rahman Sohan
committed
style: Format code with clang-format for consistency
1 parent c60c1cf commit 1fe32ae

File tree

6 files changed

+77
-36
lines changed

6 files changed

+77
-36
lines changed

src/main/java/com/thealgorithms/lineclipping/CohenSutherland.java

+32-18
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@
2323
public class CohenSutherland {
2424

2525
// 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
3131

3232
// Define the clipping window
33-
double xMin, yMin, xMax, yMax;
33+
double xMin;
34+
double yMin;
35+
double xMax;
36+
double yMax;
3437

3538
public CohenSutherland(double xMin, double yMin, double xMax, double yMax) {
3639
this.xMin = xMin;
@@ -41,24 +44,34 @@ public CohenSutherland(double xMin, double yMin, double xMax, double yMax) {
4144

4245
// Compute the region code for a point (x, y)
4346
private int computeCode(double x, double y) {
44-
int code = INSIDE;
47+
int code = inside;
4548

4649
if (x < xMin) // to the left of rectangle
47-
code |= LEFT;
50+
{
51+
code |= left;
52+
}
4853
else if (x > xMax) // to the right of rectangle
49-
code |= RIGHT;
54+
{
55+
code |= right;
56+
}
5057
if (y < yMin) // below the rectangle
51-
code |= BOTTOM;
58+
{
59+
code |= bottom;
60+
}
5261
else if (y > yMax) // above the rectangle
53-
code |= TOP;
62+
{
63+
code |= top;
64+
}
5465

5566
return code;
5667
}
5768

5869
// Cohen-Sutherland algorithm to return the clipped line
5970
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;
6275

6376
int code1 = computeCode(x1, y1);
6477
int code2 = computeCode(x2, y2);
@@ -74,25 +87,26 @@ public Line cohenSutherlandClip(Line line) {
7487
break;
7588
} else {
7689
// Some segment of the line is inside the rectangle
77-
double x = 0, y = 0;
90+
double x = 0;
91+
double y = 0;
7892

7993
// Pick an endpoint that is outside the rectangle
8094
int codeOut = (code1 != 0) ? code1 : code2;
8195

8296
// Find the intersection point using the line equation
83-
if ((codeOut & TOP) != 0) {
97+
if ((codeOut & top) != 0) {
8498
// Point is above the rectangle
8599
x = x1 + (x2 - x1) * (yMax - y1) / (y2 - y1);
86100
y = yMax;
87-
} else if ((codeOut & BOTTOM) != 0) {
101+
} else if ((codeOut & bottom) != 0) {
88102
// Point is below the rectangle
89103
x = x1 + (x2 - x1) * (yMin - y1) / (y2 - y1);
90104
y = yMin;
91-
} else if ((codeOut & RIGHT) != 0) {
105+
} else if ((codeOut & right) != 0) {
92106
// Point is to the right of the rectangle
93107
y = y1 + (y2 - y1) * (xMax - x1) / (x2 - x1);
94108
x = xMax;
95-
} else if ((codeOut & LEFT) != 0) {
109+
} else if ((codeOut & left) != 0) {
96110
// Point is to the left of the rectangle
97111
y = y1 + (y2 - y1) * (xMin - x1) / (x2 - x1);
98112
x = xMin;

src/main/java/com/thealgorithms/lineclipping/LiangBarsky.java

+18-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
public class LiangBarsky {
2424

2525
// Define the clipping window
26-
private double xMin, xMax, yMin, yMax;
26+
double xMin;
27+
double xMax;
28+
double yMin;
29+
double yMax;
2730

2831
public LiangBarsky(double xMin, double yMin, double xMax, double yMax) {
2932
this.xMin = xMin;
@@ -59,14 +62,23 @@ private double[] clipLine(double[] p, double[] q) {
5962
if (p[i] == 0 && q[i] < 0) {
6063
return null; // Line is outside the boundary
6164
} else if (p[i] < 0) {
62-
if (t > t1) return null; // Line is outside
63-
if (t > t0) t0 = t; // Update t0
65+
if (t > t1) {
66+
return null;
67+
} // Line is outside
68+
if (t > t0) {
69+
t0 = t;
70+
} // Update t0
6471
} else if (p[i] > 0) {
65-
if (t < t0) return null; // Line is outside
66-
if (t < t1) t1 = t; // Update t1
72+
if (t < t0) {
73+
return null;
74+
} // Line is outside
75+
if (t < t1) {
76+
t1 = t;
77+
} // Update t1
6778
}
6879
}
69-
return new double[]{t0, t1}; // Return valid t0 and t1
80+
81+
return new double[] {t0, t1}; // Return valid t0 and t1
7082
}
7183

7284
// calculate the clipped line based on t0 and t1

src/main/java/com/thealgorithms/lineclipping/utils/Line.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
*/
99
public class Line {
1010

11-
public Point start, end;
11+
public Point start;
12+
public Point end;
1213

1314
public Line() {
1415
}
@@ -20,8 +21,13 @@ public Line(Point start, Point end) {
2021

2122
@Override
2223
public boolean equals(Object o) {
23-
if (this == o) return true;
24-
if (!(o instanceof Line line)) return false;
24+
if (this == o) {
25+
return true;
26+
}
27+
if (!(o instanceof Line line)) {
28+
return false;
29+
}
30+
2531
return Objects.equals(start, line.start) && Objects.equals(end, line.end);
2632
}
2733

src/main/java/com/thealgorithms/lineclipping/utils/Point.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
*/
99
public class Point {
1010

11-
public double x, y;
11+
public double x;
12+
public double y;
1213

1314
public Point() {
1415
}
@@ -20,8 +21,13 @@ public Point(double x, double y) {
2021

2122
@Override
2223
public boolean equals(Object o) {
23-
if (this == o) return true;
24-
if (!(o instanceof Point point)) return false;
24+
if (this == o) {
25+
return true;
26+
}
27+
if (!(o instanceof Point point)) {
28+
return false;
29+
}
30+
2531
return Double.compare(x, point.x) == 0 && Double.compare(y, point.y) == 0;
2632
}
2733

src/test/java/com/thealgorithms/lineclipping/CohenSutherlandTest.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.thealgorithms.lineclipping;
22

3+
import static org.junit.jupiter.api.Assertions.assertNull;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
37
import com.thealgorithms.lineclipping.utils.Line;
48
import com.thealgorithms.lineclipping.utils.Point;
59
import org.junit.jupiter.api.Test;
610

7-
import static org.junit.jupiter.api.Assertions.*;
8-
911
/**
1012
* @author shikarisohan
1113
* @since 10/4/24

src/test/java/com/thealgorithms/lineclipping/LiangBarskyTest.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.thealgorithms.lineclipping;
22

3+
import static org.junit.jupiter.api.Assertions.assertNotNull;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertNull;
6+
37
import com.thealgorithms.lineclipping.utils.Line;
48
import com.thealgorithms.lineclipping.utils.Point;
59
import org.junit.jupiter.api.Test;
610

7-
import static org.junit.jupiter.api.Assertions.*;
8-
911
/**
1012
* @author shikarisohan
1113
* @since 10/5/24
@@ -60,5 +62,4 @@ void testVerticalLineClipping() {
6062
assertNotNull(clippedLine, "Line should not be null.");
6163
assertEquals(expectedClippedLine, clippedLine, "Vertical line should be clipped correctly.");
6264
}
63-
64-
}
65+
}

0 commit comments

Comments
 (0)