Graphics Lab BCA35L
Graphics Lab BCA35L
#include<graphics.h>
#include<conio.h>
void main()
{
int gd =DETECT: /*Detect the graph driver dynamically*/
int gm; /*for graph mode*/
2)
/* program to draw a rectangle , lines as its diagonals and a circle */
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT;
int gm;
initgraph(&gd,&gm,””);
rectangle(10,10,200,200); /* draws a rectangle */
line(10,10,200,200); /* draw a line on the main diagonal*/
line(10,200,200, 10); /* draws a line on off diagonal */
circle(105,105,95); /* draws a circle taking (105,105)
as center co-ordinates and 95 as radius
all the dimensions are in pixels */
getch();
closegraph(); /*closes the graph mode */
}
3) /* program to draw a circle and ellipses */
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT;
int gm;
Initgraph (&gd,&gm,””);
Circle (getmaxx ()/2,getmaxy ()/2,100); /*draws a circle taking
midpoint of the screen as center co-ordinates and
100 as radius */
ellipse(getmaxx()/2,0,360,80,50);
/* draws an ellipse taking center of the screen as its center , 0 as
starting angle and 360 as ending angle and 80 pixel as Y radius */
setcolor(4); /*sets the drawing color as red */
ellipse(getmaxx()/2, getmaxy()/2,90,270,50, 80);
/*draws half the ellipse starting from 90 angle and ending at 27o
angle with 50 pixels as X-radius and 80 pixels as Y-radius in red color */
getch();
closegraph(); /* closes the graph mode */
}
void main()
{
int gm, gd=DETECT,I;
initgraph(&gd, &gm,’’’’);
while(!kbhit())/* until pressing any key this loop continues */
{
putpixel(rand()%getmaxx(), rand() % getmaxy(), rand()%16);
/*x and y co-ordinates and the color are taken randomly*/
delay(2); /* just to draw the pixels slowly*/
}
getch ();
closegraph(); /* closes the graph mode */
}
5)
/* Program to demonstrate rectangles using putpixel and filling them
with different fill effects */
# Include <graphics.h>
# Include <conio.h>
# include <stdlib.h>
# include <dos.h>
void main()
{
int gm,gd= DETECT;
int x1,x2,y1,y2,c,I;
initgraph(&gd,&gm,’’’’);
while(!kbhit()) /* until pressing any key this loop continues */
{
/* To draw rectangle co-ordinatinates are taken randomly */
x1=rand()%getmaxx();
x2=rand()%getmaxx();
y1=rand()%getmaxy();
y2=rand()%getmaxy();
if (x1>x2)
{
c=x1; /* exchange of x1 and x2 when x1 is >x2 */
x1=x2;
x2=c;
}
if(y1>y2)
{
c=y1; /* exchange of y1 and y2 when y1 is > y2 */
y1=y2;
y2=c;
}
c=rand()%16;
/* for rectangle using putpixel */
for(I=x1 ;i<=x2;++i)
{
putpixel(I,y1,c);
delay (1);
}
for(i=y1;I<=y2;++i)
{
putpixel(x2,I,c);
delay(1);
}
for(i=x2;i>=x1; i)
{
putpixel(i,y2,c);
delay(1);
}
for(i=y2;I>=y1; i)
{
putpixel(x1,i,c);
delay(1);
}
setfillsytyle(rand()%12, rand()%8); /* setting the random fill styles and
colors *
floodfill(x1+1,y1+1,c);
delay(200); /* to draw the pixels slowly */
}
getch();
closegraph(); /* closes the graph mode */
}
initgraph(&gd,&gm,””);
while(kbhit()) /* until pressing any key this loop continues */
{
/* to draw rectangle co-ordinates are taken randomly */
x1=rand()%getmaxx();
x2=rand()%getmaxx();
y1=rand()%getmaxy();
y2=rand()%getmaxy();
#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;
clrscr();
printf(“enter starting co-ordinates of viewport (x1,y1)/n”);
scanf(“%d%d”,&x1,&y1);
printf(“enter ending co-ordinates of viewport(x2,y2)/n”);
scanf(“%d%d”,&x2,&y2);
initgraph(&gd,&gm,””);
x1=rand()%getmaxx();
x2=rand()%getmaxx();
y1=rand()%getmaxy();
y2=rand()%getmaxy();
setlinestle(rand()%10, rand()%20);
setcolor(rand()%16); /*to set the line color */
line(x1,y1,x2,y2); /*to draw the line */
delay(200);
}
getch();
closegraph(); /*closes the graph mode */
}
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<stdio.h>
void main()
{
int gm, gd=DETECT;
initgraph(&gd,&gm,””);
setcolor(5);
settextstyle(4,0,5); /*sets the text style with font, direction and char
size */
moveto(100,100); /*takes the CP to 100,100 */
outtext(“Bangalore is”);
setcolor(4);
settextstyle(3,0,6);
moveto(200,200);
outtext(“silicon”);
setcolor(1)
settextstyle(5,0,6);
moveto(300.300);
outtext(“Valley”);
setcolor(2);
sertextstyle(1,1,5);
outtextxy(150,50,”Bangalore is”);
getch();
#include<stdio.h>
#include<graphics.h>
main()
{
int x,y,i,choice;
unsigned int size;
void*car;
int gd=DETECT,gm;
initgraph(&gd, &gm,” “);
do
{
cleardevice();
printf(“1:BODY OF THE CAR\n”);
printf(“2:WHEELS OF THE CAR\n”);
printf(“3:CAR\n”);
printf(“4:QUIT”);
printf(“\nEnter your choice\n”);
scanf(“%d”,&choice);
switch(choice)
{
case 1 : initgraph (&gd,&gm,” “);
line(135,300,265,300);
arc(100,300,0,180,35);
line(65,300,65,270);
line(65,270,110,220);
line(110,220,220,220);
line(140,220,140,215);
line(180,220,180,215);
line(175,300,175,220);
line(120,215,200,250);
line(220,220,260,250);
line(260,250,85,250);
line(260,250,345,275);
arc(300,300,0,180,35);
line(345,300,345,275);
line(335,300,345,300);
getch();
cleardevice();
break;
case 2: initgraph(&gd,&gm,””);
circle(100,300,25);
circle(100,300,13);
circle(300,300,25);
circle(300,300,13);
getch();
cleardevice();
break;
line(140,220,140,215);
line(180,220,180,215);
line(175,300,175,220);
line(120,215,200,215);
line(220,220,260,250);
line(260,250,85,250);
line(260,250,345,275);
arc(300,300,0,180,35);
circle(300,300,25);
circle(300,300,13);
line(345,300,345,275);
line(335,300,345,300);
getch();
cleardevice();
break;
case 4 : exit(0);
}
}
while(choice!=4);
getch();
}
# include<stdio.h>
# include<conio.h>
#include<stddlib.h>
# include <graphics.h>
void brline(int,int,int,int);
void main()
{
int gd=DETECT,gm,color:
int x1,y1,x2,y2;
printf(“\n enter the starting point x1,y1 :”);
scanf(“%d%d”,&x1,&y1);
printf(“\n enter the starting point x2,y2 :”);
scanf(“%d%d”,&x2,&y2);
clrscr();
initgraph(&gdriver, &gmode,””);
brline(x1,y1,x2,y2);
getch();
closegraph();
}
while(x<xend)
{
color=random(getmaxcolor());
putpixel((int)x,(int)y,color);
if(e>0)
{
y++;
e=e+2*(dy-dx);
}
else
e=e+2*dy;
x++;
}
}
void main()
{
int gm,gd=DETECT,I,;
int x,y,x1,y1,j;
initgraph(&gd,&gm,””);
x=40; /*The c0-ordinate values for calculating radius */
y=40;
for(i=0;i<=360;i+=10)
{
setcolor(i+1);
x1=x*cos(i*3.142/180)+y*sin(i*3.142/180);
y1=x*sin(i*3.142/180)-y*cos(I*3.142/180);
circle(x1+getmaxx()/2,y1+getmaxy()/2,5); /* center of the circle is
center of the screen*/
delay(10);
}
getch();
}
13) /* program to implement Bresenham’s Circle Drawing Algorithm */
# include<stdio.h>
# include<conio.h>
# include <graphics.h>
# include<math.h>
#include<dos.h>
/* Function for plotting the co-ordinates at four different angles that are
placed at egual distences */
putpixel(xcentre+y,ycevtre+x,color);
putpixel(xcentre+y,ycevtre-x,color);
putpixel(xcentre-x,ycevtre+x,color);
putpixel(xcentre-y,ycevtre-x,color);
}
/* The main function that takes (x,y) and ‘r’ the radius from keyboard
and activates other functions for drawing the circle */
void main()
{
intgd=DETECT,gm,xcentre=200,ycentre=150,redius=5;
printf(“\n enter the center points and radius :\n”);
scanf(“%d%d%d”, &xcentre, &ycentre, &radius);
clrscr();
initgraph(&gd,&gm,””);
putpixel(xcentre,ycentre,5);
cir(xcentre,ycentre,redius);
getch();
closegraph();
}
void main()
{
int gm,gd=DETECT;
float x,y,x1,y1,i;
initgraph(&gd,&gm,””);
x=100;
y=100;
for(i=0;i<=360;i+=0.005)
{
x=x*cos(i*3.142/180)+y*sin(i*3.142/180);
y=x*sin(i*3.142/180)+y*cos(i*3.142/180);
putpixel((int)x+200,(int)y+200,15);
}
getch();
}
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<alloc.h>
#include<math.h>
void main()
{
int gm, gd=DETECT;
initgraph(&gd,&gm,””);
setviewport(100,100,300,300,0);
for(j=0;j<200;j=j+20)
{
for(i=0;i<=200;++i)
{
if (i%20==0)
setcolor(rand()%16+1);
line(i,j,i,j+20);
}
delay(100);
}
getch();
}