Sunteți pe pagina 1din 11

Ques 6.

Program to generate a line using Bresenhams line drawing


algorithm.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void bhm_line(int x1,int y1,int x2,int y2,int c)
{
int x,y,dx,dy,dx1,dy1,px,py,xe,ye,i;
dx=x2-x1;
dy=y2-y1;
dx1=fabs(dx);
dy1=fabs(dy);
px=2*dy1-dx1;
py=2*dx1-dy1;
if(dy1<=dx1)
{
if(dx>=0)
{
x=x1;
y=y1;
xe=x2; }
else
{
x=x2;
y=y2;
xe=x1; }
putpixel(x,y,c);
for(i=0;x<xe;i++)
{
x=x+1;
if(px<0)
{
px=px+2*dy1; }
else
{
if((dx<0 && dy<0) || (dx>0 && dy>0))
{
y=y+1; }
else
{
y=y-1; }
px=px+2*(dy1-dx1); }
putpixel(x,y,c); } }
else
{
if(dy>=0)
{
x=x1;
y=y1;
111318
Sumit Gupta

ye=y2; }
else
{
x=x2;
y=y2;
ye=y1; }
putpixel(x,y,c);
for(i=0;y<ye;i++)
{
y=y+1;
if(py<=0)
{
py=py+2*dx1; }
else
{
if((dx<0 && dy<0) || (dx>0 && dy>0))
{ x=x+1;}
else
{
x=x-1;}
py=py+2*(dx1-dy1);
putpixel(x,y,c);
} }}
void main()
{
int gd = DETECT, gm;
int dx, dy, p, end;
float x1, x2, y1, y2;
initgraph(&gd, &gm, "c:\tc\bgi");
cout<<"Enter the coordinates (x1,y1): ";
cin>>x1>>y1;
cout<<"Enter the coordinates (x2,y2): ";
cin>>x2>>y2;
cleardevice();
bhm_line(x1,y1,x2,y2,2);
getch();
closegraph();}

111318
Sumit Gupta

Ques 7. Program to demonstrate circle using DDA algorithm.


#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<math.h>
void circl(int xc,int yc,int rad)
{
int x=0,y=rad,p=1-rad;
void plot(int,int,int,int);
plot(xc,yc,x,y);
while(x<y)
{
x++;
if(p< 0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;
}
plot(xc,yc,x,y);
}
}
void plot(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,14);
putpixel(xc-x,yc+y,14);
putpixel(xc+x,yc-y,14);
putpixel(xc-x,yc-y,14);
putpixel(xc+y,yc+x,14);
putpixel(xc-y,yc+x,14);
putpixel(xc+y,yc-x,14);
putpixel(xc-y,yc-x,14);
}
void main()
{
int xc,yc,r;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"T:\\BGI");
printf("Enter the center coordinates and
radius\n");
scanf("%d%d%d",&xc,&yc,&r);
setbkcolor(BLACK);
setcolor(WHITE);
circl(xc,yc,r);
getch();
111318
Sumit Gupta

closegraph();}

Ques 8. Program to implement Midpoint Circle Drawing Algorithm.


#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
int gd=DETECT,gm;
int i,r,x,y,xc,yc;
float d;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\");
printf("Enter Radius\n");
scanf("%d",&r);
printf("Enter Center of circle\n");
scanf("%d",&xc);
scanf("%d",&yc);
d=1.25-r;
x=0;y=r;
do
{
if(d<0)
{
x=x+1;
d=d+2*x+1;
}
else
{
x=x+1;
y=y-1;
d=d+2*x-2*y+10;
}
putpixel(xc+x,yc+y,5);
putpixel(xc-y,yc-x,5);
putpixel(xc+y,yc-x,5);
putpixel(xc-y,yc+x,5);
putpixel(xc+y,yc+x,5);
putpixel(xc-x,yc-y,5);
putpixel(xc+x,yc-y,5);
putpixel(xc-x,yc+y,5);
}
while(x<y);
getch();
}

111318
Sumit Gupta

Ques 9. Program to implement Boundary fill Algorithm for color fill.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int bfillr(int a,int b,int c,int d)
{
int cur;
cur=getpixel(a,b);
if((cur!=d) && (cur!=c))
{
putpixel(a,b,c);
delay(7);
bfillr(--a,b,c,d);
a=a+1;
bfillr(a,b-1,c,d);
bfillr(a,b+1,c,d);
}
return;
}
int bfilll(int a,int b,int c,int d)
{
int cur;
cur=getpixel(a,b);
if((cur!=d) && (cur!=c))
{
putpixel(a,b,c);
delay(7);
bfilll(++a,b,c,d);
a=a-1;
bfilll(a,b-1,c,d);
bfilll(a,b+1,c,d);
}
return;
}
int main()
{
int x,y,fco,bco,t;
int gd=DETECT, gm,i;
initgraph(&gd,&gm,"");
printf("Enter the points");
scanf("%d%d",&x,&y);
printf("Enter fill color and boundry color");
scanf("%d%d",&fco,&bco);
cleardevice();
111318
Sumit Gupta

circle(50,50,30);
t=bfilll(x,y,fco,bco);
t=bfillr(x-1,y,fco,bco);
getch();
return;
}

Ques 10. Program to demonstrate different transformations like


Translation, Scaling and Rotation.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int main()
{
int gd=DETECT, gm,i,ch;
initgraph(&gd,&gm,"..\\BGI");
cleardevice();
outtext("orignal rectangle");
rectangle(100,150,400,350);
printf("\nEnter \n1 for scaling\n2 for rotation\n3 for translation");
scanf("%d",&ch);
cleardevice();
if(ch==1){
outtext("after scaling by 100 in the length");
rectangle(100,100,400,400);}
else if(ch==2){
outtext("after rotation");
rectangle(150,100,350,400);}
111318
Sumit Gupta

else if(ch==3){
outtext("after translation by 100 towards bottom");
rectangle(100,250,400,450);}
getch();
}

Ques 11. Program to demonstrate view port, clipping and lines with
different Colors, line styles and co- ordinates
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<stdio.h>
void main()
{
int gm,gd=DETECT;
int x1,x2,y1,y2,c,i,k=0;
clrscr();
printf("enter starting coordinates of viewport\n");
scanf("%d%d",&x1,&y1);
printf("enter ending coordinates of viewport\n");
scanf("%d%d",&x2,&y2);
initgraph(&gd,&gm,"..\\BGI");
rectangle(x1,y1,x2,y2);
setviewport(x1,y1,x2,y2,1);
while(k<=10)
{
k++;
111318
Sumit Gupta

x1=rand()%getmaxx();
x2=rand()%getmaxx();
y1=rand()%getmaxy();
y2=rand()%getmaxy();
setcolor(k+1);
line(x1,y1,x2,y2);
delay(200);
}
getch();
closegraph();
}

Ques 12. Program to demonstrate text and its setting.


#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<stdio.h>
#include<dos.h>
void main()
{
int gm,gd=DETECT;
initgraph(&gd,&gm,"..\\BGI");
setcolor(7);
settextstyle(1,0,5);
moveto(100,100);
outtext("SUMIT GUPTA");
setcolor(5);
settextstyle(2,0,10);
moveto(200,150);
outtext("World Cup");
setcolor(9);
settextstyle(3,0,9);
moveto(250,300);
outtext("JUET");
111318
Sumit Gupta

setcolor(2);
settextstyle(4,1,7);
outtextxy(100,150,"Computer Graphics");
getch();
}

Ques 13. Program to draw a moving vehicle (car). The different graphical
functions and transformations are used to draw different parts and
movements of the car.
#include<dos.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#define PI 3.14159
void draw_wheel(int x,int y,int theta)
{
int incr=45;
double i;
setcolor(getmaxcolor());
setfillstyle(EMPTY_FILL,getmaxcolor());
for( i=theta;i<theta+360.0;i+=2*incr)
{
sector(x,y,i,i+incr,20,20);
arc(x,y,i+incr,i+2*incr,20);
}
}
void draw_car(int ang)
{
int car_color=RED;
draw_wheel(50,200,ang);
draw_wheel(200,200,ang);
setcolor(car_color);
line(0,80,639,80);
line(0,300,639,300);
line(25,200,0,200);
line(0,200,0,160);
line(0,160,40,160);
line(40,160,70,130);
line(70,130,170,130);
line(170,130,200,160);
line(200,160,260,160);
line(260,160,260,200);
line(260,200,225,200);
line(175,200,75,200);
111318
Sumit Gupta

arc(50,200,0,180,25);
arc(200,200,0,180,25);
setfillstyle(SOLID_FILL,car_color);
floodfill(150,170,car_color);
}
void main(void)
{
int gd,gm,i,j;
void *bitmap1,*bitmap2;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc ");
draw_car(0);
bitmap1=malloc(imagesize(0,130,270,230));
getimage(0,130,270,230,bitmap1);
putimage(0,130,bitmap1,XOR_PUT);
draw_car(22);
bitmap2=malloc(imagesize(0,130,270,230));
getimage(0,130,270,230,bitmap2);
putimage(0,130,bitmap2,XOR_PUT);
for(i=0;!kbhit();i+=10)
{
if(i>500) i=0;
putimage(i,130,bitmap1,OR_PUT);
delay(100);
putimage(i,130,bitmap1,XOR_PUT);
putimage(i+5,130,bitmap2,OR_PUT);
delay(50);
putimage(i+5,130,bitmap2,XOR_PUT);
}
closegraph();}

Ques 14. Program to demonstrate spiral.


#include<graphics.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int r=5,n,i,x,y;
int graphdriver = DETECT, graphmode;
initgraph(&graphdriver, &graphmode, "..\\BGI");
printf("ENTER THE NUMBER OF RINGS IN THE SPIRAL MODEL\n");
scanf("%d",&n);
printf("Enter the origin point of the spiral");
111318
Sumit Gupta

scanf("%d%d",&x,&y);
cleardevice();
for(i=0;i<n;i++)
{
delay(50);
setcolor(14);
arc(x,y,0,180,r=r+2);
arc(x+2,y,180,360,r=r+2);
}
getch();
closegraph();
}
Input
25 rings
Origin center

111318
Sumit Gupta

S-ar putea să vă placă și