Liang Barsky Line Clipping Algorithm C Program

By | August 16, 2017

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 algorithmBresenham’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

Step 1: Set Tmin = 0 and Tmax = 1
Step 2: Calculate the values of t_x0, t_x1, t_y0, t_y1
Step 3: If T < Tmin or T > Tmax, 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 Tmin = T
        Else If Tvalue is an exiting value Then 
              set Tmax = T
Step 5: If Tmin < Tmax 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

#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

Liang Barsky Line Clipping Algorithm in C Programming with Explanation, Output, Example and Algorithm

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.

Let's Discuss