computer architecture schaum series

34
BOUNDARY FILL ALGORITHM #include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> void boundary_fill(int,int); void main() { int xmax,ymax; int ver[10][2]; int i,n; int gd=DETECT,gm; int a,b,c,d; clrscr(); initgraph(&gd,&gm,"c:\\turboc3\\bgi"); xmax=getmaxx(); ymax=getmaxy(); line(xmax/2,0,xmax/2,ymax); line(0,ymax/2,xmax,ymax/2); printf("Enter the no. of vertices"); scanf("%d",&n); for(i=0;i<n;i++) { printf("enter x and y coordinates\n"); scanf("%d%d",&ver[i][0],&ver[i][1]); } xmax=xmax/2; ymax=ymax/2; for(i=0;i<n-1;i++) { a=ver[i][0]; b=ver[i][1];c=ver[i+1][0]; d=ver[i+1][1];

Upload: preeti-bhaya

Post on 12-Apr-2015

118 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Computer Architecture Schaum Series

BOUNDARY FILL ALGORITHM#include<stdio.h>#include<conio.h>#include<graphics.h>#include<dos.h>

void boundary_fill(int,int);

void main(){int xmax,ymax;int ver[10][2];int i,n;int gd=DETECT,gm;

int a,b,c,d;clrscr();initgraph(&gd,&gm,"c:\\turboc3\\bgi");xmax=getmaxx();ymax=getmaxy();line(xmax/2,0,xmax/2,ymax);line(0,ymax/2,xmax,ymax/2);printf("Enter the no. of vertices");scanf("%d",&n);for(i=0;i<n;i++){printf("enter x and y coordinates\n");scanf("%d%d",&ver[i][0],&ver[i][1]);} xmax=xmax/2; ymax=ymax/2;for(i=0;i<n-1;i++){

a=ver[i][0];b=ver[i][1];c=ver[i+1][0];d=ver[i+1][1];line(a+xmax,ymax-b,xmax+c,ymax-d);}line(xmax+ver[i][0],ymax-ver[i][1],xmax+ver[0][0],ymax-ver[0][1]);printf("Enter any coordinate interior to polygon");scanf("%d%d",&a,&b);a=a+xmax;b=ymax-b;boundary_fill(a,b);

Page 2: Computer Architecture Schaum Series

getch();}

void boundary_fill(int x,int y) { int current; current=getpixel(x,y); if(current!=WHITE && current!=RED) { putpixel(x,y,RED); boundary_fill(x+1,y); boundary_fill(x-1,y); boundary_fill(x,y+1); boundary_fill(x,y-1); } }

Page 3: Computer Architecture Schaum Series

FLOOD FILL ALGORITHM

#include<stdio.h>#include<conio.h>#include<graphics.h>#include<dos.h>

void fill_right(int,int);void fill_left(int,int);

void main(){int xmax,ymax;int ver[10][2];int i,n;int gd=DETECT,gm;int a,b,c,d;clrscr();initgraph(&gd,&gm,"c:\\turboc3\\bgi");xmax=getmaxx();ymax=getmaxy();line(xmax/2,0,xmax/2,ymax);line(0,ymax/2,xmax,ymax/2);printf("Enter the no. of vertices");scanf("%d",&n);for(i=0;i<n;i++){printf("enter x and y coordinates\n");scanf("%d%d",&ver[i][0],&ver[i][1]);} xmax=xmax/2; ymax=ymax/2;for(i=0;i<n-1;i++){

a=ver[i][0];b=ver[i][1];c=ver[i+1][0];d=ver[i+1][1];line(a+xmax,ymax-b,xmax+c,ymax-d);}line(xmax+ver[i][0],ymax-ver[i][1],xmax+ver[0][0],ymax-ver[0][1]);printf("Enter any coordinate interior to polygon");scanf("%d%d",&a,&b);

fill_right(a,b);fill_left(a,b);getch();

Page 4: Computer Architecture Schaum Series

}

void fill_right(int x,int y){if((getpixel(x,y)!=WHITE)&& (getpixel(x,y)!=RED)){putpixel(x,y,RED);fill_right(x,y-1);fill_right(x,y+1);}delay(1);}void fill_left(int x,int y){if((getpixel(x,y)!=WHITE)&& (getpixel(x,y)!=RED)){putpixel(x,y,RED);fill_right(x-1,y);x=x+1;fill_left(x,y-1);fill_left(x,y+1);}delay(1);}

Page 5: Computer Architecture Schaum Series

SCAN FILL ALGORITHM

#include <stdio.h>#include <conio.h>#include <graphics.h>

main(){

int n,i,j,k,gd,gm,dy,dx;int x,y,temp;int a[20][2],xi[20];float slope[20];

clrscr();printf("\n\n\tEnter the no. of edges of polygon : ");scanf("%d",&n);printf("\n\n\tEnter the cordinates of polygon :\n\n\n ");

for(i=0;i<n;i++){printf("\tX%d Y%d : ",i,i);scanf("%d %d",&a[i][0],&a[i][1]);}

a[n][0]=a[0][0];a[n][1]=a[0][1];

detectgraph(&gd,&gm);initgraph(&gd,&gm,"c:\\tc\\bgi");

/*- draw polygon -*/

for(i=0;i<n;i++) {line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);}

getch();

Page 6: Computer Architecture Schaum Series

for(i=0;i<n;i++){dy=a[i+1][1]-a[i][1];dx=a[i+1][0]-a[i][0];

if(dy==0) slope[i]=1.0;if(dx==0) slope[i]=0.0;

if((dy!=0)&&(dx!=0)) /*- calculate inverse slope -*/ {slope[i]=(float) dx/dy;}}

for(y=0;y< 480;y++){k=0;for(i=0;i<n;i++){

if( ((a[i][1]<=y)&&(a[i+1][1]>y))||((a[i][1]>y)&&(a[i+1][1]<=y))){xi[k]=(int)(a[i][0]+slope[i]*(y-a[i][1]));k++;}}

for(j=0;j<k-1;j++) /*- Arrange x-intersections in order -*/for(i=0;i<k-1;i++){if(xi[i]>xi[i+1]){temp=xi[i];xi[i]=xi[i+1];xi[i+1]=temp;}}

setcolor(35);for(i=0;i<k;i+=2){line(xi[i],y,xi[i+1]+1,y);getch();}

}}

Page 7: Computer Architecture Schaum Series
Page 8: Computer Architecture Schaum Series

Assignment 2

1 Write a generalized program to shear an object in X and Y direction

#include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h>

#define ROUND(a) ((int)(a+0.5))

int ver[3][2]; int xmax,ymax;

void initialise() { int gdriver=DETECT,gmode; initgraph(&gdriver,&gmode,"c:\\turboc3\\bgi"); }

void drawpolygon() { int i; for(i=0;i<3;i++) line(ver[i][0],ver[i][1],ver[i+1][0],ver[i+1][1]); line(ver[i][0],ver[i][1],ver[0][0],ver[0][1]); }

void drawaxis() { line(xmax/2,0,xmax/2,ymax); line(0,ymax/2,xmax,ymax/2); }

void translate(int refptx,int refpty) { int i; for(i=0;i<4;i++) { ver[i][0]=ver[i][0]-refptx; ver[i][1]=ver[i][1]-refpty; } }

void shear(float shx,int refptx,int refpty) { int i; float a; translate(refptx,refpty);

Page 9: Computer Architecture Schaum Series

for(i=0;i<4;i++) { a=ver[i][0]+(ver[i][1]*shx); ver[i][0]=ROUND(a); } }

void main() { int i; int refptx,refpty; initialise(); clrscr(); xmax=getmaxx(); ymax=getmaxy(); drawaxis(); printf("enter the coordinates of vertices of polygon"); for(i=0;i<4;i++) { scanf("%d%d",&ver[i][0],&ver[i][1]); ver[i][0]=ver[i][0]+xmax/2; ver[i][1]=ymax/2-ver[i][1]; } drawpolygon(); printf("enter coordinates of reference point"); scanf("%d%d",&refptx,&refpty); shear(1,refptx,refpty); drawpolygon(); getch(); }

Page 10: Computer Architecture Schaum Series

Write a program to perform reflection of an object about any line y=mx+c. using this program reflect a diamond shaped polygon with vertices A(-1,0), B(0,-2),C(1,0),D(0,2)

#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<graphics.h>#include<math.h>#include<time.h>

int shftx,shfty;

void drawaxis(){

int xmax;int ymax;ymax=getmaxy();xmax=getmaxx();shftx=xmax/2;shfty=ymax/2;line(shftx,0,shftx,ymax); //draw y axisline(0,shfty,xmax,shfty); //draw x axis

}

void initialize(){

int gdriver=DETECT,gmode,errorcode;initgraph(&gdriver,&gmode,"c:\\Turboc3\\BGI");errorcode=graphresult();//an error occuredif(errorcode != grOk){printf("Graphics error : %s \n",grapherrormsg(errorcode));printf("Press any key to halt");getch();exit(1);}

}

void linedraw(double m,int c){

int i=0;int y,x=-shftx;double temp;for(i=0;i<getmaxx();i++){

temp=(m*x)+c;y=shfty-temp;putpixel(i,y,CGA_GREEN);

Page 11: Computer Architecture Schaum Series

x++;}

}

double calslope(int x1,int y1,int xo,int yo){

double m1=y1-yo;double m2=x1-xo;double m=m1/m2;return m;

}int * pointIntersection(double m1,int c1,float m2,int c2){

int pt[2];double temp,temp2;temp=c1-c2;temp2=m2-m1;pt[0]=temp/temp2;pt[1]=(m1*pt[0])+c1;return &pt[0];

}

void drawPoly(int * a,int n){

int * aa;int i;int x1,y1,x2,y2;aa=a;//printf("%u %u",aa,a);setcolor(CGA_YELLOW);x1=*(a);a++;y1=*(a);a++;for(i=0;i<n-1;i++){

x2=*(a);a++;y2=*(a);a++;line(x1,y1,x2,y2);

// printf("line-%d %d %d %d",x1,y1,x2,y2);x1=x2;y1=y2;

} x1=*(aa); aa++; y1=*(aa); line(x2,y2,x1,y1);}

Page 12: Computer Architecture Schaum Series

void reflection(double m,int c,int * a,int n)//n is no of sides{

int * pt;double phi;int nn;double theta;double diff; //line 2=mpt and c=0double phidash;double mpt,mptdash,mptPerp;int cPerp,cdash;int * reflctPoly;int count=0;int nxt=0;linedraw(m,c);nn=n;//perpendicular line=-1/mpt and c=y-mxwhile(n>0){mpt=calslope(shftx,shfty,*(a+nxt),*(a+nxt+1));//printf("slope of point % lf",mpt);pt= pointIntersection(m,c,mpt,0);//printf("%d %d ",pt[0],pt[1]);//dist=caldist(pt[0],pt[1],*(a+count),*(a+count+1));

//perpendicular line

mptPerp=-1/mpt;cPerp=*(pt+1)-(mptPerp*(*(pt+0)));

theta=atan(m);//printf("theta= %df",theta);phi=atan(mpt);//printf("mp1=%f",mpt);//printf("phi= %df",phi);diff=phi-theta;//printf("diff= %df",diff);phidash=theta-diff;phidash=(phidash*180)/3.14 ;//printf("phidash= %df",phidash);mptdash=tan(phidash);//printf("mptdash= %df",mptdash);cdash=*(pt+1)-(mptdash*(*(pt+0)));//printf("cdash= %df",cdash);//printf("slope of new line %lf",mptdash);pt=pointIntersection(mptPerp,cPerp,mptdash,cdash);//printf("final points %d %d ",pt[0],pt[1]);*(reflctPoly+count)=*(pt+0);*(reflctPoly+count+1)=*(pt+1);count=count+2;n--;nxt++;

