Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include<graphics.h>
- using namespace std;
- // Defining region codes
- const int INSIDE = 0;
- const int LEFT = 1;
- const int RIGHT = 2;
- const int BOTTOM = 4;
- const int TOP = 8;
- int x_max;
- int y_max;
- int x_min;
- int y_min;
- int computeCode(double x, double y)
- {
- int code = INSIDE;
- if (x < x_min)
- code |= LEFT;
- else if (x > x_max)
- code |= RIGHT;
- if (y < y_min)
- code |= BOTTOM;
- else if (y > y_max)
- code |= TOP;
- return code;
- }
- void drawwindow()
- {
- line(x_min,y_min,x_max,y_min);
- line(x_min,y_min,x_min,y_max);
- line(x_max,y_min,x_max,y_max);
- line(x_min,y_max,x_max,y_max);
- }
- void Clip(double x1, double y1, double x2, double y2)
- {
- int gd=DETECT,v,gm;
- initgraph(&gd,&gm,"");
- drawwindow();
- int code1 = computeCode(x1, y1);
- int code2 = computeCode(x2, y2);
- if(code1==0&&code2==0)
- {
- cout<<"IN The Window"<<endl;
- line(x1,y1,x2,y2);
- }
- bool accept = false;
- while (true)
- {
- if ((code1 == 0) && (code2 == 0))
- {
- accept = true;
- break;
- }
- else if (code1 & code2)
- {
- 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) * (y_max - y1) / (y2 - y1);
- y = y_max;
- }
- else if (code_out & BOTTOM)
- {
- x = x1 + (x2 - x1) * (y_min - y1) / (y2 - y1);
- y = y_min;
- }
- else if (code_out & RIGHT)
- {
- y = y1 + (y2 - y1) * (x_max - x1) / (x2 - x1);
- x = x_max;
- }
- else if (code_out & LEFT)
- {
- y = y1 + (y2 - y1) * (x_min - x1) / (x2 - x1);
- x = x_min;
- }
- if (code_out == code1)
- {
- x1 = x;
- y1 = y;
- code1 = computeCode(x1, y1);
- }
- else
- {
- x2 = x;
- y2 = y;
- code2 = computeCode(x2, y2);
- }
- }
- }
- if (accept)
- {
- cout <<"Clipping Candidate" <<endl;
- line(x1,y1,x2,y2);
- }
- else
- {
- cout<<"Out of the window"<<endl;
- }
- }
- // Driver code
- int main()
- {
- cout<<"ENTER XMIN YMIN :"<<endl;
- cin>>x_min>>y_min;
- cout<<"ENTER XMAX YMAX :"<<endl;
- cin>>x_max>>y_max;
- cout<<"Enter No of line: "<<endl;
- int n;
- cin>>n;
- for(int i=1;i<=n;i++)
- {
- int a,b,c,d;
- cout<<"Enter first Point X and Y value :";
- cin>>a>>b;
- cout<<"Enter Second Point X and Y value :";
- cin>>c>>d;
- Clip(a, b, c, d);
- delay(5000);
- closegraph();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement