Clipping & Windowing Algorithms - Cohen-Sutherland - CSUCODE - Shoolini U

Clipping (Windowing)

Clipping and Windowing in Computer Graphics Digital Art

6. Clipping (Windowing)

Clipping is the process of removing parts of graphical objects that lie outside a specified visible region. This visible region is called the clipping window. Only the portion of objects inside the window is sent further in the graphics pipeline.

Conceptually, clipping answers one question: Which part of the object can the viewer actually see?

What is the main purpose of Clipping?

A: To identify and discard parts of objects that lie outside the visible region (window) to save processing power.

6.1 Definition: Windowing

Windowing defines a rectangular region in world or viewing coordinates that represents the visible area. Anything outside this window is invisible and must be discarded.

Clipping is performed before mapping the window to the viewport to avoid unnecessary computation.

What is the difference between a Window and a Viewport?

A: A Window is the selected region in World Coordinates, while a Viewport is the area on the Display Device where coordinates are mapped.

6.2 Cohen–Sutherland Line Clipping Algorithm

This is an efficient, region-code–based algorithm for clipping a line against a rectangular window. It avoids unnecessary intersection calculations by quickly classifying lines.

6.2.1 Region Codes (TBRL – 4 bits)

The 2D plane around the window is divided into 9 regions. Each endpoint of the line is assigned a 4-bit code indicating its position relative to the window.

Inside the window: 0000

If a point has a region code of 1010, where is it located?

A: Top-Right (Top=1000 OR Right=0010).

6.2.2 Trivial Accept and Trivial Reject

These tests eliminate most lines without intersection computation.

Only lines that are neither trivially accepted nor rejected require intersection calculation.

            flowchart TD
                Start{Check Line P1-P2} --> Codes[Assign Region Codes]
                Codes --> Accept{P1 & P2 == 0000?}
                Accept -- Yes --> Draw[Trivial Accept: Draw]
                Accept -- No --> Reject{P1 & P2 != 0?}
                Reject -- Yes --> Discard[Trivial Reject: Discard]
                Reject -- No --> Clip[Calculate Intersection]
                Clip --> Codes
            
If the bitwise AND of two endpoint codes is non-zero, what does it conclude?

A: The line is Completely Outside (Trivial Reject) and can be discarded immediately.

6.2.3 Intersection Calculations

For partially visible lines, intersections are computed with window boundaries using the parametric form of a line.

Line equation:

$$x = x_1 + t(x_2 - x_1), \quad y = y_1 + t(y_2 - y_1)$$

For example, intersection with left boundary $x = xmin$:

$$t = \frac{x_{min} - x_1}{x_2 - x_1}$$

The endpoint outside the window is replaced by the intersection point, and the process repeats until accepted or rejected.

6.2.x Solved Examples: Cohen–Sutherland Line Clipping

The following problems are solved step-by-step using the Cohen–Sutherland Line Clipping Algorithm. Each solution includes region-code assignment, trivial tests, and exact intersection calculations.

Example 1

Given:

Step 1: Region Codes (TBRL)

Bitwise AND = 0101 & AND 0010 = 0000 → Not trivially rejected

Step 2: Line Equation

Slope:

$$m = \frac{20 - 8}{18 - 2} = \frac{12}{16} = \frac{3}{4}$$

Parametric form:

$$x = 2 + 16t,\quad y = 8 + 12t$$

Step 3: Clip P1 (Bottom y=10)

$$10 = 8 + 12t \Rightarrow t = \frac{1}{6}$$

$$x = 2 + 16\left(\frac{1}{6}\right) = \frac{14}{3} \approx 4.67$$

Still left of window → clip with x=5

$$5 = 2 + 16t \Rightarrow t = \frac{3}{16}$$

$$y = 8 + 12\left(\frac{3}{16}\right) = 10.25$$

New P1 = (5, 10.25)

Step 4: Clip P2 (Right x=15)

$$15 = 2 + 16t \Rightarrow t = \frac{13}{16}$$

$$y = 8 + 12\left(\frac{13}{16}\right) = 17.75$$

New P2 = (15, 17.75)

Final Clipped Line

(5, 10.25) → (15, 17.75)

Example 2

Given:

Step 1: Region Codes

AND = 0000 → proceed

Step 2: Line Equation

$$m = \frac{32 - 2}{16 - 6} = 3$$

$$x = 6 + 10t,\quad y = 2 + 30t$$

Step 3: Clip with Bottom y=6

$$6 = 2 + 30t \Rightarrow t = \frac{2}{15}$$

$$x = 6 + 10\left(\frac{2}{15}\right) = 7.33$$

New P1 = (7.33, 6)

Step 4: Clip with Top y=28

$$28 = 2 + 30t \Rightarrow t = \frac{13}{15}$$

