Liang–Barsky algorithm: Difference between revisions

Content deleted Content added
m Update 1st reference, and found a replacement for 2nd reference's dead link.
Replaced (potential) slurs with positively reinforced keywords
Line 33:
 
<syntaxhighlight lang="c++">
// Liang—BarskyLiang–Barsky line-clipping algorithm
#include<iostream>
#include<graphics.h>
Line 41:
 
// this function gives the maximum
float maxi(float arr[], int n) {
float m = 0;
for (int i = 0; i < n; ++i)
Line 71:
float q4 = ymax - y1;
 
float posarrexitParams[5], negarrentryParams[5];
int posindexitIndex = 1, negindentryIndex = 1;
posarrexitParams[0] = 1;
negarrentryParams[0] = 0;
 
rectangle(xmin, ymin, xmax, ymax); // drawing the clipping window
Line 86:
float r2 = q2 / p2;
if (p1 < 0) {
entryParams[entryIndex++] = r1;
negarr[negind++] = r1; // for negative p1, add it to negative array
posarrexitParams[posindexitIndex++] = r2; // and add p2 to positive array
} else {
negarrentryParams[negindentryIndex++] = r2;
posarrexitParams[posindexitIndex++] = r1;
}
}
Line 97:
float r4 = q4 / p4;
if (p3 < 0) {
negarrentryParams[negindentryIndex++] = r3;
posarrexitParams[posindexitIndex++] = r4;
} else {
negarrentryParams[negindentryIndex++] = r4;
posarrexitParams[posindexitIndex++] = r3;
}
}
 
float xn1clippedX1, yn1clippedY1, xn2clippedX2, yn2clippedY2;
float rn1u1, rn2u2;
rn1u1 = maxi(negarrentryParams, negindentryIndex); // maximum of negativeentry arraypoints
rn2u2 = mini(posarrexitParams, posindexitIndex); // minimum of positiveexit arraypoints
 
if (rn1u1 > rn2u2) { // reject
outtextxy(80, 80, "Line is outside the clipping window!");
return;
}
 
xn1clippedX1 = x1 + p2(x2 - x1) * rn1u1;
yn1clippedY1 = y1 + p4(y2 - y1) * rn1u1; // computing new points
clippedX2 = x1 + (x2 - x1) * u2;
 
xn2clippedY2 = x1y1 + p2(y2 - y1) * rn2u2;
yn2 = y1 + p4 * rn2;
 
setcolor(CYAN);
line(clippedX1, clippedY1, clippedX2, clippedY2); // draw clipped segment
 
line(xn1, yn1, xn2, yn2); // the drawing the new line
 
setlinestyle(1, 1, 0);
line(x1, y1, clippedX1, clippedY1); // original start to clipped start
 
line(x1x2, y1y2, xn1clippedX2, yn1clippedY2); // original end to clipped end
line(x2, y2, xn2, yn2);
}
 
int main() {
cout << "\nLiang-barskyBarsky lineLine clippingClipping";
cout << "\nThe system window outlaylayout is: (0,0) at bottom left and (631, 467) at top right";
cout << "\nEnter the co-ordinatescoordinates of the window (wxminxmin, wyminymin, wxmaxxmax, wymaxymax): ";
float xmin, ymin, xmax, ymax;
cin >> xmin >> ymin >> xmax >> ymax;
 
cout << "\nEnter the end pointsendpoints of the line (x1, y1) and (x2, y2): ";
float x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
 
int gd = DETECT, gm;
initgraph(&gd, &gm, ""); // using winbgim
 
// 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();