Content deleted Content added
No edit summary |
m →top: fmt., style |
||
Line 1:
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. This algorithm is significantly more efficient than [[Cohen–Sutherland]]. The idea of the
Consider first the usual parametric form of a straight line:
:<math>x = x_0 + t (x_1 - x_0) = x_0 + t \Delta x
:<math>y = y_0 + t (y_1 - y_0) = y_0 + t \Delta y
A point is in the clip window, if
:<math>x_
and
:<math>y_
which can be expressed as the 4 inequalities
:<math>t p_i \le q_i, \quad i = 1, 2, 3, 4
where
:<math>
\begin{align}
p_3 &= -\Delta y, & q_3 &= y_0 - y_\text{min}, & &\text{(bottom)} \\
p_4 &= \Delta y, & q_4 &= y_\text{max} - y_0. & &\text{(top)}
\end{align}
</math>
To compute the final line segment:
# A line parallel to a clipping window edge has <math>p_i = 0</math> for that boundary.
# If for that <math>i</math>, <math>q_i < 0</math>, then the line is completely outside and can be eliminated.
# When <math>p_i < 0</math>, the line proceeds outside to inside the clip window, and when <math>p_i > 0</math>, the line proceeds inside to outside.
# For nonzero <math>p_k</math>, <math>u =
# For each line, calculate <math>u_1</math> and <math>u_2</math>. For <math>u_1</math>, look at boundaries for which <math>p_i < 0</math> (i.e. outside to inside). Take <math>u_1</math> to be the largest among <math>
<source lang="c++">
// Liang
#include<iostream>
#include<graphics.h>
Line 37 ⟶ 39:
// this function gives the maximum
float maxi(float arr[],int n) {
float m = 0;
for (int
return m;
}
// this function gives the minimum
float mini(float arr[], int n) {
for (int i = 0; i < n; ++i)
return
}
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;
int
rectangle(xmin, 467 - ymin, xmax, 467 - ymax); // drawing the clipping window
if ((p1 == 0 && q1 < 0) || (p3 == 0 && q3 < 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) {
negarr[negind++] =
} else {
negarr[negind++] =
posarr[posind++] =
}
}
float xn1, yn1, xn2, yn2;
float rn1,
rn1 = maxi(negarr, negind); // maximum of negative array
rn2 = mini(posarr, posind); // minimum of positive array
xn1 = x1 + p2 * rn1;
yn1 = y1 + p4 * rn1; // computing new points
setcolor(CYAN);
line(xn1, 467 - yn1, xn2, 467 - yn2); // the drawing the new line
setlinestyle(1, 1, 0);
line(x1, 467 - y1, xn1, 467 - yn1);
line(x2, 467 - y2, xn2, 467 - 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, wxmax, wymin, wymax):";
float xmin, xmax, ymin, ymax;
cin
cout << "\nEnter the end points of the line (x1, y1) and (x2, y2):";
float x1, y1, x2, y2;
cin
}
</source>
==See also==
Algorithms used for the same purpose:
|