cg practicals

59
Computer graphics Enrollment no:100050131030 PRACTICAL:4 Aim: Write a program to implement 2D translation, rotation, scaling. Program Code: #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> void translate(); void scalling(); void rotate(); void main() { int gd=DETECT,gm; int c; initgraph(&gd,&gm,"C:\\TC\\BGI"); printf("\n 1. translation:"); printf("\n 2. scalling:"); printf("\n 3. rotate:"); printf("\n enter choice:"); scanf("%d",&c); switch(c) 1

Upload: yello-mazda

Post on 12-Nov-2014

84 views

Category:

Documents


2 download

DESCRIPTION

CG Practicals for RJ Engineering Institute.

TRANSCRIPT

Page 1: CG Practicals

Computer graphics Enrollment no:100050131030

PRACTICAL:4

Aim: Write a program to implement 2D translation, rotation, scaling.

Program Code:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<math.h>

void translate();

void scalling();

void rotate();

void main()

{

int gd=DETECT,gm;

int c;

initgraph(&gd,&gm,"C:\\TC\\BGI");

printf("\n 1. translation:");

printf("\n 2. scalling:");

printf("\n 3. rotate:");

printf("\n enter choice:");

scanf("%d",&c);

switch(c)

{

case 1:translate();

break;

1

Page 2: CG Practicals

Computer graphics Enrollment no:100050131030

case 2:scalling();

break;

case 3:rotate();

break;

default:printf("\n invalid choice");

break;

}

getch();

closegraph();

}

void translate()

{

int tx,ty;

rectangle(100,150,150,100);

printf("\n enter tx:");

scanf("%d",&tx);

printf("\n enter ty:");

scanf("%d",&ty);

printf("after translation \n");

rectangle(100+tx,150+ty,150+tx,100+ty);

}

void scalling()

{

int sx,sy;

2

Page 3: CG Practicals

Computer graphics Enrollment no:100050131030

rectangle(100,150,150,100);

printf("\n enter sx:");

scanf("%d",&sx);

printf("\n enter sy:");

scanf("%d",&sy);

rectangle(100*sx,150*sy,150*sx,100*sy);

}

void rotate()

{

int xref,yref,x1,y1,x2,y2,x3,y3,x4,y4,a1,b1,a2,b2,a3,b3,a4,b4;

float theta,angle;

xref=100;

yref=100;

x1=150;

y1=100;

x2=150;

y2=150;

x3=100;

y3=150;

x4=100;

3

Page 4: CG Practicals

Computer graphics Enrollment no:100050131030

y4=100;

printf("\n enter angle:");

scanf("%f",&angle);

theta=angle*(3.14/180);

a1=(x1-xref)*cos(theta)-(y1-yref)*sin(theta);

b1=(x1-xref)*sin(theta)+(y1-yref)*cos(theta);

a2=(x2-xref)*cos(theta)-(y2-yref)*sin(theta);

b2=(x2-xref)*sin(theta)+(y2-yref)*cos(theta);

a3=(x3-xref)*cos(theta)-(y3-yref)*sin(theta);

b3=(x3-xref)*sin(theta)+(y3-yref)*cos(theta);

a4=(x4-xref)*cos(theta)-(y4-yref)*sin(theta);

b4=(x4-xref)*sin(theta)+(y4-yref)*cos(theta);

rectangle(100,150,150,100);

line(a1+100,b1+100,a2+100,b2+100);

line(a2+100,b2+100,a3+100,b3+100);

line(a3+100,b3+100,a4+100,b4+100);

line(a4+100,b4+100,a1+100,b1+100);

}

4

Page 5: CG Practicals

Computer graphics Enrollment no:100050131030

Program Output:

5

Page 6: CG Practicals

Computer graphics Enrollment no:100050131030

6

Page 7: CG Practicals

Computer graphics Enrollment no:100050131030

PRACTICAL:5

Aim: Write a program to implement 2D fix point rotation and fix point scaling.

Program Code:

/* ROTATION */

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

void main(){

int i,j,n,p[8],xr,yr;int ch;double angle,p1[2];int gd=DETECT,gm;

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

cleardevice();

printf("\n Enter no of vertices of polygon:");scanf("%d",&n);printf("\n Enter co ordinates ");

for(i=0;i<(2*n);i++)scanf("%d",&p[i]);p[6]=p[0];p[7]=p[1];printf("\nenter coordinates of pivot pt:");scanf("%d %d",&xr,&yr);

drawpoly((n+1),p); printf("\nCountinue(yes for 1/no for 0):"); scanf("%d",&ch);

do { cleardevice();

drawpoly((n+1),p); printf("\nenter angle:"); scanf("%lf",&angle);

angle=(3.14*angle)/180;

7

Page 8: CG Practicals

Computer graphics Enrollment no:100050131030

for(i=0;i<((2*n)+2);i=i+2){

p1[0]=(xr+((p[i]-xr)*cos(angle))-((p[i+1]-yr)*sin(angle))); p1[1]=(yr+((p[i+1]-yr)*cos(angle))+((p[i]-xr)*sin(angle))); p[i]=(int)(p1[0]); p[i+1]=(int)(p1[1]);

}drawpoly((n+1),p);

printf("\nCountinue(yes for 1/no for 0):"); scanf("%d",&ch); }

while(ch!=0); getch(); closegraph();}

Output:

8

Page 9: CG Practicals

Computer graphics Enrollment no:100050131030

9

Page 10: CG Practicals

Computer graphics Enrollment no:100050131030

10

Page 11: CG Practicals

Computer graphics Enrollment no:100050131030

/* SCALING */

#include<stdio.h>#include<conio.h>#include<graphics.h>void main(){

int gd = DETECT, gm;int i,p[8],x,y;

float sx,sy,p1[8];

initgraph(&gd, &gm, "G:\\TC\\BGI"); cleardevice();

for(i=0; i<8; i++){ printf("Enter the point p[%d]:",i+1); scanf("%d",&p[i]);}

drawpoly(4,p);

printf("Enter the Sx & Sy:"); scanf("%f %f",&sx,&sy); printf("Enter the fixed point :"); scanf("%d %d",&x,&y);

if(sx!=0 && sy==0){ for(i=0; i<8; i=i+2)

{ p1[i]= (sx*p[i])+(x*(1-sx)); // p1[i+1]= (sy*p[i+1])+(y*(1-sy)); p[i]=p1[i]; // p[i+1]=p1[i+1];} drawpoly(4,p);

}

if(sx==0 && sy!=0){ for(i=0; i<8; i=i+2)

{ // p1[i]= (sx*p[i])+(x*(1-sx));

p1[i+1]= (sy*p[i+1])+(y*(1-sy)); // p[i]=p1[i];

11

Page 12: CG Practicals

Computer graphics Enrollment no:100050131030

p[i+1]=p1[i+1];}

drawpoly(4,p);}

if(sx!=0 && sy!=0){

for(i=0; i<8; i=i+2){

p1[i]= (sx*p[i])+(x*(1-sx)); p1[i+1]= (sy*p[i+1])+(y*(1-sy)); p[i]=p1[i]; p[i+1]=p1[i+1];}drawpoly(4,p);

}

getch(); closegraph();}

12

Page 13: CG Practicals

Computer graphics Enrollment no:100050131030

Program Output:

13

Page 14: CG Practicals

Computer graphics Enrollment no:100050131030

PRACTICAL:6

Aim: Write a program to implement 2D reflection and shearing.

Program Code:

/* REFLECT */

#include<stdio.h>#include<conio.h>#include<graphics.h>#include<dos.h>void main(){

int gdriver=DETECT,gmode;int i,x,y,p[8],ch,xm=639,ym=479;int p1[8];do{

printf("\n 0>Exit");printf("\n 1>Enter the point.");printf("\n 2>Reflaction about x-axis");printf("\n 3>Reflation about y-axis");printf("\n 4>reflaction about XY-axis");printf("\n Enter your choice:");scanf("%d",&ch);

switch(ch){

case 1:

for(i=0;i<8;i++){

printf("p[%d]:",i+1);scanf("%d",&p[i]);

}

initgraph(&gdriver,&gmode,"G:\\tc\\bgi ");drawpoly(4,p);line(320,0,320,480);line(0,240,640,240);outtextxy(320,245,"(320,240)");break;

case 2:

14

Page 15: CG Practicals

Computer graphics Enrollment no:100050131030

for(i=0;i<8;i=i+2){

p1[i+1]=ym-p[i+1];p1[i]=p[i];

}drawpoly(4,p1);break;

case 3:for(i=0;i<8;i=i+2){

p1[i]=xm-p[i];p1[i+1]=p[i+1];

}drawpoly(4,p1);break;

case 4:for(i=0;i<8;i=i+2){

p1[i]=xm-p[i];p1[i+1]=ym-p[i+1];

}drawpoly(4,p1);break;

}}

while(ch!=0);getch();closegraph();

}

Output:

15

Page 16: CG Practicals

Computer graphics Enrollment no:100050131030

/* SHEARING */

16

Page 17: CG Practicals

Computer graphics Enrollment no:100050131030

#include<stdio.h>#include<conio.h>#include<math.h>#include<graphics.h>void main(){

int i,p[10],p1[10],yr,xr,c;float shx,shy;int gd=DETECT,gm;initgraph(&gd,&gm,"g:\\tc\\bgi");

// textattr(4);printf("\n Enter the coordinates: ");for(i=0;i<10;i++)

scanf("%d",&p[i]);

drawpoly(5,p);do{

printf("\n (1) X direaction");printf("\n (2) Y direction");printf("\n (3) X direction shear to Y");printf("\n (4) Y direction shear to X");printf("\n (5) Exit");printf("\n Enter ur choice: ");scanf("%d",&c);

switch(c){

case 1:printf("\n Shearing factor: ");scanf("%f",&shx);for(i=0;i<10;i=i+2){

p1[i]=p[i]+(shx*p[i+1]);p1[i+1]=p[i+1];p[i]=p1[i];p[i+1]=p1[i+1];

}drawpoly(10,p);break;

case 2:printf("\n Shearing factor: ");

17

Page 18: CG Practicals

Computer graphics Enrollment no:100050131030

scanf("%f",&shy);for(i=0;i<10;i=i+2){

p1[i]=p[i];p1[i+1]=p[i+1]+(shy*p[i]);p[i]=p1[i];p[i+1]=p1[i+1];

}drawpoly(5,p);break;

case 3:printf("\n Shearing factor: ");scanf("%f",&shx);printf("\n Reference Y: ");scanf("%d",&yr);for(i=0;i<10;i=i+2){

p1[i]=p[i]+(shx*(p[i+1]-yr));p1[i+1]=p[i+1];p[i]=p1[i];p[i+1]=p1[i+1];

}drawpoly(5,p);break;

case 4:printf("\n Shearing factor: ");scanf("%f",&shy);printf("\n Reference X: ");scanf("%d",&xr);for(i=0;i<10;i=i+2){

p1[i]=p[i];p1[i+1]=p[i+1]+(shy*(p[i]-xr));p[i]=p1[i];p[i+1]=p1[i+1];

}drawpoly(5,p);break;

case 0:break;

}}

18

Page 19: CG Practicals

Computer graphics Enrollment no:100050131030

while(c!=5);

getch();closegraph();

}

19

Page 20: CG Practicals

Computer graphics Enrollment no:100050131030

Program Output:

20

Page 21: CG Practicals

Computer graphics Enrollment no:100050131030

PRACTICAL:7

Aim: Write a program to implement DDA line drawing algorithm

Program Code:

#include <stdio.h>

#include <conio.h>

#include <graphics.h>

#include <math.h>

#include <stdlib.h>

void main()

{

float x1,x2,y1,y2,ey,ex;

int length,x,y,i;

int gd=DETECT,gm;

initgraph(&gd,&gm,"C:\\TC\\BGI");

printf("\n enter first point:(x1,y1) ");

scanf("%f%f",&x1,&y1);

printf("\n enter second point:(x2,y2) ");

scanf("%f%f",&x2,&y2);

if(abs(y2-y1)>(x2-x1))

{

length=(y2-y1);

}

else

21

Page 22: CG Practicals

Computer graphics Enrollment no:100050131030

{

length=(x2-x1);

}

ex=(x2-x1)/length;

ey=(y2-y1)/length;

x=x1;

y=y1;

i=0;

while(i<=length)

{

x=x+ex;

y=y+ey;

putpixel((int)x,(int)y,4);

i++;

}

getch();

closegraph();

}

22

Page 23: CG Practicals

Computer graphics Enrollment no:100050131030

Program Output:

23

Page 24: CG Practicals

Computer graphics Enrollment no:100050131030

PRACTICAL:8

Aim: Write a program to implement Bresenham’s line drawing algorithm.

Program Code:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

{

int gd=DETECT,gm,x1,x2,y1,y2,x,y,dx,dy,i,p;

initgraph(&gd,&gm,”C:\\TC\\BGI”);

printf(“\n enter first point:(x1,y1)”);

scanf(“%d %d”,&x1,&y1);

printf(“\n enter first point:(x2,y2)”);

scanf(“%d %d”,&x2,&y2);

dx=x2-x1;

dy=y2-y1;

p=(2*dy)-dx;

x=x1;

y=y1;

while(x<=x2)

{

if(p<0)

{

x=x+1;

24

Page 25: CG Practicals

Computer graphics Enrollment no:100050131030

y=y;

p=p+(2*dy);

}

Else

{

x=x=1;

y=y+1;

p=p+(2*dy)-(2*dx);

}

putpixel(x,y,15);

}

getch();

closegraph();

}

25

Page 26: CG Practicals

Computer graphics Enrollment no:100050131030

Program Output:

26

Page 27: CG Practicals

Computer graphics Enrollment no:100050131030

PRACTICAL:9

Aim: Write a program to implement mid-point circle algorithm.

Program Code:

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<graphics.h>

void main()

{

int xc,yc,p,r,x,y,gd=DETECT,gm;

initgraph(&gd,&gm,"C:\\TC\\BGI");

printf("\tEnter input X and Y:");

scanf("%d%d",&xc,&yc);

printf("\tEnter value of radius R:");

scanf("%d",&r);

x=0;

y=r;

p=1-r;

while(x<y)

{

if(p<0)

{

x=x+1;

y=y;

p=p+2*x+1;

27

Page 28: CG Practicals

Computer graphics Enrollment no:100050131030

}

else

{

x=x+1;

y=y-1;

p=p+2*(x-y)+1;

}

putpixel(xc+x,yc+y,3);

putpixel(xc-x,yc+y,4);

putpixel(xc-x,yc-y,5);

putpixel(xc+x,yc-y,6);

putpixel(xc+y,yc+x,7);

putpixel(xc-y,yc+x,8);

putpixel(xc-y,yc-x,9);

putpixel(xc+y,yc-x,1);

}

getch();

closegraph();

}

28

Page 29: CG Practicals

Computer graphics Enrollment no:100050131030

Program Output:

29

Page 30: CG Practicals

Computer graphics Enrollment no:100050131030

PRACTICAL:10

Aim: Write a program to implement mid-point ellipse algorithm.

Program code:

#include<stdio.h>

#include<graphics.h>

#include<math.h>

#include<conio.h>

void main()

{

int gd=DETECT,gm,xc,yc;

float p,x,y,q,a,b;

initgraph(&gd,&gm,"C:\\TC\\BGI");

printf("\n enter the center point:");

scanf("%d%d",&xc,&yc);

printf("\n enter the x radius:");

scanf("%f",&a);

printf("\n enter the y radius:");

scanf("%f",&b);

x=0;

y=b;

p=(b*b)-(a*a*b)+(0.25*a*a);

while((2*b*b*x)<=(2*a*a*y))

{

if(p<0)

{

30

Page 31: CG Practicals

Computer graphics Enrollment no:100050131030

x=x+1;

y=y;

p=p+(2*b*b*x)+(b*b);

}

else

{

x=x+1;

y=y-1;

p=p+(2*b*b*x)-(2*a*a*y)+(b*b);

}

putpixel(xc+x,yc+y,5);

putpixel(xc-x,yc-y,5);

putpixel(xc-x,yc+y,5);

putpixel(xc+x,yc-y,5);

}

//region-2:

p=((b*b)*(x+0.5)*(x+0.5))+(a*a)*(y-1)*(y-1)-(a*a*b*b);

while(y>0)

{

if(p>0)

{

x=x;

y=y-1;

31

Page 32: CG Practicals

Computer graphics Enrollment no:100050131030

p=p-(2*a*a*y)+(a*a);

}

else

{

x=x+1;

y=y-1;

p=p+(2*b*b*x)-(2*a*a*y)+(a*a);

}

putpixel(xc+x,yc+y,5);

putpixel(xc-x,yc-y,5);

putpixel(xc-x,yc+y,5);

putpixel(xc+x,yc-y,5);

}

getch();

closegraph();

}

32

Page 33: CG Practicals

Computer graphics Enrollment no:100050131030

Program Output:

33

Page 34: CG Practicals

Computer graphics Enrollment no:100050131030

PRACTICAL:-11

AIM: WAP TO FILL THE POLYGON USING BOUNDRY FILL ALGORITHAM.

Program Code:

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void boundaryfill(int,int,int,int);

void main()

{

int gm,gd=DETECT;

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

int x,y,fc,bc;

rectangle(200,200,250,250);

printf("\n Enter x Value: ");

scanf("%d",&x);

printf("\n Enter y Value: ");

scanf("%d",&y);

printf("\n Enter Fill Color Value: ");

scanf("%d",&fc);

printf("\n Enter Boundary Color Value: ");

scanf("%d",&bc);

boundaryfill(x,y,fc,bc);

getch();

closegraph();

34

Page 35: CG Practicals

Computer graphics Enrollment no:100050131030

}

void boundaryfill(int x,int y,int fc,int bc)

{

int current;

current=getpixel(x,y);

if((current!=fc)&&(current!=bc))

{

setcolor(fc);

putpixel(x,y,fc);

boundaryfill(x+1,y,fc,bc);

boundaryfill(x-1,y,fc,bc);

boundaryfill(x,y+1,fc,bc);

boundaryfill(x,y-1,fc,bc);

}

}

35

Page 36: CG Practicals

Computer graphics Enrollment no:100050131030

Program Output:-

36

Page 37: CG Practicals

Computer graphics Enrollment no:100050131030

Practical:12

Aim: Write a program to implement flood-fill polygon filling algorithm.

Program Code:

#include <stdio.h>

#include <conio.h>

#include <graphics.h>

#include <dos.h>

void flood(int,int,int,int);

main()

{

initwindow(200,200);

rectangle(50,50,100,100);

flood(55,55,12,0);

}

void flood(int x,int y, int fill_col, int old_col)

{

if(getpixel(x,y)==old_col)

{

delay(10);

putpixel(x,y,fill_col);

37

Page 38: CG Practicals

Computer graphics Enrollment no:100050131030

flood(x+1,y,fill_col,old_col);

flood(x-1,y,fill_col,old_col);

flood(x,y+1,fill_col,old_col);

flood(x,y-1,fill_col,old_col);

}

}

38

Page 39: CG Practicals

Computer graphics Enrollment no:100050131030

Program Output:

39

Page 40: CG Practicals

Computer graphics Enrollment no:100050131030

Practical:13

Aim: Write a program to implement scan line polygon filling algorithm.

Program Code:

#include<stdio.h>#include<graphics.h>#include<conio.h>void main(){

int gd=DETECT,gm,x1,y1,x2,y2,xm,ym,x,y,xc[20],yc[20],i=0,k=0,j=0;initgraph(&gd,&gm,"C:\\TC\\BGI");printf("Enter the value of x1-->");scanf("%d",&x1);printf("enter the value of y1-->");scanf("%d",&y1);printf("Enter the value of x2-->");scanf("%d",&x2);printf("Enter the value of y2-->");scanf("%d",&y2);cleardevice();

line(x1,y1,x2,y2); xm=getmaxx(); ym=getmaxy();

for(y=0;y<=ym;y++) {

for(x=0;x<=xm;x++){ if(getpixel(x,y)!=BLACK) { xc[i]=x; yc[i]=y; i++; k++; }}

}cleardevice();for(j=0;j<k;j++){

40

Page 41: CG Practicals

Computer graphics Enrollment no:100050131030

printf("(%d,%d)\n",xc[j],yc[j]);

}

getch();}

41

Page 42: CG Practicals

Computer graphics Enrollment no:100050131030

Program Output:

42

Page 43: CG Practicals

Computer graphics Enrollment no:100050131030

Practical-14

Aim:- Impliment Cohen-Sutherland Line clipping Algorithm.

Program Code:

#include<stdio.h>#include<graphics.h>#include<conio.h>typedef unsigned int outcode;enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 };void lineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax )float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax;{int gd,gm;outcode code0,code1,codeout;int accept = 0, done=0;code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);do

{if(!(code0 | code1)){ accept =1 ; done =1; }elseif(code0 & code1) done = 1;else{float x,y;codeout = code0 ? code0 : code1;if(codeout & TOP){x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0);y = ywmax;}elseif( codeout & BOTTOM){x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0);y = ywmin;}

43

Page 44: CG Practicals

Computer graphics Enrollment no:100050131030

elseif ( codeout & RIGHT){y = y0+(y1-y0)*(xwmax-x0)/(x1-x0);x = xwmax;}else{y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0);x = xwmin;}if( codeout == code0){x0 = x; y0 = y;code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);}else{x1 = x; y1 = y;code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);}}} while( done == 0);

if(accept) line(x0,y0,x1,y1);

rectangle(xwmin,ywmin,xwmax,ywmax);

getch();

}int calcode (x,y,xwmin,ywmin,xwmax,ywmax)float x,y,xwmin,ywmin,xwmax,ywmax;{int code =0;

if(y> ywmax)code |=TOP;else if( y<ywmin)code |= BOTTOM;else if(x > xwmax)

44

Page 45: CG Practicals

Computer graphics Enrollment no:100050131030

code |= RIGHT;else if ( x< xwmin)code |= LEFT;

return(code);}main(){

float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;int gd=DETECT,gm;

clrscr();initgraph(&gd,&gm,"e:\\tc\\bgi");

printf("\n\n\tEnter the co-ordinates of Line :");

printf("\n\n\tX1 Y1 : ");scanf("%f %f",&x1,&y1);

printf("\n\n\tX2 Y2 : ");scanf("%f %f",&x2,&y2);printf("\n\tEnter the co_ordinates of window :\n ");printf("\n\txwmin , ywmin : ");scanf("%f %f",&xwmin,&ywmin);printf("\n\txwmax , ywmax : ");scanf("%f %f",&xwmax,&ywmax);clrscr();line(x1,y1,x2,y2);rectangle(xwmin,ywmin,xwmax,ywmax);getch();clrscr();

lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );getch();closegraph();

}

45

Page 46: CG Practicals

Computer graphics Enrollment no:100050131030

Program Output:-

46

Page 47: CG Practicals

Computer graphics Enrollment no:100050131030

Practical:15

Aim: Write a program to implement Liang-Barsky line clipping algorithm.

Program Code:

