TheAlgorithms / Java

All Algorithms implemented in Java
MIT License
60.07k stars 19.39k forks source link

[FEATURE REQUEST] Add Line Clipping Algorithms: Cohen-Sutherland, Midpoint Subdivision, Liang-Barsky #5472

Closed ShikariSohan closed 1 month ago

ShikariSohan commented 1 month ago

What would you like to Propose?

I propose implementing three line clipping algorithms: Cohen-Sutherland, Midpoint Subdivision, and Liang-Barsky. These algorithms are essential for performing efficient line clipping in 2D graphics. They enable the clipping of lines within a rectangular clipping window or viewport in computer graphics applications. Each algorithm has its approach to solving the problem and would be useful for different scenarios in graphical rendering.

Issue details

Problem Statement:

The goal is to implement efficient line clipping algorithms to determine the intersection of lines with a rectangular clipping window. The following algorithms will be implemented:

  1. Cohen-Sutherland Algorithm: This algorithm categorizes points with outcodes, allowing for fast rejection or acceptance of lines based on their positions relative to the clipping window.

  2. Midpoint Subdivision Algorithm: This algorithm recursively divides lines into segments, checking if they lie within the clipping window to determine if they should be clipped or not.

  3. Liang-Barsky Algorithm: This algorithm employs parametric equations to find intersection points of lines with the clipping window, enabling precise clipping operations.


Benefits:


Implementation:


Sample Use Case (Test Scenario):

The following use case demonstrates how the algorithms will operate:


public class LineClipping {

    // Clipping methods for Cohen-Sutherland, Midpoint Subdivision, and Liang-Barsky
    public static Line cohenSutherlandClip(Line line, int xMin, int yMin, int xMax, int yMax) {
        // Implementation here
        return clippedLine;  // Returns the clipped line or the top corner point if completely outside
    }

    public static Line midpointSubdivisionClip(Line line, int xMin, int yMin, int xMax, int yMax) {
        // Implementation here
        return clippedLine;  // Returns the clipped line or the top corner point if completely outside
    }

    public static Line liangBarskyClip(Line line, int xMin, int yMin, int xMax, int yMax) {
        // Implementation here
        return clippedLine;  // Returns the clipped line or the top corner point if completely outside
    }
}

// Sample lines to be clipped
Line line1 = new Line(50, 50, 350, 350);  // Partially outside
Line line2 = new Line(150, 150, 250, 250);  // Completely inside
Line line3 = new Line(400, 400, 450, 450);  // Completely outside

// Define clipping window
int xMin = 100, yMin = 100, xMax = 300, yMax = 300;

// Test the algorithms
Line clipped1 = LineClipping.cohenSutherlandClip(line1, xMin, yMin, xMax, yMax);
Line clipped2 = LineClipping.midpointSubdivisionClip(line2, xMin, yMin, xMax, yMax);
Line clipped3 = LineClipping.liangBarskyClip(line3, xMin, yMin, xMax, yMax);

// Expected Output
// For lines that are partially or completely within the clipping rectangle, the clipped lines will be returned.
// For completely outside lines, the method will return the top corner point of the clipping window.

### Additional Information

_No response_
TarunVishwakarma1 commented 1 month ago

Are you working on this ?

shivam8112005 commented 1 month ago

is anyone working on this?

TarunVishwakarma1 commented 1 month ago

I am 😃

ShikariSohan commented 1 month ago

@TarunVishwakarma1 I wanna work on this so I created this issue. If you wanna join lets collab here.

TarunVishwakarma1 commented 1 month ago

@ShikariSohan Yes sure works for me, DM me on discord - godxassassin.

shivammm21 commented 1 month ago

I wanna work on this assign to me..

siriak commented 1 month ago

@ShikariSohan the line clipping algorithms look interesting, feel free to create a PR to add them to the repository!

ShikariSohan commented 1 month ago

@siriak I will soon create a PR for this. @TarunVishwakarma1 If you wanna tag along knock me in discord - ShikariSohan