Page 13: Computer Architecture Schaum Series

}setcolor(CGA_YELLOW);drawPoly(reflctPoly,nn);

}

void main(){

int i,j;int a[4][2];initialize();drawaxis();printf("enter coordinates");scanf("%d %d",&a[0][0],&a[0][1]);a[0][0]+=shftx;a[0][1]=shfty-a[0][1];printf("enter coordinates");scanf("%d %d",&a[1][0],&a[1][1]);a[1][0]+=shftx;a[1][1]=shfty-a[1][1];printf("enter coordinates");scanf("%d %d",&a[2][0],&a[2][1]);a[2][0]+=shftx;a[2][1]=shfty-a[2][1];printf("enter coordinates");scanf("%d %d",&a[3][0],&a[3][1]);a[3][0]+=shftx;a[3][1]=shfty-a[3][1];

// printf("%d %d %d %d",a[0][0],a[0][1],a[1][0],a[1][1]); // printf("%d %d %d %d",a[2][0],a[2][1],a[3][0],a[3][1]);

drawPoly(a,4);reflection(1,6,&a[0],4);getch();

}

Page 14: Computer Architecture Schaum Series

Write a program that rotates an object about axis passing through point Q(x,y) by degree

#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<graphics.h>#include<math.h>#include<time.h>

int shftx,shfty;

void drawaxis(){

int xmax;int ymax;ymax=getmaxy();xmax=getmaxx();shftx=xmax/2;shfty=ymax/2;line(shftx,0,shftx,ymax); //draw y axisline(0,shfty,xmax,shfty); //draw x axis

}

void initialize(){

int gdriver=DETECT,gmode,errorcode;initgraph(&gdriver,&gmode,"c:\\Turboc3\\BGI");errorcode=graphresult();//an error occuredif(errorcode != grOk){printf("Graphics error : %s \n",grapherrormsg(errorcode));printf("Press any key to halt");

Page 15: Computer Architecture Schaum Series

getch();exit(1);}

}

void drawPoly(int * a,int n){

int * aa;int i;int x1,y1,x2,y2;aa=a;//printf("%u %u",aa,a);setcolor(CGA_YELLOW);x1=*(a);a++;y1=*(a);a++;for(i=0;i<n-1;i++){

x2=*(a);a++;y2=*(a);a++;line(x1,y1,x2,y2);x1=x2;y1=y2;

} x1=*(aa); aa++; y1=*(aa); line(x2,y2,x1,y1);}

int * rotate(int xr,int yr,double theta,int x,int y){

int pt[2];double deg1=cos(theta);double deg2=sin(theta);double x1,y1;double temp1,temp2,temp3,temp4;printf("%lf",deg1);temp1=(x-xr)*deg1;temp2=(y-yr)*deg2;temp3=(x-xr)*deg2;temp4=(y-yr)*deg1;printf("%lf",temp3);x1=(xr+temp1 - temp2);y1=(yr+temp3 + temp4);printf("%d ",pt[0]);printf("%d",pt[1]);

Page 16: Computer Architecture Schaum Series

pt[0]=x1;pt[1]=y1;return pt;

}

void rotatePoly(int * a,int n,int xr,int yr,double theta){

int * newPoly;int *pt;int i=0;int nxt=0;for(i=0;i<n;i++){

pt=rotate(xr,yr,theta,*(a+i),*(a+i+1));*(newPoly+nxt)=*(pt);nxt++;*(newPoly+nxt)=*(pt+1);nxt++;i=i+2;printf("new point %d %d ",*(newPoly+nxt-2));printf("new point %d %d ",*(newPoly+nxt-1));

}setcolor(CGA_CYAN);drawPoly(newPoly,n);

}void main(){

int i,j;int a[3][2];

int xfxd,yfxd;double deg;

initialize();drawaxis();

printf("enter coordinates");scanf("%d %d",&a[0][0],&a[0][1]);a[0][0]+=shftx;a[0][1]=shfty-a[0][1];printf("enter coordinates");scanf("%d %d",&a[1][0],&a[1][1]);a[1][0]+=shftx;a[1][1]=shfty-a[1][1];printf("enter coordinates");scanf("%d %d",&a[2][0],&a[2][1]);a[2][0]+=shftx;a[2][1]=shfty-a[2][1];

Page 17: Computer Architecture Schaum Series

printf("enter angle ");scanf("%lf",&deg);

deg=deg*(3.14/180);

printf("enter fixd points of x and y ");scanf("%d %d",&xfxd,&yfxd);

drawPoly(a,3);rotatePoly(a,3,xfxd,yfxd,deg);// (int * a,int n,int xr,int yr,int theta)getch();

}

Write a program that scales an object about a point(h,k)

#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<graphics.h>#include<math.h>#include<time.h>

int shftx,shfty;

void drawaxis(){

int xmax;int ymax;ymax=getmaxy();xmax=getmaxx();shftx=xmax/2;

Page 18: Computer Architecture Schaum Series

shfty=ymax/2;line(shftx,0,shftx,ymax); //draw y axisline(0,shfty,xmax,shfty); //draw x axis

}

void initialize(){

int gdriver=DETECT,gmode,errorcode;initgraph(&gdriver,&gmode,"c:\\Turboc3\\BGI");errorcode=graphresult();//an error occuredif(errorcode != grOk){printf("Graphics error : %s \n",grapherrormsg(errorcode));printf("Press any key to halt");getch();exit(1);}

}

void drawPoly(int * a,int n){

int * aa;int i;int x1,y1,x2,y2;aa=a;setcolor(CGA_YELLOW);x1=*(a);a++;y1=*(a);a++;for(i=0;i<n-1;i++){

x2=*(a);a++;y2=*(a);a++;line(x1,y1,x2,y2);x1=x2;y1=y2;

} x1=*(aa); aa++; y1=*(aa); line(x2,y2,x1,y1);}

int scale(int xy,float sc,int prod)//prod=1-sx or 1-sy{ return ((xy*sc)+prod);

Page 19: Computer Architecture Schaum Series

}

void scalePoly(int * a,int n,int xfxd,int yfxd,float h,float k){

int * newPoly;int nxt=0;int i;int constx,consty;constx=xfxd*(1-h);consty=yfxd*(1-k);for(i=0;i<n;i++){

*(newPoly+nxt)=scale( *(a+nxt),h,constx);nxt++;*(newPoly+nxt)=scale( *(a+nxt),k,consty);nxt++;printf("new %d %d ",*(newPoly+(nxt-1)),*(newPoly+(nxt-2)));

}setcolor(CGA_CYAN);drawPoly(newPoly,n);

}

void main(){

int i,j;int a[3][2];float h,k;int xfxd,yfxd;initialize();drawaxis();printf("enter coordinates");scanf("%d %d",&a[0][0],&a[0][1]);a[0][0]+=shftx;a[0][1]=shfty-a[0][1];printf("enter coordinates");scanf("%d %d",&a[1][0],&a[1][1]);a[1][0]+=shftx;a[1][1]=shfty-a[1][1];printf("enter coordinates");scanf("%d %d",&a[2][0],&a[2][1]);a[2][0]+=shftx;a[2][1]=shfty-a[2][1];

printf("scaling range for sx ");scanf("%f",&h);

printf("scaling range for sy ");scanf("%f",&k);

Page 20: Computer Architecture Schaum Series

printf("enter fixd points of x and y ");scanf("%d %d",&xfxd,&yfxd);

drawPoly(a,3);scalePoly(a,3,xfxd,yfxd,h,k);getch();

}

OUTPUT1:

Coordinates-(0,0) (1,1)and (5,2)

Scale-x=3

Scale y=1

Fixed points-(5,2)

OUTPUT 2:

OUTPUT 3:

Page 21: Computer Architecture Schaum Series

Write program to perform line clipping using:

Cohen Sutherland algorithm

#include<stdio.h>#include<conio.h>#include<graphics.h>#include<stdlib.h>

#define ROUND(a) ((int)(a+0.5))#define LEFT_EDGE 0X1#define RIGHT_EDGE 0X2#define BOTTOM_EDGE 0X4#define TOP_EDGE 0X8#define INSIDE(a) (!a)#define REJECT(a,b) (a&b)#define ACCEPT(a,b) (!(a|b))

struct wcPt2{ float x; float y;} wcPt2,a1,a2;

struct dcpt{ float x; float y;} dcpt,bmin,bmax;

Page 22: Computer Architecture Schaum Series

unsigned char encode(struct wcPt2 pt,struct dcpt winMin,struct dcpt winMax){ unsigned char code=0x00; if(pt.x<winMin.x) code=code|LEFT_EDGE; if(pt.x>winMax.x) code=code|RIGHT_EDGE; if(pt.y<winMin.y) code=code|BOTTOM_EDGE; if(pt.y>winMax.y) code=code|TOP_EDGE; return(code);}

void swapPts(struct wcPt2 *p1,struct wcPt2 *p2){ struct wcPt2 tmp; tmp=*p1; *p1=*p2; *p2=tmp;}

void swapCodes(unsigned char *c1,unsigned char *c2){ unsigned char tmp; tmp=*c1; *c1=*c2; *c2=tmp;}

void clipLine(struct dcpt winMin,struct dcpt winMax,struct wcPt2 p1,struct wcPt2 p2){

unsigned char code1,code2;int done=0, draw =0;float m;while(!done){

code1=encode(p1,winMin,winMax);code2=encode(p2,winMin,winMax);if(ACCEPT(code1,code2)){

done=1;draw= 1;

}elseif(REJECT(code1,code2))

done =1;else{

if(INSIDE(code1)){

Page 23: Computer Architecture Schaum Series

swapPts(&p1,&p2);swapCodes(&code1,&code2);

}if(p2.x!=p1.x)m=(p2.y-p1.y)/(p2.x-p1.x);if(code1 & LEFT_EDGE){

p1.y+=(winMin.x-p1.x)*m;p1.x=winMin.x;

}elseif(code1 & RIGHT_EDGE){

p1.y+=(winMax.x-p1.x)*m;p1.x=winMax.x;

}elseif(code1 & BOTTOM_EDGE){

if(p2.x!=p1.x)p1.x+=(winMin.y-p1.y)/m;p1.y=winMin.y;

}else if(code1 & TOP_EDGE){

if(p2.x!=p1.x)p1.x+=(winMax.y-p1.y)/m; p1.x+=(winMax.y-p1.y)/m;p1.y=winMax.y;

}}

} if(draw) line(ROUND(p1.x)+320,240-ROUND(p1.y),ROUND(p2.x)+320,240-ROUND(p2.y)); }

void main(void) { int gdriver=DETECT,gmode,errorcode;

int xmax,ymax; int x1,y1,x2,y2; clrscr(); initgraph(&gdriver,&gmode,"c:\\BGI"); errorcode=graphresult(); if(errorcode != grOk) { printf("graphics error",grapherrormsg(errorcode)); printf("press any key to halt"); getch(); exit(1); }

Page 24: Computer Architecture Schaum Series

xmax=getmaxx(); ymax=getmaxy(); line(0,ymax/2,xmax,ymax/2); line(xmax/2,0,xmax/2,ymax); //x1=x1+(xmax/2); // y1=(ymax/2)-y1; //x2=x2+(xmax/2); //y2=(ymax/2)-y2; a1.x=-60; a1.y=-0; a2.x=10; a2.y=60; bmin.x=-50; bmin.y=-50; bmax.x=50; bmax.y=50;

clrscr();line(a1.x+(xmax/2),(ymax/2)-a1.y,a2.x+(xmax/2),(ymax/2)-a2.y);rectangle(bmin.x+(xmax/2),(ymax/2)-bmin.y,bmax.x+(xmax/2),(ymax/2)-bmax.y);

getch();cleardevice();//clrscr();rectangle(bmin.x+(xmax/2),(ymax/2)-bmin.y,bmax.x+(xmax/2),(ymax/2)-bmax.y);

clipLine(bmin,bmax,a1,a2); getch(); closegraph(); }

