Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <graphics.h>
- #include <stdio.h>
- #include <conio.h>
- #define IN 0
- #define TOP 8
- #define BOTTOM 4
- #define RIGHT 2
- #define LEFT 1
- float XMIN=20, XMAX=90, YMIN=20, YMAX=70, m;
- int ax=10, ay=10, bx=100, by = 90;
- int count=0;
- float f[2][2];
- void startgraphics(){
- int gd = DETECT, gm;
- initgraph(&gd, &gm, "");
- cleardevice();
- }
- void clipit(float x, float y){
- if(count==2) return;
- f[count][0]=x;
- f[count][1]=y;
- count++;
- }
- int getCode(float x, float y){
- int code=IN;
- if(x < XMIN) code |= LEFT;
- else if(x > XMAX) code |= RIGHT;
- if(y < YMIN) code |= BOTTOM;
- else if(y > YMAX) code |= TOP;
- return code;
- }
- void calcF(int code){
- if(code & TOP)
- clipit(1/m*(YMAX-ay)+ax, YMAX);
- else if(code & BOTTOM)
- clipit(1/m*(YMIN-ay)+ax,YMIN);
- else if(code & RIGHT)
- clipit(XMAX, m*(XMAX-ax)+ay);
- else if(code & LEFT)
- clipit(XMIN, m*(XMIN-ax)+ay);
- }
- void main(){
- int codeA=0, codeB=0;
- float xnew, ynew;
- int i, j;
- m=(float)(by-ay)/(bx-ax);
- clrscr();
- printf("Windows: (%f, %f) and (%f, %f)\n", XMIN, YMIN, XMAX, YMAX);
- printf("Line: (%d, %d) to (%d, %d)", ax, ay, bx, by);
- codeA=getCode(ax, ay);
- if(codeA==0) clipit((float)ax, (float)ay);
- codeB=getCode(bx, by);
- if(codeB==0) clipit((float)bx, (float)by);
- printf("\ncodeA = %d\ncodeB = %d",codeA, codeB);
- if((codeA|codeB) == 0) printf("\n %d In!\n", codeA|codeB);
- else if(codeA & codeB) printf("\nOut!\n");
- else{
- if(codeA)calcF(codeA);
- if(codeB)calcF(codeB);
- printf("\n\n%d/%d = m = %f", by-ay, bx-ax, m);
- if(count>0){
- printf("\nPartially Visible!\nThe clipping points are:-\n");
- for(i=0; i<2; i++)
- printf("\n%f\t%f\n", f[i][0], f[i][1]);
- } else printf("\nNot Visible!");
- }
- getch();
- startgraphics();
- rectangle(XMIN, YMIN, XMAX, YMAX);
- setcolor(GREEN);
- line((int)ax, (int)ay, (int)bx, (int)by);
- getch();
- cleardevice();
- rectangle(XMIN, YMIN, XMAX, YMAX);
- setcolor(RED);
- line((int)f[0][0], (int)f[0][1], (int)f[1][0], (int)f[1][1]);
- getch();
- closegraph();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement