CSC214 · 2079 · 10 marks
Write the algorithm for Cohen-Sutherland Line clipping. Clip the polygon A(100,150), B(200,250) and C(300,200) with the clipping window defined by the coordinates (100,300), (300,300) and (200,100) using Sutherland Hodgeman Polygon Clipping algorithm.
Write the algorithm for Cohen-Sutherland Line clipping. Clip the polygon A(100,150), B(200,250) and C(300,200) with the clipping window defined by the coordinates (100,300), (300,300) and (200,100) using Sutherland Hodgeman Polygon Clipping algorithm.[10]
Cohen-Sutherland Line Clipping Algorithm and Sutherland-Hodgeman Polygon Clipping
Given
Polygon vertices:
- $A(100, 150)$
- $B(200, 250)$
- $C(300, 200)$
Clipping window (triangular) vertices:
- $W_1(100, 300)$
- $W_2(300, 300)$
- $W_3(200, 100)$
Task: State Cohen-Sutherland line clipping algorithm; clip triangle ABC against triangular window using Sutherland-Hodgeman.
Solution
Part 1: Cohen-Sutherland Line Clipping Algorithm
The clipping window divides the plane into 9 regions, each with a 4-bit outcode:
1001 | 1000 | 1010
-----+------+-----
0001 | 0000 | 0010
-----+------+-----
0101 | 0100 | 0110
Bit assignments (from left): Top ($y>y_{max}$), Bottom ($y<y_{min}$), Right ($x>x_{max}$), Left ($x<x_{min}$).
Algorithm:
- For endpoints $P_1(x_1,y_1)$, $P_2(x_2,y_2)$, compute their 4-bit outcodes.
- Trivial accept: if $\text{code}(P_1)\ \text{OR}\ \text{code}(P_2)=0000$, draw the whole line.
- Trivial reject: if $\text{code}(P_1)\ \text{AND}\ \text{code}(P_2)\neq 0000$, discard the line.
- Otherwise: pick an endpoint with a nonzero outcode, compute its intersection with the corresponding window boundary:
- Left: $y=y_1+m(x_{min}-x_1),\ x=x_{min}$
- Right: $y=y_1+m(x_{max}-x_1),\ x=x_{max}$
- Bottom: $x=x_1+\tfrac{1}{m}(y_{min}-y_1),\ y=y_{min}$
- Top: $x=x_1+\tfrac{1}{m}(y_{max}-y_1),\ y=y_{max}$
- Replace the outside endpoint with the intersection point, recompute its outcode, and repeat from step 2 until accept or reject.
Part 2: Sutherland-Hodgeman Polygon Clipping
Window edges (traversed so interior lies to the left):
Verify orientation. Vertices $W_1(100,300),W_2(300,300),W_3(200,100)$. The centroid is $\left(\tfrac{100+300+200}{3},\tfrac{300+300+100}{3}\right)=(200,\ 233.3)$.
Inside test: For directed edge $W_a\to W_b$, point $P$ is inside if $$f(P)=(x_b-x_a)(y_p-y_a)-(y_b-y_a)(x_p-x_a)\ge 0.$$
Check orientation using centroid for each edge; adjust sign so centroid is inside.
Edge 1: $W_1(100,300)\to W_2(300,300)$
$f(P)=(300-100)(y_p-300)-(300-300)(x_p-100)=200(y_p-300)$
Centroid: $200(233.3-300)<0$. So centroid gives negative; inside means $f(P)\le 0$, i.e. $y_p\le 300$.
| Vertex | $y_p$ | $y\le300$? |
|---|---|---|
| A(100,150) | 150 | Inside |
| B(200,250) | 250 | Inside |
| C(300,200) | 200 | Inside |
All inside. Output list I: A(100,150), B(200,250), C(300,200)
Edge 2: $W_2(300,300)\to W_3(200,100)$
$f(P)=(200-300)(y_p-300)-(100-300)(x_p-300)$ $=-100(y_p-300)+200(x_p-300)$
Centroid $(200,233.3)$: $-100(-66.7)+200(-100)=6670-20000=-13330<0$. So inside means $f(P)\le 0$.
Compute for each vertex of list I:
- A(100,150): $-100(150-300)+200(100-300)=15000-40000=-25000\le0$ → Inside
- B(200,250): $-100(250-300)+200(200-300)=5000-20000=-15000\le0$ → Inside
- C(300,200): $-100(200-300)+200(300-300)=10000+0=+10000>0$ → Outside
Process pairs of list I (A→B→C→A):
A(in)→B(in): add B. B(in)→C(out): add intersection of BC with edge 2.
Line B(200,250)→C(300,200): parametric $x=200+100t,\ y=250-50t$. Edge 2 line: through (300,300) and (200,100): direction $(-100,-200)$, i.e. slope $2$, line $y-300=2(x-300)\Rightarrow y=2x-300$.
Substitute: $250-50t=2(200+100t)-300=100+200t$ $250-50t=100+200t\Rightarrow150=250t\Rightarrow t=0.6$ $x=200+60=260,\ y=250-30=220$. → Intersection $P_1(260,220)$. Add $P_1$.
C(out)→A(in): add intersection of CA with edge 2, then add A.
Line C(300,200)→A(100,150): $x=300-200t,\ y=200-50t$. Into $y=2x-300$: $200-50t=2(300-200t)-300=300-400t$ $200-50t=300-400t\Rightarrow350t=100\Rightarrow t=0.2857$ $x=300-57.14=242.86,\ y=200-14.29=185.71$. → $P_2(242.86,185.71)$. Add $P_2$, then A.
Output list II: B(200,250), $P_1$(260,220), $P_2$(242.86,185.71), A(100,150)
Edge 3: $W_3(200,100)\to W_1(100,300)$
$f(P)=(100-200)(y_p-100)-(300-100)(x_p-200)$ $=-100(y_p-100)-200(x_p-200)$
Centroid: $-100(133.3)-200(0)=-13330<0$. Inside means $f(P)\le 0$.
Check list II:
- B(200,250): $-100(150)-200(0)=-15000\le0$ → Inside
- $P_1$(260,220): $-100(120)-200(60)=-12000-12000=-24000\le0$ → Inside
- $P_2$(242.86,185.71): $-100(85.71)-200(42.86)=-8571-8572=-17143\le0$ → Inside
- A(100,150): $-100(50)-200(-100)=-5000+20000=+15000>0$ → Outside
Process pairs (B→$P_1$→$P_2$→A→B):
B(in)→$P_1$(in): add $P_1$ $P_1$(in)→$P_2$(in): add $P_2$ $P_2$(in)→A(out): add intersection of $P_2$A with edge 3.
Edge 3 line through (200,100),(100,300): slope $=\tfrac{300-100}{100-200}=-2$, line $y-100=-2(x-200)\Rightarrow y=-2x+500$. Line $P_2(242.86,185.71)\to A(100,150)$: $x=242.86-142.86t,\ y=185.71-35.71t$. $185.71-35.71t=-2(242.86-142.86t)+500=-485.72+285.72t+500=14.28+285.72t$ $185.71-14.28=285.72t+35.71t\Rightarrow171.43=321.43t\Rightarrow t=0.5333$ $x=242.86-76.19=166.67,\ y=185.71-19.05=166.67$. → $P_3(166.67,166.67)$. Add $P_3$.
A(out)→B(in): add intersection of AB with edge 3, then add B. Line A(100,150)→B(200,250): $x=100+100t,\ y=150+100t$. Into $y=-2x+500$: $150+100t=-2(100+100t)+500=300-200t$ $150+100t=300-200t\Rightarrow300t=150\Rightarrow t=0.5$ $x=150,\ y=200$. → $P_4(150,200)$. Add $P_4$, then B.
Final Clipped Polygon
Vertices: $$P_1(260,220),\ P_2(242.86,185.71),\ P_3(166.67,166.67),\ P_4(150,200),\ B(200,250)$$
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.