Vous êtes sur la page 1sur 50

/* PROGRAM TO DRAW A LINE USING DDA ALGORITHM */

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

float round(float a);


void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode ;
int x1,y1,x2,y2,steps,k;
float xincr,yincr,x,y,dx,dy;
clrscr( );

printf("Enter the coordinates of starting point:");


scanf("%d%d",&x1,&y1);
printf("Enter the coordinates of ending point:");
scanf("%d%d",&x2,&y2);

/* initialize graphics mode */


initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

dx = x2-x1;
dy = y2-y1;
if(abs(dx) > abs(dy))
steps = abs(dx);
else
steps = abs(dy);
xincr = dx/steps;
yincr = dy/steps;
x = x1;
y = y1;
putpixel(x,y,WHITE);
for(k=1;k<=steps;k++)
{
delay(100);
x = x+xincr;
y = y+yincr;
putpixel(round(x),round(y),WHITE);
}
outtextxy(200,20,"ILLUSTRATION OF DDA ALGORITHM");
outtextxy(x1+5,y1-5,"(x1,y1)");
outtextxy(x2+5,y2+5,"(x2,y2)");
getch( );
closegraph( );
restorecrtmode( );
}
float round(float a)
{
int b = a+0.5;
return(floor(b));
}

OUTPUT :

Enter the coordinates of starting point:200


100
Enter the coordinates of ending point:300
100
/* PROGRAM TO DRAW LINE : BRESENHAM'S ALGORITHM */

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

void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode ;
int x1,y1,x2,y2,dx,dy,p,k,x,y;
clrscr( );

printf("Enter the coordinates of starting point:");


scanf("%d%d",&x1,&y1);
printf("Enter the coordinates of ending point:");
scanf("%d%d",&x2,&y2);

/* initialize graphics mode */


initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

dx = x2-x1;
dy = y2-y1;
p = 2*dy-dx;
x = x1;
y = y1;

putpixel(x,y,15);
for(k=1;k<=dx;k++)
{
if(p<0)
{
x = x+1;
putpixel(x,y,15);
p = p+2*dy;
}
else
{
x = x+1;
y = y+1;
putpixel(x,y,15);
p = p+2*dy-2*dx;
}
}
outtextxy(200,10,"ILLUSTRATION OF BRESENHAM'S ALGORITHM");
outtextxy(x1+5,y1-5,"(x1,y1)");
outtextxy(x2+5,y2+5,"(x2,y2)");
getch( );
closegraph( );
restorecrtmode( );
}

OUTPUT :

Enter the coordinates of starting point:200


100
Enter the coordinates of ending point:300
180
/* PROGRAM TO DRAW A CIRCLE : MIDPOINT CIRCLE*/
/* ALGORITHM */

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

void symmetry(int xc,int yc,int x,int y);


void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode ;
int xc,yc,r,p,x,y;
clrscr( );

printf("Enter the coordinates of center of circle:");


scanf("%d%d",&xc,&yc);
printf("Enter the radius of circle:");
scanf("%d",&r);

/* initialize graphics mode */


initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

x = 0;
y = r;
symmetry(xc,yc,x,y);
p = 1-r;
while(x<=y)
{
if(p<0)
{
x = x+1;
p = p+2*x+1;
}
else
{
x = x+1;
y = y-1;
p = p+2*x+1-2*y;
}
symmetry(xc,yc,x,y);
}
outtextxy(120,20,"ILLUSTRATION OF MIDPOINT CIRCLE ALGORITHM");
outtextxy(xc-25,yc,"(xc,yc)");
getch( );
closegraph( );
restorecrtmode( );
}
void symmetry(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc-y,WHITE);
putpixel(xc+y,yc+x,WHITE);
putpixel(xc-y,yc+x,WHITE);
putpixel(xc+y,yc-x,WHITE);
putpixel(xc-y,yc-x,WHITE);
}
OUTPUT:

Enter the coordinates of center of circle:320


240
Enter the radius of circle:100
/* PROGRAM TO DRAW CIRCLE : BRESENHAM'S CIRCLE*/
/*ALGORITHM */

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

void symmetry(int xc,int yc,int x,int y);


void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode ;
int xc,yc,r,s,x,y;
clrscr( );

printf("Enter the coordinates of center of circle:");


scanf("%d%d",&xc,&yc);
printf("Enter the radius of circle:");
scanf("%d",&r);

/* initialize graphics mode */


initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

x = 0;
y = r;
symmetry(xc,yc,x,y);
s = 3-2*r;
while(x<=y)
{
if(s<0)
{
x = x+1;
s = s+4*x+6;
}
else
{
x = x+1;
y = y-1;
s = s+4*(x-y)+10;
}
symmetry(xc,yc,x,y);
}
outtextxy(120,20,"ILLUSTRATION OF BRESENHAM'S CIRCLE
ALGORITHM");
outtextxy(xc-25,yc,"(xc,yc)");
getch( );
closegraph( );
restorecrtmode( );
}
void symmetry(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc-y,WHITE);
putpixel(xc+y,yc+x,WHITE);
putpixel(xc-y,yc+x,WHITE);
putpixel(xc+y,yc-x,WHITE);
putpixel(xc-y,yc-x,WHITE);
}
OUTPUT :

Enter the coordinates of center of circle:300


200
Enter the radius of circle:80
/* PROGRAM TO DRAW AN ELLIPSE: MIDPOINT ELLIPSE*/
/*ALGORITHM */

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

void symmetry(int xc,int yc,int x,int y);


void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode ;
int xc,yc,a,b,x,y,fx,fy,p;
int aa = a*a,bb = b*b,aa2 = aa*2,bb2 = bb*2;
clrscr( );

printf("Enter the coordinates of center of ellipse:");


scanf("%d%d",&xc,&yc);
printf("Enter the x-radius and y-radius of ellipse:");
scanf("%d%d",&a,&b);

/* initialize graphics mode */


initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

x = 0; /*starting point*/
y = b;
symmetry(xc,yc,x,y);
fx = 0; /*initial partial derivatives*/
fy = aa2*y;
p = bb-aa*b+(0.25*aa); /*compute and round off p1*/

while(fx < fy) /* |slope|<1 */


{
x++;
fx = fx+bb2;
if(p<0)
p = p+fx+bb;
else
{
y--;
fy = fy-aa2;
p = p+fx+bb-fy;
}
symmetry(xc,yc,x,y);
}
p = bb*(x+0.5)*(x+0.5)+aa*(y-1)*(y-1)-aa*bb;
while(y>0)
{
y--;
fy = fy-aa2;
if(p>=0)
p = p-fy+aa;
else
{
x++;
fx = fx+bb2;
p=p+fx-fy+aa;
}
symmetry(xc,yc,x,y);
}
outtextxy(120,20,"ILLUSTRATION OF MIDPOINT ELLIPSE ALGORITHM");
outtextxy(xc-25,yc,"(xc,yc)");
getch( );
closegraph( );
restorecrtmode( );
}
void symmetry(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,WHITE);
putpixel(xc-x,yc+y,WHITE);
putpixel(xc+x,yc-y,WHITE);
putpixel(xc-x,yc-y,WHITE);
}
OUTPUT:

Enter the coordinates of center of ellipse:320


240
Enter the x-radius and y-radius of ellipse:100
80
/*ROTATION OF LINE ABOUT ORIGIN AND FIXED POINT*/

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

void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode,ch ;
float x1,y1,x2,y2,a,x,y;

/* initialize graphics and local variables */


initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
do
{
printf("\n######### MAIN MENU #########\n");
printf("\n1.Rotation about origin\n");
printf("2.Rotation about fixed point\n");
printf("Enter your choice:0 for exit\n");
scanf("%d",&ch);
if(ch= =0)
{
getch( );
exit(1);
}
printf("Enter the value of line coordinates:");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
printf("Enter the value of angle of rotation:");
scanf("%f",&a);

switch(ch)
{
case 1: cleardevice( );
/* draw the original line */
outtextxy(250,20,"LINE BEFORE ROTATION");
line(x1,y1,x2,y2);
a = a*(3.14/180);
x1 = x1*cos(a)-y1*sin(a);
y1 = x1*sin(a)+y1*cos(a);
x2 = x2*cos(a)-y2*sin(a);
y2 = x2*sin(a)+y2*cos(a);
getch( );
cleardevice( );
outtextxy(250,20,"LINE AFTER ROTATION");
line(x1,y1,x2,y2);
getch( );
cleardevice( );
break;

case 2: printf("Enter the fixed point:");


scanf("%f%f",&x,&y);
cleardevice( );
/* draw the original line */
outtextxy(250,20,"LINE BEFORE ROTATION");
line(x1,y1,x2,y2);
a = a*(3.14/180);
x1 = x1*cos(a)-y1*sin(a)-x*cos(a)+y*sin(a)+x;
y1 = x1*sin(a)+y1*cos(a)-x*sin(a)-y*cos(a)+y;
x2 = x2*cos(a)-y2*sin(a)-x*cos(a)+y*sin(a)+x;
y2 = x2*sin(a)+y2*cos(a)-x*sin(a)-y*cos(a)+y;
getch( );
cleardevice( );
outtextxy(250,20,"LINE AFTER ROTATION");
line(x1,y1,x2,y2);
getch( );
cleardevice( );
break;

default:printf("Invalid choice");
cleardevice( );
}
}while(ch!=0);
getch( );
closegraph( );
restorecrtmode( );
}
OUTPUT:
######### MAIN MENU #########

1.Rotation about origin


2.Rotation about fixed point
Enter your choice:0 for exit :1
Enter the value of line coordinates:200
100
320
220
Enter the value of angle of rotation:20
######### MAIN MENU #########

1.Rotation about origin


2.Rotation about fixed point
Enter your choice:0 for exit :2
Enter the value of line coordinates:220
80
350
160
Enter the value of angle of rotation:20
Enter the fixed point:220
80
/* SCALING OF LINE ABOUT ORIGIN AND FIXED POINT */

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

void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode ;
int x1,y1,x2,y2,a,b,h,k,ch;

/* initialize graphics and local variables */


initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
do
{
printf("\n######### MAIN MENU #########\n");
printf("\n1.Scaling about origin\n");
printf("2.Scaling about fixed point\n");
printf("Enter your choice:0 for exit\n");
scanf("%d",&ch);
if(ch= =0)
{
getch( );
exit(1);
}
printf("Enter the value of line coordinates:");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("Enter the value of x-scaling factor:");
scanf("%d",&a);
printf("Enter the value of y-scaling factor:");
scanf("%d",&b);

switch(ch)
{
case 1: cleardevice( );
/* draw the original line */
outtextxy(200,20,"LINE BEFORE SCALING");
line(x1,y1,x2,y2);
x1 = x1*a;
y1 = y1*b;
x2 = x2*a;
y2 = y2*b;
getch( );
cleardevice( );
outtextxy(200,20,"LINE AFTER SCALING");
line(x1,y1,x2,y2);
getch( );
cleardevice( );
break;

case 2: printf("Enter the fixed point:");


scanf("%d%d",&h,&k);
cleardevice( );
/* draw the original line */
outtextxy(200,20,"LINE BEFORE SCALING");
line(x1,y1,x2,y2);
x1 = x1*a-h*a+h;
y1 = y1*b-k*b+k;
x2 = x2*a-h*a+h;
y2 = y2*b-k*b+k;
getch( );
cleardevice( );
outtextxy(200,20,"LINE AFTER SCALING");
line(x1,y1,x2,y2);
getch( );
cleardevice( );
break;

default:printf("Invalid choice");
cleardevice( );
}
}while(ch!=0);
getch( );
closegraph( );
restorecrtmode( );
}
OUTPUT:
######### MAIN MENU #########

1.Scaling about origin


2.Scaling about fixed point
Enter your choice:0 for exit
1
Enter the value of line coordinates:
100
100
180
180
Enter the value of x-scaling factor:2
Enter the value of y-scaling factor:1
######### MAIN MENU #########

1.Scaling about origin


2.Scaling about fixed point
Enter your choice:0 for exit
1
Enter the value of line coordinates:
100
300
200
200
Enter the value of x-scaling factor:2
Enter the value of y-scaling factor:2
Enter the fixed point:100
300
######### MAIN MENU #########

1.Scaling about origin


2.Scaling about fixed point
Enter your choice:0 for exit
0
/*TRANSLATION OF LINE */

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

void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode ;
int x1,y1,x2,y2,a,b;

/* initialize graphics and local variables */


initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

printf("Enter the value of line coordinates:");


scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

printf("Enter the value of x-translation factor:");


scanf("%d",&a);
printf("Enter the value of y-translation factor:");
scanf("%d",&b);
cleardevice( );
outtextxy(200,20,"LINE BEFORE TRANSLATION");
line(x1,y1,x2,y2);
x1 = x1+a;
y1 = y1+b;
x2 = x2+a;
y2 = y2+b;
getch( );
cleardevice( );
outtextxy(200,20,"LINE AFTER TRANSLATION");
line(x1,y1,x2,y2);

getch( );
closegraph( );
restorecrtmode( );
}
OUTPUT:

Enter the value of line coordinates:


200
100
350
100
Enter the value of x-translation factor:50
Enter the value of y-translation factor:100
/* SHEARING OF LINE */

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

void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode;
int x1,y1,x2,y2,a,b;

/* initialize graphics and local variables */


initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

printf("Enter the value of line coordinates:");


scanf("%d%d%d%d",&x1,&y1,&x2,&y2);

printf("Enter the value of x-shearing factor:");


scanf("%d",&a);
printf("Enter the value of y-shearing factor:");
scanf("%d",&b);
cleardevice( );
outtextxy(200,20,"LINE BEFORE SHEARING");
line(x1,y1,x2,y2);
x1 = x1+a*y1;
y1 = b*x1+y1;
x2 = x2+a*y2;
y2 = b*x2+y2;
getch( );
cleardevice( );
outtextxy(200,20,"LINE AFTER SHEARING");
line(x1,y1,x2,y2);

getch( );
closegraph( );
restorecrtmode( );
}
OUTPUT:

Enter the value of line coordinates:


20
20
100
100
Enter the value of x-shearing factor:1
Enter the value of y-shearing:1
/*REFLECTION OF LINE ABOUT X-AXIS, Y-AXIS AND*/
/*ARBITARY LINE*/

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

