Skip to content

Commit

Permalink
style: Format code with clang-format for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
Moksedur Rahman Sohan committed Oct 5, 2024
1 parent c60c1cf commit 1fe32ae
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 36 deletions.
50 changes: 32 additions & 18 deletions src/main/java/com/thealgorithms/lineclipping/CohenSutherland.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@
public class CohenSutherland {

// Region codes for the 9 regions
final int INSIDE = 0; // 0000
final int LEFT = 1; // 0001
final int RIGHT = 2; // 0010
final int BOTTOM = 4; // 0100
final int TOP = 8; // 1000
private final int inside = 0; // 0000
private final int left = 1; // 0001
private final int right = 2; // 0010
private final int bottom = 4; // 0100
private final int top = 8; // 1000

// Define the clipping window
double xMin, yMin, xMax, yMax;
double xMin;
double yMin;
double xMax;
double yMax;

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

// Compute the region code for a point (x, y)
private int computeCode(double x, double y) {
int code = INSIDE;
int code = inside;

if (x < xMin) // to the left of rectangle
code |= LEFT;
{
code |= left;
}
else if (x > xMax) // to the right of rectangle
code |= RIGHT;
{
code |= right;
}
if (y < yMin) // below the rectangle
code |= BOTTOM;
{
code |= bottom;
}
else if (y > yMax) // above the rectangle
code |= TOP;
{
code |= top;
}

return code;
}

// Cohen-Sutherland algorithm to return the clipped line
public Line cohenSutherlandClip(Line line) {
double x1 = line.start.x, y1 = line.start.y;
double x2 = line.end.x, y2 = line.end.y;
double x1 = line.start.x;
double y1 = line.start.y;
double x2 = line.end.x;
double y2 = line.end.y;

int code1 = computeCode(x1, y1);
int code2 = computeCode(x2, y2);
Expand All @@ -74,25 +87,26 @@ public Line cohenSutherlandClip(Line line) {
break;
} else {
// Some segment of the line is inside the rectangle
double x = 0, y = 0;
double x = 0;
double y = 0;

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

// Find the intersection point using the line equation
if ((codeOut & TOP) != 0) {
if ((codeOut & top) != 0) {
// Point is above the rectangle
x = x1 + (x2 - x1) * (yMax - y1) / (y2 - y1);
y = yMax;
} else if ((codeOut & BOTTOM) != 0) {
} else if ((codeOut & bottom) != 0) {
// Point is below the rectangle
x = x1 + (x2 - x1) * (yMin - y1) / (y2 - y1);
y = yMin;
} else if ((codeOut & RIGHT) != 0) {
} else if ((codeOut & right) != 0) {
// Point is to the right of the rectangle
y = y1 + (y2 - y1) * (xMax - x1) / (x2 - x1);
x = xMax;
} else if ((codeOut & LEFT) != 0) {
} else if ((codeOut & left) != 0) {
// Point is to the left of the rectangle
y = y1 + (y2 - y1) * (xMin - x1) / (x2 - x1);
x = xMin;
Expand Down
24 changes: 18 additions & 6 deletions src/main/java/com/thealgorithms/lineclipping/LiangBarsky.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
public class LiangBarsky {

// Define the clipping window
private double xMin, xMax, yMin, yMax;
double xMin;
double xMax;
double yMin;
double yMax;

public LiangBarsky(double xMin, double yMin, double xMax, double yMax) {
this.xMin = xMin;
Expand Down Expand Up @@ -59,14 +62,23 @@ private double[] clipLine(double[] p, double[] q) {
if (p[i] == 0 && q[i] < 0) {
return null; // Line is outside the boundary
} else if (p[i] < 0) {
if (t > t1) return null; // Line is outside
if (t > t0) t0 = t; // Update t0
if (t > t1) {
return null;
} // Line is outside
if (t > t0) {
t0 = t;
} // Update t0
} else if (p[i] > 0) {
if (t < t0) return null; // Line is outside
if (t < t1) t1 = t; // Update t1
if (t < t0) {
return null;
} // Line is outside
if (t < t1) {
t1 = t;
} // Update t1
}
}
return new double[]{t0, t1}; // Return valid t0 and t1

return new double[] {t0, t1}; // Return valid t0 and t1
}

// calculate the clipped line based on t0 and t1
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/thealgorithms/lineclipping/utils/Line.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
*/
public class Line {

public Point start, end;
public Point start;
public Point end;

public Line() {
}
Expand All @@ -20,8 +21,13 @@ public Line(Point start, Point end) {

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Line line)) return false;
if (this == o) {
return true;
}
if (!(o instanceof Line line)) {
return false;
}

return Objects.equals(start, line.start) && Objects.equals(end, line.end);
}

Expand Down
12 changes: 9 additions & 3 deletions src/main/java/com/thealgorithms/lineclipping/utils/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
*/
public class Point {

public double x, y;
public double x;
public double y;

public Point() {
}
Expand All @@ -20,8 +21,13 @@ public Point(double x, double y) {

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Point point)) return false;
if (this == o) {
return true;
}
if (!(o instanceof Point point)) {
return false;
}

return Double.compare(x, point.x) == 0 && Double.compare(y, point.y) == 0;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.thealgorithms.lineclipping;

import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.thealgorithms.lineclipping.utils.Line;
import com.thealgorithms.lineclipping.utils.Point;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

/**
* @author shikarisohan
* @since 10/4/24
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.thealgorithms.lineclipping;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
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;

import static org.junit.jupiter.api.Assertions.*;

/**
* @author shikarisohan
* @since 10/5/24
Expand Down Expand Up @@ -60,5 +62,4 @@ void testVerticalLineClipping() {
assertNotNull(clippedLine, "Line should not be null.");
assertEquals(expectedClippedLine, clippedLine, "Vertical line should be clipped correctly.");
}

}
}

0 comments on commit 1fe32ae

Please sign in to comment.