Computer Graphics Lab Programs
1)bouncing ball
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm=DETECT;
int x,y=0,j,t=400,c=1;
initgraph(&gd,&gm," ");
setcolor(RED);
setfillstyle(SOLID_FILL,RED);
for(x=40;x<602;x++)
{
cleardevice();
circle(x,y,30);
floodfill(x,y,RED);
delay(40);
if(y>=400)
{
c=0;
t-=20;
}
if(y<=(400-t))
c=1;
y=y+(c?15:-15);
}
getch();
}
2)biezer curve
#include<graphics.h>
#include<dos.h>
#include<stdio.h>
int x1,y1,x2,y2,x3,y3,x4,y4;
void bezier_curve(int,int,int,int,int,int,int,int);
void main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm," ");
x1=50,y1=200,x2=200,y2=50,x3=400,y3=400,x4=450,y4=150;
bezier_curve(x1,y1,x2,y2,x3,y3,x4,y4);
getch();
}
void bezier_curve(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4)
{
float t;
putpixel(x2,y2,15);
putpixel(x3,y3,15);
for(t = 0; t < 1; t += 0.00005)
{
float xt = (1-t)*(1-t)*(1-t)*x1 + 3*t*(1-t)*(1-t)*x2+ 3*t*t*(1-t)*x3 + t*t*t*x4;
float yt = (1-t)*(1-t)*(1-t)*y1 + 3*t*(1-t)*(1-t)*y2+ 3*t*t*(1-t)*y3 + t*t*t*y4;
putpixel(xt,yt,3);
line(x1,y1,x2,y2);
line(x2,y2,x4,y4);
line(x4,y4,x3,y3);
line(x3,y3,x1,y1);
}
getch();
}
3) 2d transformation
#include<stdio.h>
#include<graphics.h>
#include<math.h>
int x1,x2,y1,y2;
void input()
{
printf("enter the line coordinates");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
line(x1,y1,x2,y2);
}
void translate()
{
int tx,ty;
input();
printf("enter the translation distances");
scanf("%d%d",&tx,&ty);
line(x1+tx,y1+ty,x2+tx,y2+ty);
}
void scaling()
{
float sx,sy;
input();
printf("enter the scaling factors(only positve)");
scanf("%f%f",&sx,&sy);
line(x1,y1,ceil(x1*sx),ceil(y1*sy));
}
void main()
{
int gd=0,gm=0,ch;
initgraph(&gd,&gm," ");
cleardevice();
printf("enter the choice 1: translate 2:scaling:");
scanf("%d",&ch);
switch(ch)
{
case 1:
translate();
break;
case 2:
scaling();
break;
}
}
4) flood fill
#include<graphics.h>
#include<dos.h>
#include<stdio.h>
void flood_fill(int seedx,int seedy,int fill_color,int old_color);
void main(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
int x,y,r,oldcolor=BLACK;
setcolor(YELLOW);
printf("\n Enter the center and radius of a circle to fill");
scanf("%d %d %d",&x,&y,&r);
cleardevice();
circle(x,y,r);
flood_fill(x,y,LIGHTGREEN,oldcolor);
getch();
}
void flood_fill(int seedx,int seedy,int fill_color,int old_color)
{
if(getpixel(seedx,seedy)==old_color)
{
delay(20);
putpixel(seedx,seedy,fill_color);
flood_fill((seedx+1),seedy,fill_color,old_color);
flood_fill((seedx-1),seedy,fill_color,old_color);
flood_fill(seedx,(seedy+1),fill_color,old_color);
flood_fill(seedx,(seedy-1),fill_color,old_color);
}
}
5) mid point
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void plotpoints(int x,int y,int cx,int cy)
{
putpixel(cx+x,cy+y,4);
putpixel(cx-x,cy+y,4);
putpixel(cx+x,cy-y,4);
putpixel(cx-x,cy-y,4);
putpixel(cx+y,cy+x,4);
putpixel(cx-y,cy+x,4);
putpixel(cx+y,cy-x,4);
putpixel(cx-y,cy-x,4);
}
void main()
{
int cx,cy,x=0,y,r,p;
int gd=DETECT,gm=DETECT;
clrscr();
printf("Enter the center n");
scanf("%d%d",&cx,&cy);
printf("Enter the radius : ");
scanf("%d",&r);
y=r;
p=1-r;
initgraph(&gd,&gm," ");
cleardevice();
while(x<y)
{
plotpoints(x,y,cx,cy);
x++;
if(p<0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;
}
}
getch();
}
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm=DETECT;
int x,y=0,j,t=400,c=1;
initgraph(&gd,&gm," ");
setcolor(RED);
setfillstyle(SOLID_FILL,RED);
for(x=40;x<602;x++)
{
cleardevice();
circle(x,y,30);
floodfill(x,y,RED);
delay(40);
if(y>=400)
{
c=0;
t-=20;
}
if(y<=(400-t))
c=1;
y=y+(c?15:-15);
}
getch();
}
2)biezer curve
#include<graphics.h>
#include<dos.h>
#include<stdio.h>
int x1,y1,x2,y2,x3,y3,x4,y4;
void bezier_curve(int,int,int,int,int,int,int,int);
void main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm," ");
x1=50,y1=200,x2=200,y2=50,x3=400,y3=400,x4=450,y4=150;
bezier_curve(x1,y1,x2,y2,x3,y3,x4,y4);
getch();
}
void bezier_curve(int x1,int y1,int x2,int y2,int x3,int y3,int x4,int y4)
{
float t;
putpixel(x2,y2,15);
putpixel(x3,y3,15);
for(t = 0; t < 1; t += 0.00005)
{
float xt = (1-t)*(1-t)*(1-t)*x1 + 3*t*(1-t)*(1-t)*x2+ 3*t*t*(1-t)*x3 + t*t*t*x4;
float yt = (1-t)*(1-t)*(1-t)*y1 + 3*t*(1-t)*(1-t)*y2+ 3*t*t*(1-t)*y3 + t*t*t*y4;
putpixel(xt,yt,3);
line(x1,y1,x2,y2);
line(x2,y2,x4,y4);
line(x4,y4,x3,y3);
line(x3,y3,x1,y1);
}
getch();
}
3) 2d transformation
#include<stdio.h>
#include<graphics.h>
#include<math.h>
int x1,x2,y1,y2;
void input()
{
printf("enter the line coordinates");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
line(x1,y1,x2,y2);
}
void translate()
{
int tx,ty;
input();
printf("enter the translation distances");
scanf("%d%d",&tx,&ty);
line(x1+tx,y1+ty,x2+tx,y2+ty);
}
void scaling()
{
float sx,sy;
input();
printf("enter the scaling factors(only positve)");
scanf("%f%f",&sx,&sy);
line(x1,y1,ceil(x1*sx),ceil(y1*sy));
}
void main()
{
int gd=0,gm=0,ch;
initgraph(&gd,&gm," ");
cleardevice();
printf("enter the choice 1: translate 2:scaling:");
scanf("%d",&ch);
switch(ch)
{
case 1:
translate();
break;
case 2:
scaling();
break;
}
}
4) flood fill
#include<graphics.h>
#include<dos.h>
#include<stdio.h>
void flood_fill(int seedx,int seedy,int fill_color,int old_color);
void main(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
int x,y,r,oldcolor=BLACK;
setcolor(YELLOW);
printf("\n Enter the center and radius of a circle to fill");
scanf("%d %d %d",&x,&y,&r);
cleardevice();
circle(x,y,r);
flood_fill(x,y,LIGHTGREEN,oldcolor);
getch();
}
void flood_fill(int seedx,int seedy,int fill_color,int old_color)
{
if(getpixel(seedx,seedy)==old_color)
{
delay(20);
putpixel(seedx,seedy,fill_color);
flood_fill((seedx+1),seedy,fill_color,old_color);
flood_fill((seedx-1),seedy,fill_color,old_color);
flood_fill(seedx,(seedy+1),fill_color,old_color);
flood_fill(seedx,(seedy-1),fill_color,old_color);
}
}
5) mid point
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void plotpoints(int x,int y,int cx,int cy)
{
putpixel(cx+x,cy+y,4);
putpixel(cx-x,cy+y,4);
putpixel(cx+x,cy-y,4);
putpixel(cx-x,cy-y,4);
putpixel(cx+y,cy+x,4);
putpixel(cx-y,cy+x,4);
putpixel(cx+y,cy-x,4);
putpixel(cx-y,cy-x,4);
}
void main()
{
int cx,cy,x=0,y,r,p;
int gd=DETECT,gm=DETECT;
clrscr();
printf("Enter the center n");
scanf("%d%d",&cx,&cy);
printf("Enter the radius : ");
scanf("%d",&r);
y=r;
p=1-r;
initgraph(&gd,&gm," ");
cleardevice();
while(x<y)
{
plotpoints(x,y,cx,cy);
x++;
if(p<0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;
}
}
getch();
}
0 comments:
Post a Comment
Note: only a member of this blog may post a comment.