void main( )
{
/* request auto detection */
int gdriver = DETECT, gmode ;
float x1,y1,x2,y2,x3,y3,x4,y4,m,n,b;
int ch,x,y;

/* initialize graphics and local variables */


initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

do
{
printf("\n######### MAIN MENU #########\n");
printf("\n1.Reflection about x-axis\n");
printf("2.Reflection about y-axis\n");
printf("3.Reflection about arbitary line\n");
printf("Enter your choice:0 for exit\n");
scanf("%d",&ch);
if(ch==0)
{
getch( );
exit(1);
}

printf("Enter the value of line coordinates:");


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

switch(ch)
{
case 1: cleardevice( );
/* draw the original line */
outtextxy(250,10,"LINE BEFORE REFLECTION");
x = getmaxx( );
y = getmaxy( );
line(0,y/2,x,y/2);
line(x/2,0,x/2,y);
line(x1+x/2,y1+y/2,x2+x/2,y2+y/2);
x1 = x1+x/2;
y1 = y1+y/2-2*y1;
x2 = x2+x/2;
y2 = y2+y/2-2*y2;
getch( );
cleardevice( );
outtextxy(200,10,"LINE AFTER REFLECTION");
line(x1,y1,x2,y2);
line(0,y/2,x,y/2);
line(x/2,0,x/2,y);
getch( );
cleardevice( );
break;

case 2: cleardevice( );
/* draw the original line */
outtextxy(200,10,"LINE BEFORE REFLECTION");
x = getmaxx( );
y = getmaxy( );
line(0,y/2,x,y/2);
line(x/2,0,x/2,y);
line(x1+x/2,y1+y/2,x2+x/2,y2+y/2);
x1 = x1+x/2-2*x1;
y1 = y1+y/2;
x2 = x2+x/2-2*x2;
y2 = y2+y/2;
getch( );
cleardevice( );
outtextxy(200,10,"LINE AFTER REFLECTION");
line(x1,y1,x2,y2);
line(0,y/2,x,y/2);
line(x/2,0,x/2,y);
getch( );
cleardevice( );
break;

case 3: printf("Enter the arbitary line:");


scanf("%f%f%f%f",&x3,&y3,&x4,&y4);
cleardevice( );
/* draw the original line */
outtextxy(200,10,"LINE BEFORE REFLECTION");
line(x1,y1,x2,y2);
line(x3,y3,x4,y4);
m = (y4-y3)/(x4-x3);
b = y3-m*x3;
n = m*m+1;
x1 = (1-m*m)*x1/n+2*m*y1/n-2*b*m/n;
y1 = (m*m-1)*y1/n+2*m*x1/n+2*b/n;
x2 = (1-m*m)*x2/n+2*m*y2/n-2*b*m/n;
y2 = (m*m-1)*y2/n+2*m*x2/n+2*b/n;
getch( );
cleardevice( );
outtextxy(200,10,"LINE AFTER REFLECTION");
line(x1,y1,x2,y2);
line(x3,y3,x4,y4);
getch( );
cleardevice( );
break;

default:printf("Invalid choice");
cleardevice( );
}
}while(ch!=0);
getch( );
closegraph( );
restorecrtmode( );

}
OUTPUT:

######### MAIN MENU #########

1.Reflection about x-axis


2.Reflection about y-axis
3.Reflection about arbitary line
Enter your choice:0 for exit
1
Enter the value of line coordinates:
10
10
100
100
######### MAIN MENU #########

1.Reflection about x-axis


2.Reflection about y-axis
3.Reflection about arbitary line
Enter your choice:0 for exit
0
/*COHEN SUTHERLAND LINE CLIPPING*/

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

#define TRUE 1
#define FALSE 0
typedef unsigned int outcode;
outcode CompOutCode(float x,float y);

enum { TOP = 0x1,


BOTTOM = 0x2,
RIGHT = 0x4,
LEFT = 0x8
};
float xmin,xmax,ymin,ymax;
void clip(float x0,float y0,float x1,float y1)
{
outcode outcode0,outcode1,outcodeOut;
int accept = FALSE,done = FALSE;
outcode0 = CompOutCode(x0,y0);
outcode1 = CompOutCode(x1,y1);
do
{
if(!(outcode0|outcode1))
{
accept = TRUE;
done = TRUE;
}
else
if(outcode0 & outcode1)
done = TRUE;
else
{
float x,y;
outcodeOut = outcode0?outcode0:outcode1;
if(outcodeOut & TOP)
{
x = x0+(x1-x0)*(ymax-y0)/(y1-y0);
y = ymax;
}
else
if(outcodeOut & BOTTOM)
{
x = x0+(x1-x0)*(ymin-y0)/(y1-y0);
y = ymin;
}
else
if(outcodeOut & RIGHT)
{
y = y0+(y1-y0)*(xmax-x0)/(x1-x0);
x = xmax;
}
else
{
y = y0+(y1-y0)*(xmin-x0)/(x1-x0);
x = xmin;
}
if(outcodeOut==outcode0)
{
x0 = x;
y0 = y;
outcode0 = CompOutCode(x0,y0);
}
else
{
x1 = x;
y1 = y;
outcode1 = CompOutCode(x1,y1);
}
}
}while(done==FALSE);
if(accept)
line(x0,y0,x1,y1);
outtextxy(200,20,"LINE AFTER CLIPPING");
rectangle(xmin,ymin,xmax,ymax);
}
outcode CompOutCode(float x,float y)
{
outcode code = 0;
if(y>ymax)
code|=TOP;
else
if(y<ymin)
code|=BOTTOM;
if(x>xmax)
code|=RIGHT;
else
if(x<xmin)
code|=LEFT;
return code;
}

void main( )
{
float x1,y1,x2,y2;
int gdriver = DETECT, gmode ;

printf("\nEnter the endpoints of line\n");


scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
printf("Enter the rectangular coordinates of clipping window\n");
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);

/* initialize graphics and local variables */


initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

outtextxy(200,20,"LINE BEFORE CLIPPING");


line(x1,y1,x2,y2);
rectangle(xmin,ymin,xmax,ymax);
getch( );
cleardevice( );
clip(x1,y1,x2,y2);
getch( );
restorecrtmode( );
}
OUTPUT:

Enter the endpoints of line


100
80
470
340
Enter the rectangular coordinates of clipping window
180
150
300
280
/* WINDOW TO VIEWPORT TRANSFORMATION */

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

void main( )
{
int wxmin,wymin,wxmax,wymax,vxmin,vymin,vxmax,vymax ;
/* request auto detection */
int gdriver = DETECT, gmode , i , n, poly[14],poly1[14];
float sx,sy;

/* initialize graphics and local variables */


initgraph(&gdriver, &gmode, "c:\\tc\\bgi");

printf("Enter the coordinates of window\nwxmin = ");


scanf("%d",&wxmin);
printf("\nwymin = ");
scanf("%d",&wymin);
printf("\nwxmax = ");
scanf("%d",&wxmax);
printf("\nwymax = ");
scanf("%d",&wymax);
printf("Enter the coordinates of viewport\nvxmin = ");
scanf("%d",&vxmin);
printf("\nvymin = ");
scanf("%d",&vymin);
printf("\nvxmax = ");
scanf("%d",&vxmax);
printf("\nvymax = ");
scanf("%d",&vymax);
printf("\nEnter the no of sides of polygon:");
scanf("%d",&n);
printf("Enter the coordinates of polygon:\n");
for(i=0;i<2*n;i++)
{
scanf("%d",&poly[i]);
}
cleardevice( );

poly[2*n] = poly[0];
poly[2*n+1] = poly[1];
/* draw the polygon */
drawpoly(n+1, poly);
rectangle(wxmin,wymin,wxmax,wymax);

sx = (vxmax-vxmin)/(wxmax-wxmin);
sy = (vymax-vymin)/(wymax-wymin);

for(i=0;i<n;i++)
{
poly1[2*i] = sx*(poly[2*i]-wxmin)+vxmin;
poly1[2*i+1] = sx*(poly[2*i+1]-wymin)+vymin;
}
poly1[2*n] = poly1[0];
poly1[2*n+1] = poly1[1];
rectangle(vxmin,vymin,vxmax,vymax);
outtextxt(150,10,” WINDOW TO VIEWPORT TRANSFORMATION”);
drawpoly(n+1,poly1);
/* clean up */
getch( );
closegraph( );
return 0;
}

OUTPUT:

Enter the coordinates of window


wxmin = 30
wymin = 30
wxmax = 150
wymax = 150
Enter the coordinates of viewport
vxmin = 160
vymin = 160
vxmax = 450
vymax = 450
Enter the no of sides of polygon:3
Enter the coordinates of polygon:
100
100
130
120
50
140
/*SUTHERLAND-HODGEMAN POLYGON CLIPPING*/

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

#define TRUE 1
#define FALSE 0

typedef unsigned int outcode;


outcode CompOutCode(float x,float y);

enum { TOP = 0x1,


BOTTOM = 0x2,
RIGHT = 0x4,
LEFT = 0x8
};
float xmin,xmax,ymin,ymax;

void clip(float x0,float y0,float x1,float y1)


{
outcode outcode0,outcode1,outcodeOut;
int accept = FALSE,done = FALSE;
outcode0 = CompOutCode(x0,y0);
outcode1 = CompOutCode(x1,y1);
do
{
if(!(outcode0|outcode1))
{
accept = TRUE;
done = TRUE;
}
else
if(outcode0 & outcode1)
done = TRUE;
else
{
float x,y;
outcodeOut = outcode0?outcode0:outcode1;
if(outcodeOut & TOP)
{
x = x0+(x1-x0)*(ymax-y0)/(y1-y0);
y = ymax;
}
else
if(outcodeOut & BOTTOM)
{
x = x0+(x1-x0)*(ymin-y0)/(y1-y0);
y = ymin;
}
else
if(outcodeOut & RIGHT)
{
y = y0+(y1-y0)*(xmax-x0)/(x1-x0);
x = xmax;
}
else
{
y = y0+(y1-y0)*(xmin-x0)/(x1-x0);
x = xmin;
}
if(outcodeOut==outcode0)
{
x0 = x;
y0 = y;
outcode0 = CompOutCode(x0,y0);
}
else
{
x1 = x;
y1 = y;
outcode1 = CompOutCode(x1,y1);
}
}
}while(done==FALSE);
if(accept)
line(x0,y0,x1,y1);
outtextxy(150,20,"POLYGON AFTER CLIPPING");
rectangle(xmin,ymin,xmax,ymax);

outcode CompOutCode(float x,float y)


{
outcode code = 0;
if(y>ymax)
code|=TOP;
else
if(y<ymin)
code|=BOTTOM;
if(x>xmax)
code|=RIGHT;
else
if(x<xmin)
code|=LEFT;
return code;
}

void main( )
{
float x1,y1,x2,y2;
/* request auto detection */
int gdriver = DETECT, gmode, n,poly[14],i;
clrscr( );
printf("Enter the no of sides of polygon:");
scanf("%d",&n);
printf("\nEnter the coordinates of polygon\n");
for(i=0;i<2*n;i++)
{
scanf("%d",&poly[i]);
}
poly[2*n]=poly[0];
poly[2*n+1]=poly[1];
printf("Enter the rectangular coordinates of clipping window\n");
scanf("%f%f%f%f",&xmin,&ymin,&xmax,&ymax);

/* initialize graphics and local variables */


initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
outtextxy(150,20,"POLYGON BEFORE CLIPPING");
drawpoly(n+1,poly);
rectangle(xmin,ymin,xmax,ymax);
getch( );
cleardevice( );
for(i=0;i<n;i++)
clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]);
getch( );
restorecrtmode( );
}

OUTPUT:
Enter the no of sides of polygon:5
Enter the coordinates of polygon
50
50
200
100
350
350
80
200
40
80
Enter the rectangular coordinates of clipping window
150
150
300
300
INDEX
1. Program to draw a line : DDA Algorithm.
2. Program to draw a line : Bresenham’s Algorithm.
3. Program to draw a circle : Midpoint Circle Algorithm.
4. Program to draw a circle : Bresenham’s Circle Algorithm.
5. Program to draw an ellipse : Midpoint Ellipse Algorithm.
6. Rotation of line about origin and fixed point.
7. Scaling of line about origin and fixed point.
8. Translation of line.
9. Shearing of line.
10. Reflection of line about X-axis,Y-axis and arbitary line.
11. Program to clip a line : Cohen Sutherland Algorithm.
12. Window to Viewport transformation.

Vous aimerez peut-être aussi