#include<graphics.h>#include<dos.h>#include<conio.h>#include<stdlib.h> void main(){int gd=DETECT, gm ;int x1 =10, y1=10 , x2=250 , y2=250 ;int wxmin=100,wymin=100 , wxmax=300 , wymax=300 ;float u1 = 0.0 ,u2 = 1.0 ; int p1 , q1 , p2 , q2 , p3 , q3 , p4 ,q4 ; float r1 , r2 , r3 , r4 ; int x11 , y11 , x22 , y22 ; initgraph(&gd,&gm,"d:\\tc\\bgi"); clrscr(); /* printf("Enter the windows left , bottom boundry\n"); scanf("%d%d",&wxmin,&wymin); printf("Enter the windows right ,bottom boundry\n"); scanf("%d%d",&wxmax,&wymax); printf("Enter line x1 , y1 co-ordinate\n"); scanf("%d%d",&x1,&y1); printf("Enter line x2 , y2 co-ordinate\n"); scanf("%d%d",&x2,&y2); */ p1 = -(x2 - x1 ); q1 = x1 - wxmin ; p2 = ( x2 - x1 ) ; q2 = wxmax - x1 ; p3 = - ( y2 - y1 ) ; q3 = y1 - wymin ; p4 = ( y2 - y1 ) ; q4 = wymax - y1 ;

if( ( ( p1 == 0.0 ) && ( q1 < 0.0 ) ) || ( ( p2 == 0.0 ) && ( q2 < 0.0 ) ) || ( ( p3 == 0.0 ) && ( q3 < 0.0 ) ) || ( ( p4 == 0.0 ) && ( q4 < 0.0 ) ) ) {

printf("Line is rejected\n"); initgraph(&gd,&gm,"c:\\tc\\bgi"); setcolor(2);

47

Page 48: CG Practicals

Computer graphics Enrollment no:100050131030

rectangle(wxmin,wymax,wxmax,wymin); setcolor(1); line(x1,y1,x2,y2); getch(); setcolor(0); line(x1,y1,x2,y2); getch();

} else { if( p1 != 0.0 )

{r1 =(float) q1 /p1 ;if( p1 < 0 )

u1 = max(r1 , u1 );else

u2 = min(r1 , u2 ); }

if( p2 != 0.0 ) {

r2 = (float ) q2 /p2 ; if( p2 < 0 )

u1 = max(r2 , u1 );else

u2 = min(r2 , u2 ); }

if( p3 != 0.0 ){

r3 = (float )q3 /p3 ; if( p3 < 0 )

u1 = max(r3 , u1 );else

u2 = min(r3 , u2 ); }

if( p4 != 0.0 ) {

r4 = (float )q4 /p4 ; if( p4 < 0 )

u1 = max(r4 , u1 ); else

u2 = min(r4 , u2 );

48

Page 49: CG Practicals

Computer graphics Enrollment no:100050131030

} if( u1 > u2 )

printf("line rejected\n");else{

x11 = x1 + u1 * ( x2 - x1 ) ; y11 = y1 + u1 * ( y2 - y1 ) ; x22 = x1 + u2 * ( x2 - x1 ); y22 = y1 + u2 * ( y2 - y1 );

printf("Original line cordinates\n");printf("x1 = %d , y1 = %d, x2 = %d, y2 = %d\n",x1,y1,x2,y2);printf("Windows coordiante are \n");printf("wxmin = %d, wymin = %d,wxmax = %d , wymax =%d",wxmin,wymin,wxmax,wymax);printf("New coordinates are \n");printf("x1 = %d, y1 = %d,x2 = %d , y2 = %d\n",x11,y11,x22,y22);rectangle(wxmin,wymax,wxmax,wymin);setcolor(1);line(x1,y1,x2,y2);setcolor(0);line(x1,y1,x2,y2);setcolor(3);line(x11,y11,x22,y22);}

}getch();

}

49

Page 50: CG Practicals

Computer graphics Enrollment no:100050131030

OUTPUT:

50

Page 51: CG Practicals

Computer graphics Enrollment no:100050131030

51

Page 52: CG Practicals

Computer graphics Enrollment no:100050131030

52