Skip to content

Instantly share code, notes, and snippets.

@AakashCode12
Last active September 28, 2020 22:34
Show Gist options
  • Save AakashCode12/edca6db221cb8819139b150b2c33431b to your computer and use it in GitHub Desktop.
Save AakashCode12/edca6db221cb8819139b150b2c33431b to your computer and use it in GitHub Desktop.
Finally Done
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <dos.h>
#include <graphics.h>
#include <ctype.h>
//todo Global Declarations
int x1b, y1b, x2b, y2b;
//?Defining region codes
int INSIDE = 0; // 0000
int LEFT = 1; // 0001
int RIGHT = 2; // 0010
int BOTTOM = 4; // 0100
int TOP = 8; // 1000
//todo Function Prototyping
void axisPlotting();
void lineClipping(double lx1, double lx2, double ly1, double ly2);
int computeCode(double x, double y);
//todo Function axis plotting
void axisPlotting()
{
line(x1b, y1b, x1b, y2b);
line(x1b, y1b, x2b, y1b);
line(x2b, y2b, x2b, y1b);
line(x2b, y2b, x1b, y2b);
}
//todo Function to calculate the ABRL regionCode
int computeCode(double x, double y)
{
int code = INSIDE;
if (x < x1b)
code |= LEFT;
else if (x > x2b)
code |= RIGHT;
if (y < y1b)
code |= BOTTOM;
else if (y > y2b)
code |= TOP;
return code;
}
//todo Function line clipping
void lineClipping(double x1, double x2, double y1, double y2)
{
int code1 = computeCode(x1, y1);
int code2 = computeCode(x2, y2);
int accept = 0;
while (accept == 0)
{
//!till here given the ABRL to both the lines
//todo If else statement for cases of ABRL
if ((code1 == 0) && (code2 == 0))
{
accept = 1;
cleardevice();
axisPlotting();
line(x1, y1, x2, y2);
break;
}
else if (code1 & code2)
{
cout << "\nthe line is completely rejected";
getch();
cleardevice();
axisPlotting();
break;
}
else
{
int code_out;
double x, y;
if (code1 != 0)
code_out = code1;
else
code_out = code2;
if (code_out & TOP)
{
x = x1 + (x2 - x1) * (y2b - y1) / (y2 - y1);
y = y2b;
}
else if (code_out & BOTTOM)
{
x = x1 + (x2 - x1) * (y1b - y1) / (y2 - y1);
y = y1b;
}
else if (code_out & RIGHT)
{
y = y1 + (y2 - y1) * (x2b - x1) / (x2 - x1);
x = x2b;
}
else if (code_out & LEFT)
{
y = y1 + (y2 - y1) * (x1b - x1) / (x2 - x1);
x = x1b;
}
if (code_out == code1)
{
x1 = x;
y1 = y;
code1 = computeCode(x1, y1);
}
else
{
x2 = x;
y2 = y;
code2 = computeCode(x2, y2);
}
}
}
if (accept == 1)
{
cout << "Line accepted from " << x1 << ", "
<< y1 << " to " << x2 << ", " << y2 << endl;
cleardevice();
axisPlotting();
line(x1, y1, x2, y2);
}
}
//todo main function
void main()
{
clrscr();
int gd = DETECT, gm;
float rx, ry, cx, cy;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");
cleardevice();
cout << "\n--------------Line Clipping Algorithm--------------";
cout << "\nEnter Coordinates-- min and then max";
cout << "\nEnter the Box diagonal coordinates (X1, Y1) & (X2, Y2) :\n ";
cin >> x1b >> y1b;
cin >> x2b >> y2b;
axisPlotting();
cout << "\nEnter the Line Coordinates (X1, Y1) & (X2, Y2) :\n ";
double lx1, ly1, lx2, ly2;
cin >> lx1 >> ly1;
cin >> lx2 >> ly2;
line(lx1, ly1, lx2, ly2);
getch();
lineClipping(lx1, lx2, ly1, ly2);
cout << "\nAfter LineClipping Algorithm";
getch();
closegraph();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment