Let us understand what exactly is Liang Barsky Line Clipping Algorithm in computer graphics and then let us see how to implement Liang Barsky Line Algorithm in C programming using different methods.

#### What is Liang Barsky Line Clipping Algorithm?

The planar line clipping algorithms are the second best approach after line drawing algorithms such as DDA line drawing algorithm**, **Bresenham’s line drawing algorithm and much more.

Basically, clipping means to find the intersection wherein these line algorithms clips these single lines against rectangular or convex regions on a plane surface.

The Liang Barsky Line Clipping Algorithm is comparatively easy to implement and is very fast in execution as compared to Cohen – Sutherland algorithm because the intersection calculations are reduced here.

The Liang Barsky algorithm makes use of parameterization of the line determined by a segment, and many of the line clipping algorithms are derived using this algorithm.

The Liang–Barsky line clipping method makes use of a **parametric line** equation and the inequalities that depict the range of the clipping window.

It, thereby, helps to determine the intersections between the line and the clip window. With this intersection, it becomes possible to identify the portion of the line which needs to be drawn.

This algorithm can be extended to 3 – dimensional clipping. There are many other line clipping algorithms such as following:

- Nicholl-Lee-Nicholl Algorithm
- Cyrus-Beck Algorithm

#### Liang Barsky Line Clipping Algorithm

1 2 3 4 5 6 7 8 9 10 11 12 | Step 1: Set T<sub>min</sub> = 0 and T<sub>max</sub> = 1 Step 2: Calculate the values of t_x0, t_x1, t_y0, t_y1 Step 3: If T < T<sub>min</sub> or T > T<sub>max</sub>, Then Skip and Proceed to Next Edge Else Categorize the Tvalue as Entering or Exiting value Step 4: If Tvalue is an entering value Then set T<sub>min</sub> = T Else If Tvalue is an exiting value Then set T<sub>max</sub> = T Step 5: If T<sub>min</sub> < T<sub>max</sub> Then Draw a line |

If you face any issues while compiling this line drawing algorithm in computer graphics, you may have to **Install graphics.h** header file in your operating system.

## Implement Liang Barsky Line Clipping Algorithm in C Programming

**Note:** This C program for Liang Barsky Line Clipping Algorithm is compiled with Turbo C compiler on Microsoft Windows 10 operating system.

#### Method 1: C Program To Implement Bresenham Line Drawing Algorithm

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | #include<graphics.h> #include<stdio.h> int main() { int count, graphics_driver = DETECT, graphics_mode; int dx, dy, x0, y0, x1, y1; int x_axis_minimum, x_axis_maximum, y_axis_minimum, y_axis_maximum; int t_x0, t_x1, t_y0, t_y1, delay_time; float tmin, tmax, p[4], q[4], t; initgraph(&graphics_driver, &graphics_mode, "c:\\turboc3\\bgi"); printf("\nEnter X - axis line coordinates:\n"); printf("\nEnter the value of x0:\t"); scanf("%d", &x0); printf("\nEnter the value of x1:\t"); scanf("%d", &x1); printf("\nEnter Y - axis line coordinates:\n"); printf("\nEnter the value of y0:\t"); scanf("%d", &y0); printf("\nEnter the value of y1:\t"); scanf("%d", &y1); printf("\nEnter delay time:\t"); scanf("%d", &delay_time); printf("\nEnter Minimum and Maximum Values for X-axis and Y-axis\n"); prinf("X - Axis Minimum Value:\t"); scanf("%d", &x_axis_minimum); prinf("X - Axis Maximum Value:\t"); scanf("%d", &x_axis_maximum); prinf("Y - Axis Minimum Value:\t"); scanf("%d", &y_axis_minimum); prinf("Y - Axis Maximum Value:\t"); scanf("%d", &y_axis_maximum); rectangle(x_axis_minimum, y_axis_minimum, x_axis_maximum, y_axis_maximum); dx = x1 - x0; dy = y1 - y0; p[0] = -dx; p[1] = dx; p[2] = -dy; p[3] = dy; q[0] = x0 - x_axis_minimum; q[1] = x_axis_maximum - x0; q[2] = y0 - y_axis_minimum; q[3] = y_axis_maximum - y0; for(count = 0; count < 4; count++) { if(p[count] == 0) { printf("\nLine is parallel\n"); if(q[count] >= 0) { if(count < 2) { if(y0 < y_axis_minimum) { y0 = y_axis_minimum; } if(y1 > y_axis_maximum) { y1 = y_axis_maximum; } line(x0, y0, x1, y1); } if(count > 1) { if(x0 < x_axis_minimum) { x0 = x_axis_minimum; } if(x1 > x_axis_maximum) { x1 = x_axis_maximum; } line(x0, y0, x1, y1); } } } } tmin = 0; tmax = 1; for(count = 0; count < 4; count++) { t = q[count] / p[count]; if(p[count] < 0) { if(tmin <= t) { tmin = t; } } else { if(tmax > t) { tmax = t; } } } if(tmin < tmax) { t_x0 = x0 + tmin * p[1]; t_x1 = x0 + tmax * p[1]; t_y0 = y0 + tmin * p[3]; t_y1 = y0 + tmax * p[3]; line(t_x0, t_y0, t_x1, t_y1); } delay(delay_time); closegraph(); return 0; } |

#### Output

If you have any compilation errors or doubts about this C program for Liang Barsky Line Clipping Algorithm, let us discuss it in the comment section below. Read more about it on Wikipedia.