$$x = 6 + 10\left(\frac{13}{15}\right) = 14.67$$

New P2 = (14.67, 28)

Final Clipped Line

(7.33, 6) → (14.67, 28)

Example 3

Given:

Step 1: Region Codes

AND = 0000 → proceed

Step 2: Line Equation

$$m = \frac{15 - 5}{40 - 12} = \frac{10}{28} = \frac{5}{14}$$

$$x = 12 + 28t,\quad y = 5 + 10t$$

Step 3: Clip P1 with Bottom y=10

$$10 = 5 + 10t \Rightarrow t = 0.5$$

$$x = 12 + 28(0.5) = 26$$

New P1 = (26, 10)

Step 4: Clip P2 with Right x=30

$$30 = 12 + 28t \Rightarrow t = \frac{18}{28} = \frac{9}{14}$$

$$y = 5 + 10\left(\frac{9}{14}\right) = 11.43$$

New P2 = (30, 11.43)

Final Clipped Line

(26, 10) → (30, 11.43)

6.3 Mid-Point Subdivision Line Clipping Algorithm

This algorithm repeatedly subdivides a line until visible and invisible segments are separated.

Core idea: If one endpoint is inside and the other outside, split the line at its midpoint and test each half.

6.3.1 Algorithm Logic

This method is conceptually simple and useful for non-rectangular clipping regions, but computationally slower than Cohen–Sutherland.

When does the Mid-Point Subdivision algorithm stop splitting?

A: When the segment is largely reduced to a point where it can be trivially accepted (inside) or trivially rejected (outside).

6.3.x Solved Examples: Mid-Point Subdivision Line Clipping

The following problems are solved using the Mid-Point Subdivision Clipping Algorithm. Each solution explicitly shows region classification, subdivision logic, and final visible segments.

Example 1

Given:

Step 1: Region Classification

One endpoint inside and one outside → subdivision required.

Step 2: First Midpoint

Midpoint M1:

$$(x,y)=\left(\frac{80+260}{2},\frac{120+60}{2}\right)=(170,90)$$

Visible segment lies between A and M1.

Step 3: Second Midpoint

Midpoint M2 between A and M1:

$$(x,y)=\left(\frac{80+170}{2},\frac{120+90}{2}\right)=(125,105)$$

Intersection lies between M2 and M1.

Step 4: Continue Subdivision (Boundary Convergence)

Repeated subdivision converges to intersection with top boundary y=100.

Exact intersection (line equation):

$$y-120 = \frac{60-120}{260-80}(x-80)$$

Solving for y=100 gives:

$$x = 140$$

Final Visible Segment

(80,120) → (140,100)

Example 2

Given:

Step 1: Region Classification

Endpoints lie in different outside regions → subdivision required.

Step 2: First Midpoint

M1:

$$(50,50)$$

Visible portion lies around midpoint.

Step 3: Subdivide Left Segment

Midpoint between (0,0) and (50,50):

$$(25,25)$$

Further subdivision yields intersection at (20,20).

Step 4: Subdivide Right Segment

Midpoint between (50,50) and (100,100):

$$(75,75)$$

Further subdivision yields intersection at (80,80).

Final Visible Segment

(20,20) → (80,80)

Example 3

Given:

Step 1: Region Classification

Line is horizontal and crosses window → subdivision required.

Step 2: First Midpoint

M1:

$$(10,50)$$

Step 3: Subdivide Left Half

Midpoint between (-10,50) and (10,50):

$$(0,50)$$

Step 4: Subdivide Right Half

Midpoint between (10,50) and (30,50):

$$(20,50)$$

Final Visible Segment

(0,50) → (20,50)

6.4 Polygon Clipping (Sutherland–Hodgman Algorithm)

This algorithm clips a polygon against a convex clipping window by processing one window edge at a time.

Instead of clipping the entire polygon at once, the polygon is clipped sequentially against each boundary.

6.4.1 Inside–Outside Test

For each polygon edge and window boundary, vertices are classified as:

6.4.2 Vertex Processing Rules

For each polygon edge from S (start) to E (end):

The output list becomes the input for clipping against the next boundary.

            flowchart LR
                Start((Start)) --> S_In{Start In?}
                S_In -- Yes --> E_In{End In?}
                S_In -- No --> E_In2{End In?}
                E_In -- Yes --> OutE[Output E]
                E_In -- No --> OutI[Output Intersection]
                E_In2 -- Yes --> OutIE[Output Intersect & E]
                E_In2 -- No --> OutN[Output Nothing]
            
In Sutherland-Hodgman, if you move from Inside to Outside, what do you output?

A: Only the Intersection Point.

6.4.3 Key Properties

What is the main constraint of the Sutherland-Hodgman algorithm?

A: It only works correctly with Convex clipping windows.

Clipping & Windowing Wrap-up