0% found this document useful (0 votes)
37 views13 pages

CG Experiments

The document contains code snippets implementing various computer graphics algorithms: 1) DDA line drawing algorithm 2) Bresenham's line drawing algorithm 3) Midpoint circle drawing algorithm 4) Midpoint ellipse drawing algorithm 5) Boundary fill algorithm 6) 2D geometric transformations including translation, rotation, and scaling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views13 pages

CG Experiments

The document contains code snippets implementing various computer graphics algorithms: 1) DDA line drawing algorithm 2) Bresenham's line drawing algorithm 3) Midpoint circle drawing algorithm 4) Midpoint ellipse drawing algorithm 5) Boundary fill algorithm 6) 2D geometric transformations including translation, rotation, and scaling.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

pract 1:implement dda line drawing

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

#include<stdlib.h>

void main()

int x,y,x1,x2,y1,y2,k,dx,dy,s,xi,yi;

int gdriver=DETECT,gmode;

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

prin ("enter first point");

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

prin ("enter second point");

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

x=x1;

y=y1;

putpixel(x,y,7 );

dx=x2-x1; dy=y2-y1;

if(abs(dx)>abs(dy)) s=abs(dx);

else s=abs(dy);

xi=dx/s;

yi=dy/s ;

x=x1;

y=y1;

putpixel(x,y,7);

for(k=0;k<s;k++)

{ x=x+xi;

y=y+yi;

putpixel(x,y,7);

getch();

closegraph();

}
pract 3:implementa on midpoint circle

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void circlepoints(int,int);

void main()

int x,y,p,r;

int gdriver=DETECT,gmode;

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

clrscr();

prin ("enter the radius");

scanf("%d",&r);

x=0;y=r;p=1-r;

while(x<y)

x++;

if(p>0)

else

p=p+2*(x-

y)+1;y--;

p=p+2*x+1;

circlepoints(x,y);

getch();

void circlepoints(int x,int y)

putpixel(x+300,y+300,8);

putpixel(x+300,-y+300,8);

putpixel(-x+300,y+300,8);

putpixel(-x+300,-

y+300,8);

putpixel(y+300,x+300,8);

putpixel(y+300,-x+300,8);
putpixel(-y+300,x+300,8);

putpixel(-y+300,-

x+300,8);

pract 4:implement midpoint ellipse

#include<stdio.h>

#include<graphics.h>

void main()

int gdriver=DETECT,gmode;

float p,x,y,xc,yc,a,b;

initgraph(&gdriver,&gmode,"C:\\turboc3\\bgi");

prin ("Enter xc:\n");

scanf("%f",&xc);

prin ("Enter yc:\n");

scanf("%f",&yc);

prin ("Enter a:\n");

scanf("%f",&a);

prin ("Enter b:\n");

scanf("%f",&b);

x=0;

y=b;

//Region 1

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

do

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

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

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

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

if(p<0)

x=x+1;

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;

}while(2*b*b*x<2*a*a*y);

//Region 2

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

do

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

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

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

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

if(p>0)

y=y-1;

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

else

x=x+1;

y=y-1;

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

}while(y!=0);

closegraph();

}
1)DDA

#include<stdio.h>

#include<conio.h>

#include<graphics.h

>void main()

int x,y,x1,x2,y1,y2,k,dx,dy,s,xi,yi;

int gdriver=DETECT,gmode;

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

prin ("enter first point");

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

prin ("enter second point");

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

x=x1;

y=y1;

putpixel(x,y,7

);dx=x2-x1;

dy=y2-y1;

if(abs(dx)>abs(dy))

s=abs(dx);

else

s=abs(dy);

xi=dx/s;

yi=dy/s

;x=x1;

y=y1;

putpixel(x,y,7);

for(k=0;k<s;k++)

x=x+xi;

y=y+yi;

putpixel(x,y,7)

getch();

closegraph();

}
2)Bresenham's

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void main()

int x,y,x1,y1,x2,y2,p,dx,dy;

int gdriver=DETECT,gmode;

initgraph(&gdriver,&gmode,"C:\\turboc3\\BGI");

prin ("\nEnter the x-coordinate of the first point ::");

scanf("%d",&x1);

prin ("\nEnter the y-coordinate of the first point ::");

scanf("%d",&y1);

prin ("\nEnter the x-coordinate of the second point ::");

scanf("%d",&x2);

prin ("\nEnter the y-coordinate of the second point ::");

scanf("%d",&y2);

x=x1;

y=y1;

dx=x2-x1;

dy=y2-y1;

putpixel(x,y,2);

p=(2*dy-dx);

while(x<=x2)

if(p<0)

x=x+1;

p=2*x-dx;

else

x=x+1;
y=y+1;

p=p+2*dy;

putpixel(x,y,7);

getch();

closegraph();

3)Midpoint Circle

#include<stdio.h>

#include<conio.h>

#include<graphics.h>

