Skip to content

14. Clipping - 1

Dated: 13-05-2025

Concept

To protect other portions of the canvas, the graphics primitives are not drawn outside a rectangular boundary called clipping rectangle.
The default clipping rectangle is the full screen itself.

cs604_e_14_1.svg

Clipping

Point Clipping

We will save a point \((x, y)\) only if

\[x_\text{min} \le x \le x_\text{max}\]
\[y_\text{min} \le y \le y_\text{max}\]

Line Clipping

\[x = x_1 + u(x_2 - x_1)\]
\[y = y_1 + u(y_2 - y_1)\]
\[u \in [0, 1]\]

If \(u\) is not in this range then the line does not enters the clipping rectangle.

Solve Simultaneous Equations

Consider 2 end points of a line.1 If both are inside the clipping rectangle then it is accepted. If either one or both points are outside, then we need to find if there are any intersections with clipping boundary and then check for interior points again.

We don't have to touch the infinitely interior points of the line1 but just its end points.

The Cohen-sutherland Line-clipping Algorithm

Extend the clipping boundaries so that the screen is divided into 9 sub regions.

cs604_e_14_2.svg

The data represented by each bit in these 4 bits code, assigned to each sub region, is as follows:

  1. \(y > y_\text{max}\)
  2. \(y < y_\text{min}\)
  3. \(x > x_\text{max}\)
  4. \(x < x_\text{min}\)

This algorithm is useful to keep computing intersections at minimal.

Liang-barsky Algorithm

\[x_{\text{min}} \le x_1 + u \Delta x \le x_{\text{max}}\]
\[y_{\text{min}} \le y_1 + u \Delta y \le y_{\text{max}}\]

These inequalities can be summed up as

\[u \times p_k \le q_k, \quad k = 1, 2, 3, 4\]

Definitions of \(p_k\) and \(q_k\)

\[p_1 = - \Delta x, \quad q_1 = x_1 - x_{\text{min}}\]
\[p_2 = - \Delta x, \quad q_2 = x_{\text{max}} - x_1\]
\[p_3 = - \Delta y, \quad q_3 = y_1 - y_{\text{min}}\]
\[p_4 = - \Delta y, \quad q_4 = y_{\text{max}} - y_1\]

The \(k\) indices represent following edges of the clipping rectangle.

  1. right
  2. left
  3. bottom
  4. top

For each edge \(k\), if \(p_k​ = 0\) and \(q_k ​< 0\), the line1 is parallel and outside → reject.
If (\(p_k \ne 0\)) \(r_k = \frac{p_k}{q_k}\)​
if (\(p_k < 0\)) \(u_1 = \max(0, r_k) \quad \forall k\)
if (\(p_k > 0\)) \(u_2 = \min(1, r_k) \quad \forall k\)
if \((u_1 > u_2)\) accept the line
else clip the line1

References


  1. Read more about lines