fbpx
Wikipedia

Liang–Barsky algorithm

In computer graphics, the Liang–Barsky algorithm (named after You-Dong Liang and Brian A. Barsky) is a line clipping algorithm. The Liang–Barsky algorithm uses the parametric equation of a line and inequalities describing the range of the clipping window to determine the intersections between the line and the clip window. With these intersections it knows which portion of the line should be drawn. So this algorithm is significantly more efficient than Cohen–Sutherland. The idea of the Liang–Barsky clipping algorithm is to do as much testing as possible before computing line intersections.

The algorithm uses the parametric form of a straight line:

A point is in the clip window, if

and

which can be expressed as the 4 inequalities

where

To compute the final line segment:

  1. A line parallel to a clipping window edge has for that boundary.
  2. If for that , , then the line is completely outside and can be eliminated.
  3. When , the line proceeds outside to inside the clip window, and when , the line proceeds inside to outside.
  4. For nonzero , gives for the intersection point of the line and the window edge (possibly projected).
  5. The two actual intersections of the line with the window edges, if they exist, are described by and , calculated as follows. For , look at boundaries for which (i.e. outside to inside). Take to be the largest among . For , look at boundaries for which (i.e. inside to outside). Take to be the minimum of .
  6. If , the line is entirely outside the clip window. If it is entirely inside it.
// Liang—Barsky line-clipping algorithm #include<iostream> #include<graphics.h> #include<math.h> using namespace std; // this function gives the maximum float maxi(float arr[],int n) {  float m = 0;  for (int i = 0; i < n; ++i)  if (m < arr[i])  m = arr[i];  return m; } // this function gives the minimum float mini(float arr[], int n) {  float m = 1;  for (int i = 0; i < n; ++i)  if (m > arr[i])  m = arr[i];  return m; } void liang_barsky_clipper(float xmin, float ymin, float xmax, float ymax,  float x1, float y1, float x2, float y2) {  // defining variables  float p1 = -(x2 - x1);  float p2 = -p1;  float p3 = -(y2 - y1);  float p4 = -p3;  float q1 = x1 - xmin;  float q2 = xmax - x1;  float q3 = y1 - ymin;  float q4 = ymax - y1;  float posarr[5], negarr[5];  int posind = 1, negind = 1;  posarr[0] = 1;  negarr[0] = 0;  rectangle(xmin, ymin, xmax, ymax); // drawing the clipping window  if ((p1 == 0 && q1 < 0) || (p2 == 0 && q2 < 0) || (p3 == 0 && q3 < 0) || (p4 == 0 && q4 < 0)) {  outtextxy(80, 80, "Line is parallel to clipping window!");  return;  }  if (p1 != 0) {  float r1 = q1 / p1;  float r2 = q2 / p2;  if (p1 < 0) {  negarr[negind++] = r1; // for negative p1, add it to negative array  posarr[posind++] = r2; // and add p2 to positive array  } else {  negarr[negind++] = r2;  posarr[posind++] = r1;  }  }  if (p3 != 0) {  float r3 = q3 / p3;  float r4 = q4 / p4;  if (p3 < 0) {  negarr[negind++] = r3;  posarr[posind++] = r4;  } else {  negarr[negind++] = r4;  posarr[posind++] = r3;  }  }  float xn1, yn1, xn2, yn2;  float rn1, rn2;  rn1 = maxi(negarr, negind); // maximum of negative array  rn2 = mini(posarr, posind); // minimum of positive array  if (rn1 > rn2) { // reject  outtextxy(80, 80, "Line is outside the clipping window!");  return;  }  xn1 = x1 + p2 * rn1;  yn1 = y1 + p4 * rn1; // computing new points  xn2 = x1 + p2 * rn2;  yn2 = y1 + p4 * rn2;  setcolor(CYAN);  line(xn1, yn1, xn2, yn2); // the drawing the new line  setlinestyle(1, 1, 0);  line(x1, y1, xn1, yn1);  line(x2, y2, xn2, yn2); } int main() {  cout << "\nLiang-barsky line clipping";  cout << "\nThe system window outlay is: (0,0) at bottom left and (631, 467) at top right";  cout << "\nEnter the co-ordinates of the window(wxmin, wymin, wxmax, wymax):";  float xmin, ymin, xmax, ymax;  cin >> xmin >> ymin >> xmax >> ymax;  cout << "\nEnter the end points of the line (x1, y1) and (x2, y2):";  float x1, y1, x2, y2;  cin >> x1 >> y1 >> x2 >> y2;  int gd = DETECT, gm;  // using the winbgim library for C++, initializing the graphics mode  initgraph(&gd, &gm, "");  liang_barsky_clipper(xmin, ymin, xmax, ymax, x1, y1, x2, y2);  getch();  closegraph(); } 