void circlepoints(int,int);

void main()

int x,y,p,r;

int gdriver=DETECT,gmode;

initgraph(&gdriver,&gmode,"C:\\turboC3\\BGI");

clrscr();

prin ("Enter the radius: ");

scanf("%d",&r);

x=0;

y=r;

p=1-r;

while(x<y)

x++;

if(p>0){

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

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

else{

p=p+2*x+1;

circlepoints(x,y);

getch();

closegraph();

void circlepoints(int x,int y)

putpixel(x+300,y+300,8);

putpixel(x+300,-y+300,8);

putpixel(-x+300,y+300,8);

putpixel(-x+300,-y+300,8);

putpixel(y+300,x+300,8);

putpixel(y+300,-x+300,8);

putpixel(-y+300,x+300,8);

putpixel(-y+300,-x+300,8);

}
4)Midpoint Eclipse

#include<stdio.h>

#include<graphics.h>

void main()

int gdriver=DETECT,gmode;

float p,x,y,xc,yc,a,b;

initgraph(&gdriver,&gmode,"C:\\turboc3\\bgi");

prin ("Enter xc:\n");

scanf("%f",&xc);

prin ("Enter yc:\n");

scanf("%f",&yc);

prin ("Enter a:\n");

scanf("%f",&a);

prin ("Enter b:\n");

scanf("%f",&b);

x=0;

y=b;

//Region 1

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

do

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

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

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

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

if(p<0)

x=x+1;

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;

}while(2*b*b*x<2*a*a*y);

//Region 2

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

do

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

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

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

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

if(p>0)

y=y-1;

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

else

x=x+1;

y=y-1;

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

}while(y!=0);

getch();

closegraph();

}
5)Boundary Fill

#include<stdio.h>

#include<graphics.h>

#include<dos.h>

void boundaryfill(int x,int y,int f_color,int b_color)

if(getpixel(x,y)!=b_color && getpixel(x,y)!=f_color)

putpixel(x,y,f_color);

boundaryfill(x+1,y,f_color,b_color);

boundaryfill(x,y+1,f_color,b_color);

boundaryfill(x-1,y,f_color,b_color);

boundaryfill(x,y-1,f_color,b_color);

//getpixel(x,y) gives the color of specified pixel

int main()

int gm,gd=DETECT,radius;

int x,y;

prin ("Enter x and y posi ons for circle\n");

scanf("%d%d",&x,&y);

prin ("Enter radius of circle\n");

scanf("%d",&radius);

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

circle(x,y,radius);

boundaryfill(x,y,4,15);

delay(5000);

closegraph();

return 0;

}
6)2D transforma on

#include<graphics.h>

#include<stdlib.h>

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

int gm;

int gd=DETECT;

int x1,x2,x3,y1,y2,y3,nx1,nx2,nx3,ny1,ny2,ny3,c;

int sx,sy,xt,yt,r;

float t;

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

prin ("\t Program for basic transac ons");

prin ("\n\t Enter the points of triangle");

setcolor(1);

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

line(x1,y1,x2,y2);

line(x2,y2,x3,y3);

line(x3,y3,x1,y1);

getch();

prin ("\n 1.Transac on\n 2.Rota on\n 3.Scalling\n 4.exit");

prin "Enter your choice:");

scanf("%d",&c);

switch(c)

case 1:

prin ("\n Enter the transla on factor");

scanf("%d%d",&xt,&yt);

nx1=x1+xt;

ny1=y1+yt;

nx2=x2+xt;

ny2=y2+yt;

nx3=x3+xt;

ny3=y3+yt;

line(nx1,ny1,nx2,ny2);
line(nx2,ny2,nx3,ny3);

line(nx3,ny3,nx1,ny1);

getch();

case 2:

prin ("\n Enter the angle of rota on");

scanf("%d",&r);

t=3.14*r/180;

nx1=abs(x1*cos(t)-y1*sin(t));

ny1=abs(x1*sin(t)+y1*cos(t));

nx2=abs(x2*cos(t)-y2*sin(t));

ny2=abs(x2*sin(t)+y2*cos(t));

nx3=abs(x3*cos(t)-y3*sin(t));

ny3=abs(x3*sin(t)+y3*cos(t));

line(nx1,ny1,nx2,ny2);

line(nx2,ny2,nx3,ny3);

line(nx3,ny3,nx1,ny1);

getch();

case 3:

prin ("\n Enter the scalling factor");

scanf("%d%d",&sx,&sy);

nx1=x1*sx;

ny1=y2*sy;

nx2=x2*sx;

ny2=y2*sy;

nx3=x3*sx;

ny3=y3*sy;

line(nx1,ny1,nx2,ny2);

line(nx2,ny2,nx3,ny3);

line(nx3,ny3,nx1,ny1);

getch();

case 4:

break;

default:

prin ("Enter the correct choice");

closegraph();

You might also like