|
| 1 | +package com.thealgorithms.lineclipping; |
| 2 | + |
| 3 | +import com.thealgorithms.lineclipping.utils.Line; |
| 4 | +import com.thealgorithms.lineclipping.utils.Point; |
| 5 | + |
| 6 | +/** |
| 7 | + * @author shikarisohan |
| 8 | + * @since 10/4/24 |
| 9 | + * Cohen-Sutherland Line Clipping Algorithm |
| 10 | + * |
| 11 | + * This algorithm is used to clip a line segment to a rectangular window. |
| 12 | + * It assigns a region code to each endpoint of the line segment, and |
| 13 | + * then efficiently determines whether the line segment is fully inside, |
| 14 | + * fully outside, or partially inside the window. |
| 15 | + * |
| 16 | + * Reference: |
| 17 | + * https://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm |
| 18 | + * |
| 19 | + * Clipping window boundaries are defined as (xMin, yMin) and (xMax, yMax). |
| 20 | + * The algorithm computes the clipped line segment if it's partially or |
| 21 | + * fully inside the clipping window. |
| 22 | + */ |
| 23 | +public class CohenSutherland { |
| 24 | + |
| 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 |
| 31 | + |
| 32 | + // Define the clipping window |
| 33 | + double xMin, yMin, xMax, yMax; |
| 34 | + |
| 35 | + public CohenSutherland(double xMin, double yMin, double xMax, double yMax) { |
| 36 | + this.xMin = xMin; |
| 37 | + this.yMin = yMin; |
| 38 | + this.xMax = xMax; |
| 39 | + this.yMax = yMax; |
| 40 | + } |
| 41 | + |
| 42 | + // Compute the region code for a point (x, y) |
| 43 | + private int computeCode(double x, double y) { |
| 44 | + int code = INSIDE; |
| 45 | + |
| 46 | + if (x < xMin) // to the left of rectangle |
| 47 | + code |= LEFT; |
| 48 | + else if (x > xMax) // to the right of rectangle |
| 49 | + code |= RIGHT; |
| 50 | + if (y < yMin) // below the rectangle |
| 51 | + code |= BOTTOM; |
| 52 | + else if (y > yMax) // above the rectangle |
| 53 | + code |= TOP; |
| 54 | + |
| 55 | + return code; |
| 56 | + } |
| 57 | + |
| 58 | + // Cohen-Sutherland algorithm to return the clipped line |
| 59 | + 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; |
| 62 | + |
| 63 | + int code1 = computeCode(x1, y1); |
| 64 | + int code2 = computeCode(x2, y2); |
| 65 | + boolean accept = false; |
| 66 | + |
| 67 | + while (true) { |
| 68 | + if ((code1 == 0) && (code2 == 0)) { |
| 69 | + // Both points are inside the rectangle |
| 70 | + accept = true; |
| 71 | + break; |
| 72 | + } else if ((code1 & code2) != 0) { |
| 73 | + // Both points are outside the rectangle in the same region |
| 74 | + break; |
| 75 | + } else { |
| 76 | + // Some segment of the line is inside the rectangle |
| 77 | + double x = 0, y = 0; |
| 78 | + |
| 79 | + // Pick an endpoint that is outside the rectangle |
| 80 | + int codeOut = (code1 != 0) ? code1 : code2; |
| 81 | + |
| 82 | + // Find the intersection point using the line equation |
| 83 | + if ((codeOut & TOP) != 0) { |
| 84 | + // Point is above the rectangle |
| 85 | + x = x1 + (x2 - x1) * (yMax - y1) / (y2 - y1); |
| 86 | + y = yMax; |
| 87 | + } else if ((codeOut & BOTTOM) != 0) { |
| 88 | + // Point is below the rectangle |
| 89 | + x = x1 + (x2 - x1) * (yMin - y1) / (y2 - y1); |
| 90 | + y = yMin; |
| 91 | + } else if ((codeOut & RIGHT) != 0) { |
| 92 | + // Point is to the right of the rectangle |
| 93 | + y = y1 + (y2 - y1) * (xMax - x1) / (x2 - x1); |
| 94 | + x = xMax; |
| 95 | + } else if ((codeOut & LEFT) != 0) { |
| 96 | + // Point is to the left of the rectangle |
| 97 | + y = y1 + (y2 - y1) * (xMin - x1) / (x2 - x1); |
| 98 | + x = xMin; |
| 99 | + } |
| 100 | + |
| 101 | + // Replace the point outside the rectangle with the intersection point |
| 102 | + if (codeOut == code1) { |
| 103 | + x1 = x; |
| 104 | + y1 = y; |
| 105 | + code1 = computeCode(x1, y1); |
| 106 | + } else { |
| 107 | + x2 = x; |
| 108 | + y2 = y; |
| 109 | + code2 = computeCode(x2, y2); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (accept) { |
| 115 | + return new Line(new Point(x1, y1), new Point(x2, y2)); |
| 116 | + } else { |
| 117 | + |
| 118 | + return null; // The line is fully rejected |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments