You are on page 1of 25

SHUBHAM VARSHNEY

2K12/CO/126
V SEM
COE

INDEX
EXPERIMENT NO

EXPERIMENT
NAME

SIMPLE DDA

BRESENHAM

MID POINT CIRCLE

MID POINT ELLIPSE

COHEN
SUTHERLAND

TRANSLATION

ROTATION

REFLECTION

SCALING

DATE

SIGN

EXPERIMENT NO:1
Q: Write a program to draw a line using simple DDA line drawing algorithm.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#define round(x) (int)(x+0.5)
void simple_dda_line(int x1,int y1,int x2,int y2)
{int dx=x2-x1;
int dy=y2-y1;
int l;
if(abs(dx)>abs(dy))
l=abs(dx);
else
l=abs(dy);
double xinc=(abs(dx)/(double)l);
double yinc=(abs(dy)/(double)l);
int z=1;
double x,y;
x=x1;
y=y1;
putpixel(round(x),round(y),15);
while(z<=l){
x+=xinc;
y+=yinc;
putpixel(round(x),round(y),15);
z++;
}}
int main()
{
int x1,x2,y1,y2;
printf("\n\t Simple DDA Algorithm \t\n");
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"..\\BGI");
printf("\n Enter the coordinates for first end point\n");
scanf("%d %d",&x1,&y1);
printf("\n Enter the coordinates for second end point\n");
scanf("%d %d",&x2,&y2);
simple_dda_line(x1,y1,x2,y2);
getch();
closegraph();
return 0;}

EXPERIMENT NO:2
Q: Write a program to draw a line using Bresenham line drawing algorithm .
Code:
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#define round(x) (int)(x+0.5)
void bresline(int x1, int y1, int x2, int y2)
{
int twody = 2*abs(y2-y1);
int dx = abs(x2-x1);
int p = twody-dx;
int x, y, xend, ystep=1;
int twodydx= twody - 2*dx;
if(x1>x2)
{
x=x2;
y=y2;
xend=x1;
if(y2>y1) ystep=-1;
}
else
{
x=x1;
y=y1;
xend=x2;
if(y1>y2) ystep=-1;
}
putpixel(x,y,15);
while(x<xend)
{
x++;
if(p<0)
{
p+=twody;
}
else
{
y+=ystep;
p+=twodydx;
}

putpixel(x,y,15);
}
}
int main()
{
int x1,x2,y1,y2;
printf("\n\t BH algorithm \t\n");
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"..\\BGI");
printf("\n Enter the coordinates for first end point\n");
scanf("%d %d",&x1,&y1);
printf("\n Enter the coordinates for second end point\n");
scanf("%d %d",&x2,&y2);
getch();
getch();
bresline(x1,y1,x2,y2);
getch();
closegraph();
return 0;
}

EXPERIMENT NO:3
Q: Write a program to draw a circle using mid-point circle algorithm.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<conio.h>
void setpixel(int xc, int yc, int x, int y)
{
putpixel(xc+x,yc+y,15);
putpixel(xc+x,yc-y,15);
putpixel(xc-x,yc+y,15);
putpixel(xc-x,yc-y,15);
putpixel(xc+y,yc+x,15);
putpixel(xc+y,yc-x,15);
putpixel(xc-y,yc+x,15);
putpixel(xc-y,yc-x,15);
}
void midptcircle(int xc, int yc, int r)
{
double p = 5.0/4.0 - r;
int x= 0, y= r;
setpixel(xc,yc,x,y);
while(x<y)
{
if(p<0)
{
p+= (2.0*x) + 3.0;
}
else
{
p+= 2*(x-y) +5.0;
y--;
}
x++;
setpixel(xc,yc,x,y);
}
}
int main()
{
clrscr();
int xc, yc, r;

int gdriver = DETECT, gmode;


initgraph(&gdriver, &gmode, "..\\BGI");
printf("Enter center of circle\n");
scanf("%d %d", &xc, &yc);
printf("Enter radius\n");
scanf("%d", &r);
midptcircle(xc,yc,r);
getch();
closegraph();
return 0;
}

