Content deleted Content added
Now it is working for all sides, before it was working only for bottom and left. |
CortexFiend (talk | contribs) Link suggestions feature: 2 links added. |
||
(12 intermediate revisions by 10 users not shown) | |||
Line 1:
{{Short description|Line-clipping 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.
:<math>x = x_0 + t (x_1 - x_0) = x_0 + t \Delta x,</math>
Line 23 ⟶ 24:
</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_i</math>, <math>u = q_i / p_i</math> gives <math>t</math> for the intersection point of the line and the window edge (possibly projected).
#
# If <math>u_1 > u_2</math>, the line is entirely outside <
//
#include<iostream>
#include<graphics.h>
Line 39 ⟶ 41:
// this function gives the maximum
float maxi(float arr[], int n) {
float m = 0;
for (int i = 0; i < n; ++i)
Line 69 ⟶ 71:
float q4 = ymax - y1;
float
int
rectangle(xmin, ymin, xmax, ymax); // drawing the clipping window
Line 84 ⟶ 86:
float r2 = q2 / p2;
if (p1 < 0) {
entryParams[entryIndex++] = r1;
} else {
}
}
Line 95 ⟶ 97:
float r4 = q4 / p4;
if (p3 < 0) {
} else {
}
}
float
float
if (
outtextxy(80, 80, "Line is outside the clipping window!");
return;
}
clippedX2 = x1 + (x2 - x1) * u2;
setcolor(CYAN);
line(clippedX1, clippedY1, clippedX2, clippedY2); // draw clipped segment
setlinestyle(1, 1, 0);
line(x1, y1, clippedX1, clippedY1); // original start to clipped start
line(
}
int main() {
cout << "\nLiang-
cout << "\nThe system window
cout << "\nEnter the
float xmin,
cin >> xmin >> ymin >> xmax >> ymax;
cout << "\nEnter the
float x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // using winbgim▼
▲ initgraph(&gd, &gm, "");
liang_barsky_clipper(xmin, ymin, xmax, ymax, x1, y1, x2, y2);
getch();
closegraph();
}
</syntaxhighlight>
==See also==
Line 156 ⟶ 155:
==References==
* Liang, Y. D., and Barsky, B., "[
* Liang, Y. D., B. A., Barsky, and M. Slater, ''[
* James D. Foley. ''[https://books.google.com/books/about/Computer_graphics.html?id=-4ngT05gmAQC Computer graphics: principles and practice]''. Addison-Wesley Professional, 1996. p. 117.
|