Computer GraphicsUnit 610 min read
2D Viewing & Clipping: Windows, Viewports, Cohen-Sutherland, Liang-Barsky
Unit 6 of Computer Graphics covers how to define 2D viewing areas (windows/viewports), clip lines and polygons to visible regions, and implement algorithms like Cohen-Sutherland and Liang-Barsky with step-by-step examples and real-world applications in games, CAD, and UI design.
TAKEAWAYS:
- Viewing transformation maps a world coordinate window to a device coordinate viewport using scaling, translation, and clipping.
- Cohen-Sutherland clips lines by region codes (9-bit binary flags) and trivial accept/reject tests before parametric intersection checks.
- Liang-Barsky improves efficiency by computing intersection parameters directly from line equations and clipping boundaries.
- Polygon clipping (Sutherland-Hodgman) processes edges sequentially, adding/subtracting vertices at intersections.
- Real-world use: Clipping ensures only visible content renders (e.g., Daraz’s product thumbnails, Pathao’s ride boundaries).
- Exam focus: Derive region codes, trace clipping steps, and compare algorithms’ time/space complexity.
1. Viewing in 2D: Windows and Viewports
Key Concepts
- World Coordinate System (WCS): The original coordinate system where objects are defined (e.g., a 2D map of Kathmandu with coordinates in meters).
- Window: A rectangular subset of WCS that defines the visible area (e.g.,
W = [x₁, x₂] × [y₁, y₂]). - Viewport: The corresponding rectangle in device coordinates (e.g., pixels on your screen,
V = [u₁, u₂] × [v₁, v₂]). - Viewing Transformation: Maps the window to the viewport using:
- Scaling: Stretch/shrink the window to fit the viewport.
- Translation: Shift the scaled window to align with the viewport.
Mathematical Formulation
For a point (x, y) in WCS:
- Scale to viewport dimensions:
- Translate to viewport origin:
Example: Daraz Product Thumbnail
- Scenario: Daraz displays product images in a grid. Each product’s bounding box (window) in its original resolution must be clipped to a fixed viewport (e.g., 150×150 pixels).
- Steps:
- Define window:
W = [0, 1000] × [0, 800](original image dimensions). - Define viewport:
V = [0, 150] × [0, 150](thumbnail size). - Apply viewing transformation to clip and scale the image.
- Define window:
Mermaid Diagram: Viewing Pipeline
flowchart LR
A["World Coordinate System\n(x, y)"] -->|"Window Selection"| B["Window\nW = [x₁,x₂]×[y₁,y₂]"]
B -->|"Scaling"| C["Scaled Coordinates\n(x', y')"]
C -->|"Translation"| D["Viewport\nV = [u₁,u₂]×[v₁,v₂]"]
D -->|"Rasterization"| E["Device Screen\n(u, v)"]2. Line Clipping: Cohen-Sutherland Algorithm
Why Clipping?
- Problem: Lines or polygons may extend outside the viewport. Only visible portions should be rendered.
- Example: In a game like Angry Birds, birds’ trajectories are clipped to the screen edges.
Region Codes
Each line endpoint is assigned a 9-bit region code (3 bits for left/right, 3 for top/bottom, 3 for inside/outside):
- Bit 1-3: Left (
001), Right (010), Inside (000). - Bit 4-6: Top (
100), Bottom (001), Inside (000). - Bit 7-9: Outside (
100), Inside (000).
Formula:
For a line from (x₁, y₁) to (x₂, y₂) and window [x₁, x₂] × [y₁, y₂]:
Algorithm Steps
- Compute region codes for both endpoints (
P₁andP₂). - Trivial Accept: If both codes are
000, accept the line. - Trivial Reject: If
P₁ ∧ P₂ ≠ 0, reject the line (both outside same region). - Clip: Find intersection of the line with the window boundary and compute the new endpoint’s region code. Repeat until trivial accept/reject.
Worked Example: Clipping a Line
Line: (x₁, y₁) = (50, 50), (x₂, y₂) = (200, 200)
Window: [100, 300] × [100, 300]
- Region Codes:
P₁:001(left) +001(bottom) =001001001(binary) →73(decimal).P₂:000(inside).
- Trivial Reject?
73 ∧ 0 = 0→ No. - Clip to Left Edge (
x = 100):- Parametric equation:
x = x₁ + t(x₂ - x₁),y = y₁ + t(y₂ - y₁). - Solve
100 = 50 + t(200 - 50)→t = 0.75. - New point:
(100, 50 + 0.75(150)) = (100, 162.5). - New code:
000(inside).
- Parametric equation:
- Accept: Both endpoints now inside → Clip to
(100, 162.5)to(200, 200).
Visual: Cohen-Sutherland Clipping
graph TD
A["Window: [100,300]×[100,300]"] --> B["Line: (50,50)→(200,200)"]
B --> C["Clip to x=100\nNew point: (100,162.5)"]
C --> D["Accept clipped line"]Advantages/Disadvantages
| Cohen-Sutherland | Liang-Barsky (Next Section) |
|---|---|
| ✅ Simple to implement | ✅ Faster (no region codes) |
| ✅ Works for any rectangle | ✅ Fewer iterations |
| ❌ Up to 4 iterations | ❌ Slightly complex math |
| ❌ Region code overhead | ❌ Requires parametric equations |
3. Line Clipping: Liang-Barsky Algorithm
Improvement Over Cohen-Sutherland
- Uses parametric equations to compute intersections directly.
- Avoids region codes → faster for complex scenes (e.g., 3D game engines).
Parametric Line Equation
A line from (x₁, y₁) to (x₂, y₂) can be written as:
where t is the parameter.
Clipping Conditions
For window [x₁, x₂] × [y₁, y₂], compute:
- Left/Right:
If
p > 0:t₀ = q / p, elset₀ = q / p(sign matters). - Bottom/Top:
Similarly compute
t₀.
Algorithm Steps
- Initialize
t₀ = 0,t₁ = 1. - For each boundary (left, right, bottom, top):
- Compute
tfor intersection. - Update
t₀ = max(t₀, t)andt₁ = min(t₁, t). - If
t₀ > t₁, reject the line.
- Compute
- If
t₀ ≤ t₁, accept the clipped line fromt₀tot₁.
Worked Example: Liang-Barsky
Line: (50, 50) to (200, 200)
Window: [100, 300] × [100, 300]
Left Edge (
x = 100):p = 200 - 50 = 150,q = 50 - 100 = -50.t = q / p = -50 / 150 ≈ -0.333→ Reject ift < 0(but we takemax(t₀, t)).- Update
t₀ = max(0, -0.333) = 0,t₁ = min(1, ∞) = 1(no change).
Right Edge (
x = 300):p = 150,q = 50 - 300 = -250.t = -250 / 150 ≈ -1.666→t₀remains0.
Bottom Edge (
y = 100):p = 150,q = 50 - 100 = -50.t = -50 / 150 ≈ -0.333→t₀ = 0.
Top Edge (
y = 300):p = 150,q = 50 - 300 = -250.t = -250 / 150 ≈ -1.666→t₀ = 0.
Result: t₀ = 0.75 (from left edge), t₁ = 1.
- Clipped line:
(100, 162.5)to(200, 200).
Visual: Liang-Barsky Intersection
graph TD
A["Line: (50,50)→(200,200)"] --> B["Intersection at t=0.75\n(100,162.5)"]
B --> C["Clipped line: (100,162.5)→(200,200)"]4. Polygon Clipping: Sutherland-Hodgman Algorithm
Problem
Clipping polygons (e.g., a country’s border on a map) requires handling multiple edges.
Steps
- Clip the polygon against one edge of the window at a time (left, right, bottom, top).
- For each edge, process vertices sequentially:
- If a vertex is inside, add it to the output list.
- If a vertex is outside, compute its intersection with the edge and add the intersection point.
- If both vertices are outside, skip.
Worked Example: Clipping a Triangle
Polygon: (50, 50), (200, 50), (100, 200)
Window: [100, 300] × [100, 300]
Clip against Left Edge (
x = 100):- Start with
(50, 50)(outside). - Intersection with
(200, 50):t = (100 - 50)/(200 - 50) = 0.5→(100, 50). (100, 200)is inside → add to output.- Result:
(100, 50),(100, 200).
- Start with
Clip against Bottom Edge (
y = 100):(100, 50)is outside → intersection with(100, 200):t = (100 - 50)/(200 - 50) = 0.5→(100, 100).(100, 200)is inside → add.- Result:
(100, 100),(100, 200).
Final Clipped Polygon:
(100, 100),(100, 200).
Visual: Polygon Clipping
graph TD
A["Original Triangle"] --> B["Clip Left Edge\n(100,50)-(100,200)"]
B --> C["Clip Bottom Edge\n(100,100)-(100,200)"]In the Real World
Pathao Ride Boundaries
- Idea: Viewport Clipping
- How: Pathao’s app shows ride routes only within the map’s visible area. The driver’s path is clipped to the viewport to avoid rendering off-screen segments, improving performance.
Daraz Product Thumbnails
- Idea: Line and Polygon Clipping
- How: Product images are clipped to fixed-size thumbnails. The algorithm ensures only the visible portion of each product’s bounding box is rendered, saving memory and rendering time.
NTC Traffic Simulation
- Idea: Sutherland-Hodgman Polygon Clipping
- How: Simulating vehicle paths on Kathmandu’s roads involves clipping vehicle polygons to road boundaries. This ensures only valid paths (within road limits) are rendered in traffic models.
Exam Tip
- Derive Region Codes: For Cohen-Sutherland, always show the 9-bit binary code for each endpoint.
- Trace Steps: In Liang-Barsky, explicitly compute
tfor each boundary and updatet₀/t₁. - Polygon Clipping: For Sutherland-Hodgman, process edges in order (left → right → bottom → top) and show intermediate vertex lists.
- Common Pitfalls:
- Forgetting to handle
t < 0ort > 1in Liang-Barsky. - Incorrectly computing intersections in polygon clipping (use parametric equations).
- Forgetting to handle
- Compare Algorithms: In short-answer questions, contrast Cohen-Sutherland (region codes) vs. Liang-Barsky (parametric math).
Key Formula Summary:
| Concept | Formula/Method |
|---|---|
| Viewing Transformation | |
| Cohen-Sutherland Code | Bitwise flags for left/right/top/bottom |
Liang-Barsky t |
(parametric) |
| Polygon Clipping | Sutherland-Hodgman: Edge-by-edge intersection |
Based on the PU BE Computer (PU) syllabus for Computer Graphics, unit 6.
Discussion
Loading…