EXPERIMENT NO:4
Q: Write a program to draw ellipse using mid-point ellipse algorithm.
Code:
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<conio.h>
void setpixel(int xc, int yc, int x, int y)
{
putpixel(xc+x,yc+y,1);
putpixel(xc+x,yc-y,1);
putpixel(xc-x,yc+y,1);
putpixel(xc-x,yc-y,1);
}
void midptellipse(int xc, int yc, int a, int b)
{
double dz;
int x=0,y=b;
double d1=(b*b)-(a*a*b)+(0.25*a*a);
double d2;
setpixel(xc,yc,x,y);

while( (a*a*(y-0.5)) > (b*b*(x+1)) )


{
if(d1<0)
{
d1 += (b*b*((2*x)+3));
}
else
{
d1 += (b*b*((2*x)+3)) + (a*a*((-2*y)+2));
y--;
}
x++;
setpixel(xc,yc,x,y);
}

d2=b*b*(x+0.5)*(x+0.5)+a*a*(y-1)*(y-1) -a*a*b*b;
while(y>0)
{
if(d2<0)
{
d2 += (b*b*((2*y)+2)) + (a*a*((-2*y)+3));
x++;
}
else
d2 += (a*a*((-2*y)+3));
y--;
setpixel(xc,yc,x,y);
}
}
int main()
{
int xc, yc, rx, ry;
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "..\\BGI");
printf("Enter center of ellipse\n");
scanf("%d %d", &xc, &yc);
printf("Enter x and y radius\n");
scanf("%d %d", &rx, &ry);
midptellipse(xc,yc,rx,ry);
getch();
closegraph();
return 0;
}

EXPERIMENT NO:5
Q: Write a program to implement Cohen Sutherland Algorithm.
Code:
#include<graphics.h>
#include<stdio.h>
#define TOP 0x1
#define BOTTOM 0x2
#define RIGHT 0x4
#define LEFT 0x8
#define ROUND(a) ((int)(a+0.5))
void ddaline(int x1, int y1, int x2, int y2)
{
float xsteps, ysteps, x=x1, y=y1;
int dx = x2-x1;
int dy = y2-y1;
int steps,k=1;
if(abs(dx)>=abs(dy))
steps=abs(dx);
else steps=abs(dy);
xsteps= dx/(float)steps;
ysteps= dy/(float)steps;
putpixel(ROUND(x),ROUND(y),15);
while(k<=steps)
{
x+=xsteps;
y+=ysteps;
putpixel(ROUND(x), ROUND(y),15);
k++;
}
}
int calcode (float x,float y,float xwmin, float ywmin,float xwmax,float ywmax)
{
int code =0;
if(y> ywmax)
code |=TOP;
else if( y<ywmin)
code |= BOTTOM;
else if(x > xwmax)
code |= RIGHT;

else if ( x< xwmin)


code |= LEFT;
return(code);
}
void lineclip(float x0,float y0,float x1,float y1,float xwmin,float ywmin,float xwmax,float
ywmax )
{
unsigned int 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; }
else
if(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;
}
else
if( codeout & BOTTOM)
{
x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0);
y = ywmin;
}
else
if ( 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) ddaline(x0,y0,x1,y1);
ddaline(xwmin,ywmin,xwmin,ywmax);
ddaline(xwmin,ywmax,xwmax,ywmax);
ddaline(xwmax,ywmax,xwmax,ywmin);
ddaline(xwmax,ywmin,xwmin,ywmin);
getch();
}
int main()
{
float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "..\\BGI");
printf("Enter the starting point\n");
scanf("%f %f", &x1, &y1);
printf("Enter the ending point\n");
scanf("%f %f", &x2, &y2);
printf("Enter xwmin, ywmin, xwmax, ywmax\n");
scanf("%f %f %f %f",&xwmin,&ywmin,&xwmax,&ywmax);
lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );
getch();
closegraph();
return 0;
}

EXPERIMENT NO:6
Q: Write a program to implement 2-D translation.
Code:
#include<stdio.h>
#include<graphics.h>
#define ROUND(a) ((int)(a+0.5))
void ddaline(int x1, int y1, int x2, int y2)
{
float xsteps, ysteps, x=x1, y=y1;
int dx = x2-x1;
int dy = y2-y1;
int steps,k=1;
if(abs(dx)>=abs(dy))
steps=abs(dx);
else steps=abs(dy);
xsteps= dx/(float)steps;
ysteps= dy/(float)steps;
putpixel(ROUND(x),ROUND(y),15);
while(k<=steps)
{
x+=xsteps;
y+=ysteps;
putpixel(ROUND(x), ROUND(y),15);
k++;
}
}
void translate(int x1, int y1, int x2, int y2, int tx, int ty)
{
ddaline(x1,y1,x2,y2);
ddaline(x1+tx,y1+ty,x2+tx,y2+ty);
}
int main()

{
int x1, x2, y1, y2, tx, ty;
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "..\\BGI");
printf("Enter start point\n");
scanf("%d %d", &x1, &y1);
printf("Enter end point\n");
scanf("%d %d", &x2, &y2);
printf("Enter value for \'tx\' and \'ty\' \n");
scanf("%d %d", &tx, &ty);
translate(x1, y1, x2, y2, tx, ty);
getch();
closegraph();
return 0;
}

EXPERIMENT NO:7
Q: Write a program to implement 2-D rotation.
Code:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
#define ROUND(a) ((int)(a+0.5))
void ddaline(int x1, int y1, int x2, int y2)
{
float xsteps, ysteps, x=x1, y=y1;
int dx = x2-x1;
int dy = y2-y1;
int steps,k=1;
if(abs(dx)>=abs(dy))
steps=abs(dx);
else steps=abs(dy);
xsteps= dx/(float)steps;
ysteps= dy/(float)steps;
putpixel(ROUND(x),ROUND(y),15);
while(k<=steps)
{
x+=xsteps;
y+=ysteps;
putpixel(ROUND(x), ROUND(y),15);
k++;
}
}
void rotate(int x1, int y1, int x2, int y2, float theta)
{
int xtmp, ytmp;
xtmp = x1 + ROUND ((x2-x1)*cos(theta) - (y2-y1)*sin(theta));
ytmp = y1 + ROUND ((x2-x1)*sin(theta) + (y2-y1)*cos(theta));

ddaline(x1,y1,x2,y2);
ddaline(x1,y1,xtmp,ytmp);
}
int main()
{
int x1, x2, y1, y2;
float theta;
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "..\\BGI");
printf("Enter start point\n");
scanf("%d %d", &x1, &y1);
printf("Enter end point\n");
scanf("%d %d", &x2, &y2);
printf("Enter value of angle to rotate line about starting point\n");
scanf("%f", &theta);
rotate(x1, y1, x2, y2, theta);
getch();
closegraph();
return 0;
}

EXPERIMENT NO:8
Q: Write a program to implement 2-D reflection.
Code:
#include<stdio.h>
#include<graphics.h>
#define ROUND(a) ((int)(a+0.5))
void ddaline(int x1, int y1, int x2, int y2)
{
float xsteps, ysteps, x=x1, y=y1;
int dx = x2-x1;
int dy = y2-y1;
int steps,k=1;
if(abs(dx)>=abs(dy))
steps=abs(dx);
else steps=abs(dy);
xsteps= dx/(float)steps;
ysteps= dy/(float)steps;
putpixel(ROUND(x),ROUND(y),15);
while(k<=steps)
{
x+=xsteps;
y+=ysteps;
putpixel(ROUND(x), ROUND(y),15);
k++;
}
}

void reflect(int x1, int y1, int x2, int y2, int Y)
{
ddaline(x1, y1, x2, y2);
ddaline(0, Y, 400, Y);
ddaline(x1, 2*Y-y1, x2, 2*Y-y2);
}
int main()

{
int x1, x2, y1, y2, Y;
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "..\\BGI");
printf("Enter start point\n");
scanf("%d %d", &x1, &y1);
printf("Enter end point\n");
scanf("%d %d", &x2, &y2);
printf("Enter value for \'y\' about which line is to be reflected\n");
scanf("%d", &Y);
reflect(x1, y1, x2, y2, Y);
getch();
closegraph();
return 0;
}

EXPERIMENT NO:9
Q: Write a program to implement 2-D scaling.
Code:
#include<stdio.h>
#include<graphics.h>
#define ROUND(a) ((int)(a+0.5))
void ddaline(int x1, int y1, int x2, int y2)
{
float xsteps, ysteps, x=x1, y=y1;
int dx = x2-x1;
int dy = y2-y1;
int steps,k=1;
if(abs(dx)>=abs(dy))
steps=abs(dx);
else steps=abs(dy);
xsteps= dx/(float)steps;
ysteps= dy/(float)steps;
putpixel(ROUND(x),ROUND(y),15);
while(k<=steps)
{
x+=xsteps;
y+=ysteps;
putpixel(ROUND(x), ROUND(y),15);
k++;
}
}

void scale(int x1, int y1, int x2, int y2, int Sx, int Sy)
{
ddaline(x1,y1,x2,y2);
ddaline(Sx*x1,Sy*y1,Sx*x2,Sy*y2);
}
int main()
{

int x1, x2, y1, y2, Sx, Sy;


int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, "..\\BGI");
printf("Enter start point\n");
scanf("%d %d", &x1, &y1);
printf("Enter end point\n");
scanf("%d %d", &x2, &y2);
printf("Enter value for \'Sx\' and \'Sy\' \n");
scanf("%d %d", &Sx, &Sy);
scale(x1, y1, x2, y2, Sx, Sy);
getch();
closegraph();
return 0;
}

EXPERIMENT NO:10
Q: Write a program to implement boundary fill algorithm.
Code:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void fill_right(int x,int y);
void fill_left(int x,int y);
void main()
{
int gd=DETECT,gm,x,y,n,i;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("*** Boundary Fill algorithm ***");
/*- draw object -*/
line (50,50,200,50);
line (200,50,200,300);
line (200,300,50,300);
line (50,300,50,50);
/*- set seed point -*/
x=100; y=100;
fill_right(x,y);
fill_left(x-1,y);
getch();
}
void fill_right(int x,int y)
{
if((getpixel(x,y) != WHITE)&&(getpixel(x,y) != RED))
{
putpixel(x,y,RED);
fill_right(++x,y); x=x-1;
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_left(--x,y); x=x+1;
fill_left(x,y-1);
fill_left(x,y+1);
}
delay(1);
}

EXPERIMENT NO:11
Q: Write a program to implement Flood fill algorithm.
Code:
#include<conio.h>
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
void fill_right(int x,int y);
void fill_left(int x,int y);
void main()
{
int gd=DETECT,gm,n,i,x,y,a[10][10];
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("*** Flood Fill Algorithm ***\n");
printf("Enter the no. of edges of polygon\t");
scanf("%d",&n);
printf("Enter the cordinates of polygon\n");
for(i=0;i<n;i++)
{
printf("X%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];
printf("Enter the seed Point (X,Y)\t");
scanf("%d%d",&x,&y);
setcolor(WHITE);
for(i=0;i<n;i++) /*- draw poly -*/
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
fill_right(x,y);
fill_left(x-1,y);
getch();
}

void fill_right(int x,int y)


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

EXPERIMENT NO:12
Q: Write a program to determine the 7 points of the bezier curve where
(100,100), (200,300),(400,300) & (300,100) are the vertices of the Bezier
polygon.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#include <math.h>
void bezier (int x[4], int y[4])
{
int gd = DETECT, gm;
int i;
double t;
initgraph (&gd, &gm, "..\\bgi");
for (t = 0.0; t < 1.0; t += 0.14) //To find out the curve Coordinates
{
double xt = pow (1-t, 3) * x[0] + 3 * t * pow (1-t, 2) * x[1] + 3 * pow (t, 2) * (1-t) * x[2]
+ pow (t, 3) * x[3];
double yt = pow (1-t, 3) * y[0] + 3 * t * pow (1-t, 2) * y[1] + 3 * pow (t, 2) * (1-t) * y[2]
+ pow (t, 3) * y[3];
putpixel (xt, yt, WHITE);
}
for (i=0; i<4; i++) //Plot the control Points
putpixel (x[i], y[i], YELLOW);
getch();
closegraph();
return;
}
void main()
{
int x[4], y[4];
int i;
printf ("Enter the x- and y-coordinates of the four control points.\n");
for (i=0; i<4; i++)
scanf ("%d%d", &x[i], &y[i]);
bezier (x, y);
}

You might also like