See also edit

Algorithms used for the same purpose:

References edit

  • Liang, Y. D., and Barsky, B., "A New Concept and Method for Line Clipping", ACM Transactions on Graphics, 3(1):1–22, January 1984.
  • Liang, Y. D., B. A., Barsky, and M. Slater, Some Improvements to a Parametric Line Clipping Algorithm[dead link], CSD-92-688, Computer Science Division, University of California, Berkeley, 1992.
  • James D. Foley. Computer graphics: principles and practice. Addison-Wesley Professional, 1996. p. 117.

External links edit

liang, barsky, algorithm, computer, graphics, named, after, dong, liang, brian, barsky, line, clipping, algorithm, uses, parametric, equation, line, inequalities, describing, range, clipping, window, determine, intersections, between, line, clip, window, with,. In computer graphics the Liang Barsky algorithm named after You Dong Liang and Brian A Barsky is a line clipping algorithm The Liang Barsky algorithm uses the parametric equation of a line and inequalities describing the range of the clipping window to determine the intersections between the line and the clip window With these intersections it knows which portion of the line should be drawn So this algorithm is significantly more efficient than Cohen Sutherland The idea of the Liang Barsky clipping algorithm is to do as much testing as possible before computing line intersections The algorithm uses the parametric form of a straight line x x0 t x1 x0 x0 tDx displaystyle x x 0 t x 1 x 0 x 0 t Delta x y y0 t y1 y0 y0 tDy displaystyle y y 0 t y 1 y 0 y 0 t Delta y A point is in the clip window if xmin x0 tDx xmax displaystyle x text min leq x 0 t Delta x leq x text max and ymin y0 tDy ymax displaystyle y text min leq y 0 t Delta y leq y text max which can be expressed as the 4 inequalities tpi qi i 1 2 3 4 displaystyle tp i leq q i quad i 1 2 3 4 where p1 Dx q1 x0 xmin left p2 Dx q2 xmax x0 right p3 Dy q3 y0 ymin bottom p4 Dy q4 ymax y0 top displaystyle begin aligned p 1 amp Delta x amp q 1 amp x 0 x text min amp amp text left p 2 amp Delta x amp q 2 amp x text max x 0 amp amp text right p 3 amp Delta y amp q 3 amp y 0 y text min amp amp text bottom p 4 amp Delta y amp q 4 amp y text max y 0 amp amp text top end aligned To compute the final line segment A line parallel to a clipping window edge has pi 0 displaystyle p i 0 for that boundary If for that i displaystyle i qi lt 0 displaystyle q i lt 0 then the line is completely outside and can be eliminated When pi lt 0 displaystyle p i lt 0 the line proceeds outside to inside the clip window and when pi gt 0 displaystyle p i gt 0 the line proceeds inside to outside For nonzero pi displaystyle p i u qi pi displaystyle u q i p i gives t displaystyle t for the intersection point of the line and the window edge possibly projected The two actual intersections of the line with the window edges if they exist are described by u1 displaystyle u 1 and u2 displaystyle u 2 calculated as follows For u1 displaystyle u 1 look at boundaries for which pi lt 0 displaystyle p i lt 0 i e outside to inside Take u1 displaystyle u 1 to be the largest among 0 qi pi displaystyle 0 q i p i For u2 displaystyle u 2 look at boundaries for which pi gt 0 displaystyle p i gt 0 i e inside to outside Take u2 displaystyle u 2 to be the minimum of 1 qi pi displaystyle 1 q i p i If u1 gt u2 displaystyle u 1 gt u 2 the line is entirely outside the clip window If u1 lt 0 lt 1 lt u2 displaystyle u 1 lt 0 lt 1 lt u 2 it is entirely inside it Liang Barsky line clipping algorithm include lt iostream gt include lt graphics h gt include lt math h gt using namespace std this function gives the maximum float maxi float arr int n float m 0 for int i 0 i lt n i if m lt arr i m arr i return m this function gives the minimum float mini float arr int n float m 1 for int i 0 i lt n i if m gt arr i m arr i return m void liang barsky clipper float xmin float ymin float xmax float ymax float x1 float y1 float x2 float y2 defining variables float p1 x2 x1 float p2 p1 float p3 y2 y1 float p4 p3 float q1 x1 xmin float q2 xmax x1 float q3 y1 ymin float q4 ymax y1 float posarr 5 negarr 5 int posind 1 negind 1 posarr 0 1 negarr 0 0 rectangle xmin ymin xmax ymax drawing the clipping window if p1 0 amp amp q1 lt 0 p2 0 amp amp q2 lt 0 p3 0 amp amp q3 lt 0 p4 0 amp amp q4 lt 0 outtextxy 80 80 Line is parallel to clipping window return if p1 0 float r1 q1 p1 float r2 q2 p2 if p1 lt 0 negarr negind r1 for negative p1 add it to negative array posarr posind r2 and add p2 to positive array else negarr negind r2 posarr posind r1 if p3 0 float r3 q3 p3 float r4 q4 p4 if p3 lt 0 negarr negind r3 posarr posind r4 else negarr negind r4 posarr posind r3 float xn1 yn1 xn2 yn2 float rn1 rn2 rn1 maxi negarr negind maximum of negative array rn2 mini posarr posind minimum of positive array if rn1 gt rn2 reject outtextxy 80 80 Line is outside the clipping window return xn1 x1 p2 rn1 yn1 y1 p4 rn1 computing new points xn2 x1 p2 rn2 yn2 y1 p4 rn2 setcolor CYAN line xn1 yn1 xn2 yn2 the drawing the new line setlinestyle 1 1 0 line x1 y1 xn1 yn1 line x2 y2 xn2 yn2 int main cout lt lt n Liang barsky line clipping cout lt lt n The system window outlay is 0 0 at bottom left and 631 467 at top right cout lt lt n Enter the co ordinates of the window wxmin wymin wxmax wymax float xmin ymin xmax ymax cin gt gt xmin gt gt ymin gt gt xmax gt gt ymax cout lt lt n Enter the end points of the line x1 y1 and x2 y2 float x1 y1 x2 y2 cin gt gt x1 gt gt y1 gt gt x2 gt gt y2 int gd DETECT gm using the winbgim library for C initializing the graphics mode initgraph amp gd amp gm liang barsky clipper xmin ymin xmax ymax x1 y1 x2 y2 getch closegraph See also editAlgorithms used for the same purpose Cyrus Beck algorithm Nicholl Lee Nicholl algorithm Fast clippingReferences editLiang Y D and Barsky B A New Concept and Method for Line Clipping ACM Transactions on Graphics 3 1 1 22 January 1984 Liang Y D B A Barsky and M Slater Some Improvements to a Parametric Line Clipping Algorithm dead link CSD 92 688 Computer Science Division University of California Berkeley 1992 James D Foley Computer graphics principles and practice Addison Wesley Professional 1996 p 117 External links edithttp hinjang com articles 04 html eight Skytopia The Liang Barsky line clipping algorithm in a nutshell Retrieved from https en wikipedia org w index php title Liang Barsky algorithm amp oldid 1152574361, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.