Page 25: Computer Architecture Schaum Series

Liang Baskey algorithm

#include <stdio.h>#include<conio.h>#include<graphics.h>#include<stdlib.h>

#define ROUND(a) ((int)(a+0.5))

struct wcPt2{ float x; float y;} wcPt2,a1,a2;

struct dcpt{ float x; float y;} dcpt,bmin,bmax;

int clipTest(float p,float q,float *u1,float *u2){ float r; int retVal=1; if(p<0.0)

Page 26: Computer Architecture Schaum Series

{ r=q/p; if(r>*u2) retVal=0; else if(r>*u1) *u1=r; } else if(p>0.0) { r=q/p; if(r<*u1) retVal=0; else if(r<*u2) *u2=r; } else if(q<0.0) retVal=0; return(retVal); }

void clipLine(struct dcpt winMin,struct dcpt winMax,struct wcPt2 p1,struct wcPt2 p2) { float u1=0.0,u2=1.0,dx=p2.x-p1.x,dy; if(clipTest(-dx,p1.x-winMin.x,&u1,&u2)) if(clipTest(dx,winMax.x-p1.x,&u1,&u2)) { dy=p2.y-p1.y; if(clipTest(-dy,p1.y-winMin.y,&u1,&u2)) if(clipTest(dy,winMax.y-p1.y,&u1,&u2)) { if(u2<1.0) {

p2.x=p1.x+u2*dx;p2.y=p1.y+u2*dy;

} if(u1>0.0) {

p1.x+=u1*dx;p1.y+=u1*dy;

} // line(ROUND(p1.x)+320,240-ROUND(p1.y),ROUND(p2.x)+320,240-ROUND(p2.y));

line(ROUND(p1.x),ROUND(p1.y),ROUND(p2.x),ROUND(p2.y)); } } }

void main() { int gdriver=DETECT,gmode,errorcode; int xmax,ymax;

Page 27: Computer Architecture Schaum Series

int x1,x2,y1,y2; clrscr(); initgraph(&gdriver,&gmode,"c:\\TURBOC3\\BGI"); errorcode=graphresult(); if(errorcode!=grOk) { printf("graphics error",grapherrormsg(errorcode)); printf("press any key to halt"); getch(); exit(1); }

xmax=getmaxx(); ymax=getmaxy(); line(0,ymax/2,xmax,ymax/2); line(xmax/2,0,xmax/2,ymax); x1=x1+(xmax/2); y1=(ymax/2)-y1; x2=x2+(xmax/2); y2=(ymax/2)-y2; a1.x=-60; a1.y=0; a2.x=10; a2.y=60; bmin.x= -50; bmin.y=-50; bmax.x=50; bmax.y=50; clrscr(); line(a1.x+(xmax/2),(ymax/2)-a1.y,a2.x+(xmax/2),(ymax/2)-a2.y); rectangle(bmin.x+(xmax/2),(ymax/2)-bmin.y,bmax.x+(xmax/2),(ymax/2)-bmax.y); getch(); cleardevice(); rectangle(bmin.x+(xmax/2),(ymax/2)-bmin.y,bmax.x+(xmax/2),(ymax/2)-bmax.y);

clipLine(bmin,bmax,a1,a2); getch(); closegraph(); }

Page 28: Computer Architecture Schaum Series
Page 29: Computer Architecture Schaum Series