WAP TO DRAW BASIC SHAPES.
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT, gm;
int poly[12]={350,450, 350,410, 430,400, 350,350, 400,310, 350,450};
initgraph(&gd,&gm,"d:\tc\bgi");
setbkcolor(BLUE);
setcolor(YELLOW);
circle(100,100,50);
outtextxy(75,170, "Circle");
rectangle(200,50,350,150);
outtextxy(240, 170, "Rectangle");
ellipse(500,100,0,360,100,40);
outtextxy(480, 170, "Ellipse");
line(100,250,540,250);
outtextxy(300,260,"Line");
sector(150, 400, 30, 200, 90,50);
outtextxy(120, 460, "Sector");
drawpoly(6, poly);
fillpoly(6,poly);
outtextxy(340, 460, "Polygon");
arc(520,410,0,150,60);
outtextxy(500,420,"Arc");
getch();
closegraph();
}
OUTPUT:-
WAP TO SET THE BACKGRAOUND COLOR.
#include <graphics.h>
#include<conio.h>
main()
{
int gd = DETECT, gm,i;
initgraph(&gd, &gm, "D:\TC\BGI");
for (i=0;i<12;i++)
{
setbkcolor(i);
delay(1000);
}
getch();
closegraph();
return 0;
}
Output:-
WAP TO DRAW BAR CHART.
#include<graphics.h>
#include<conio.h>
void main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "D:\TC\BGI");
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
setcolor(WHITE);
outtextxy(275,10,"BAR CHART");
setlinestyle(SOLID_LINE,0,2);
line(100,420,100,60);
line(100,420,600,420);
line(90,70,100,60);
line(110,70,100,60);
line(590,410,600,420);
line(590,430,600,420);
outtextxy(95,35,"Y");
outtextxy(610,405,"X");
outtextxy(85,415,"O");
setfillstyle(LINE_FILL,BLUE);
bar(150,100,200,419);
setfillstyle(XHATCH_FILL,RED);
bar(225,150,275,419);
setfillstyle(WIDE_DOT_FILL,GREEN);
bar(300,200,350,419);
setfillstyle(INTERLEAVE_FILL,MAGENTA);
bar(375,125,425,419);
setfillstyle(HATCH_FILL,BROWN);
bar(450,175,500,419);
getch();
}
OUTPUT:-
WAP TO DRAW PIE CHART.
#include<graphics.h>
#include<conio.h>
void main()
{
int gd = DETECT, gm, midx, midy;
initgraph(&gd, &gm, "D:\TC\BGI");
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
setcolor(WHITE);
outtextxy(275,80,"PIE CHART");
midx = getmaxx()/2;
midy = getmaxy()/2;
setfillstyle(LINE_FILL,BLUE);
pieslice(midx, midy, 0, 75, 100);
setfillstyle(XHATCH_FILL,RED);
pieslice(midx, midy, 75, 225, 100);
setfillstyle(WIDE_DOT_FILL,GREEN);
pieslice(midx, midy, 225, 360, 100);
getch();
}
OUTPUT:-
WAP TO CREATE CONCENTRIC CIRCLE.
#include <graphics.h>
#include<conio.h>
void main()
{
int gd = DETECT, gm;
int x = 320, y = 240, radius;
initgraph(&gd, &gm, "D:\TC\BGI");
outtextxy(250,80,"CONCENTRIC CIRCLE");
for ( radius = 25; radius <= 125 ; radius = radius + 20)
circle(x, y, radius);
getch();
closegraph();
}
OUTPUT:-
WAP TO DRAW DIFFERENT LINESTYLE.
#include <graphics.h>
#include <conio.h>
char line_styles []={ SOLID_LINE, DOTTED_LINE, CENTER_LINE, DASHED_LINE, USERBIT_LINE };
void main()
{
int gd = DETECT, gm, c , x = 100, y = 50;
initgraph(&gd, &gm, "D:\TC\BGI");
for ( c = 0 ; c < 5 ; c++ )
{
setlinestyle(c, 0, 2);
line(x, y, x+200, y);
y = y + 25;
}
getch();
closegraph();
}
OUTPUT:-
WAP TO DRAW DIFFERENT TEXTSTYLE.
#include<graphics.h>
#include<conio.h>
char font_names[]={ DEFAULT_FONT, TRIPLEX_FONT, SMALL_FONT, SANS_SERIF_FONT, GOTHIC_FONT, SCRIPT_FONT, SIMPLEX_FONT, TRIPLEX_SCR_FONT, COMPLEX_FONT, EUROPEAN_FONT, BOLD_FONT };
void main()
{
int gd = DETECT, gm, x = 25, y = 25, font = 0;
initgraph(&gd,&gm, "D:\TC\BGI");
for ( font = 0 ; font <= 10 ; font++)
{
settextstyle(font, HORIZ_DIR, 1);
outtextxy(x, y, "Text with different fonts");
y = y + 25;
}
getch();
closegraph();
}
OUTPUT:-
WAP TO DRAW A HUMAN FACE.
#include <graphics.h>
#include<conio.h>
void main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, "D:\TC\BGI");
circle(300,180,80);
circle(270,150,10);
circle(320,150,10);
line(300,170,300,200);
arc(300,180,240,300,50);
outtextxy(280,280,"HUMAN FACE");
getch();
closegraph();
}
OUTPUT:-
WAP TO GENERATE CIRCLES ON THE SCREEN RANDOMLY.
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
void main()
{
int gd = DETECT,gm;
int x,y;
initgraph(&gd, &gm, "D:\TC\BGI");
x=getmaxx()/2;
y=getmaxy()/2;
while(!kbhit)
{
setcolor(random(getmaxcolor));
circle(random(x),random(y),random(400));
delay(1000);
}
getch();
closegraph();
}
OUTPUT:-
WAP TO FILL A RECTANGLE USING A SETFILLSTYLE.
#include<graphics.h>
#include<conio.h>
#include<dos.h>
char fill_styles[]={EMPTY_FILL,SOLID_FILL,LINE_FILL,
LTSLASH_FILL,SLASH_FILL,BKSLASH_FILL,LTBKSLASH_FILL,HATCH_FILL,
XHATCH_FILL,INTERLEAVE_FILL,WIDE_DOT_FILL,CLOSE_DOT_FILL,
USER_FILL};
void main()
{
int midx,midy,style;
char stylestr[40];
int gd = DETECT, gm;
initgraph(&gd, &gm, "D:\TC\BGI");
midx=getmaxx()/2;
midy=getmaxy()/2;
rectangle(0,0,midx,midy);
for(style=0;style<12;style++)
{
setfillstyle(style,5);
strcpy(stylestr,fill_styles[style]);
rectangle(0,0,midx,midy);
floodfill(10,10,WHITE);
delay(1000);
getch();
cleardevice();
}
closegraph();
}
OUTPUT:-
WAP TO GENERATE CIRCLES WITH RANDOM RADIUS AND RANDOM PIXEL ON THE SCREEN.
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
void main()
{
int gd = DETECT,gm;
int x,y;
initgraph(&gd, &gm, "D:\TC\BGI");
x=getmaxx()/2;
y=getmaxy()/2;
while(!kbhit())
{
putpixel(random(600),random(600),random(600));
circle(random(x),random(y),random(5));
delay(1000);
}
getch();
closegraph();
}
OUTPUT:-
WAP TO PRINT A LINE USING DDA ALGORITHM.
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<dos.h>
void main()
{ int x1,x2,y1,y2,dx,dy,m,x,y;
int gd=DETECT,gm;
initgraph(&gd, &gm, "D:\tc\bgi");
printf("Enter the value of points = ");
scanf("%d%d%d%d",&x1,&x2,&y1,&y2);
dx=x2-x1;
dy=y2-y1;
m=dy/dx;
x=x1;
y= y1;
putpixel(x,y,WHITE);
if(m<1)
while(x<x2)
{
x=x+1;
y=y+1;
putpixel(x,y,WHITE);
}
else
while(y<=y2)
{
y=y+1;
x=x+1/m;
putpixel(x,y,WHITE);
}
getch();
closegraph();
}
OUTPUT:-
WAP TO CREATE CONCENTRIC ELLIPSE.
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
void main()
{
int i,r1,r2,n, j=2,xr=200,yr=100;
int gd=DETECT,gm;
initgraph(&gd,&gm,"D:\tc\bgi");
printf("enter the no. of Ellipse=");
scanf("%d",&n);
r1=xr/n;
r2=yr/n;
for(i=0;i<n;i++)
{
setfillstyle(1,j);
ellipse(300,200,0,360,xr,yr);
if(j==10)
j=2;
xr=xr-r1;
yr=yr-r2;
j++;
}
getch();
}
OUTPUT:-
WAP TO DRAW PIECHART OF MARKS ENTERED BY USER.
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
void main()
{
int gd=DETECT,gm;
float x=0,y=0,tm,m;
int c=1,n=5;
initgraph(&gd,&gm,"D:\tc\bgi");
setbkcolor(WHITE);
setcolor(BLUE);
pieslice(300,250,0,360,100);
printf("Enter total marks=");
scanf("%f",&tm);
while(n>0)
{
printf("Enter marks=");
scanf("%f",&m);
setfillstyle(1,c);
c++;
y=y+((m/tm)*360);
pieslice(300,250,x,y,100);
x=y;
n--;
}
getch();
closegraph();
}
OUTPUT:-
WAP TO PRINT LINE USING BRESENHAM’S ALGORITHM.
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
void main()
{
int gd=DETECT,gm;
float x,y,y1,y2,x1,x2,p,dx,dy;
initgraph(&gd,&gm,"D:\tc\bgi");
printf("Enter the values=");
scanf("%f%f%f%f",&x1,&x2,&y1,&y2);
dx=x2-x1;
dy=y2-y1;
p=2*dy-dx;
x=x1;
y=y1;
putpixel(x,y,WHITE);
while(x<x2)
{
if(p<0)
{
p=p+2*dy;
}
else
{
p=p+2*dy-2*dx;
y++;
}
x++;
putpixel(x,y,15);
}
getch();
closegraph();
}
OUTPUT:-
WAP TO CREATE A CIRCLE THROUGH MID-POINT ALGORITHM.
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
void main()
{
int x,y,h,k,r,p;
int gd=DETECT,gm;
initgraph(&gd,&gm,"D:\tc\bgi");
printf(" Enter the center=");
scanf("%d%d",&h,&k);
printf("Enter the radius=");
scanf("%d",&r);
x=0;
y=r;
p=1-r;
while(x<y)
{
if(p<0)
{
p=p+(2*x)+3;
}
else
{
p=p+2*(x-y)+5;
y=y-1;
}
x++;
putpixel(x+h,y+k,15);
putpixel(y+h,x+k,15);
putpixel(-x+h,-y+k,15);
putpixel(-y+h,-x+k,15);
putpixel(-x+h,y+k,15);
putpixel(x+h,-y+k,15);
putpixel(y+h,-x+k,15);
putpixel(-y+h,x+k,15);
}
getch();
closegraph();
}
OUTPUT:-
WAP TO DRAW AN ELLIPSE BY USING x2/a2+y2/b2=1.
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
float x,y;
int a=100,b=80;
initgraph(&gd,&gm,"D:\tc\bgi");
do
{
x=x+0.5;
y=sqrt(b*b*(1-(x*x)/(a*a)));
putpixel(320+x,240+y,15);
putpixel(320+x,240-y,15);
putpixel(320-x,240+y,15);
putpixel(320-x,240-y,15);
}
while(x<99.9);
getch();
}
OUTPUT:-
WAP TO TRANSLATE A POINT.
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
void main()
{
int gd=DETECT,gm;
int xold,xnew,sx,ynew,yold,sy,x1old,y1old,x1new,y1new;
initgraph(&gd,&gm,"D:\tc\bgi");
printf("enter the end of the line=");
scanf("%d %d %d %d ",&xold,&yold,&x1old,y1old);
line(xold,yold,x1old,y1old);
printf("enter the translation in x direction and y direction=");
scanf("%d%d",&sx,&sy);
xnew=xold+sx;
ynew=yold+sy;
x1new=x1old+sx;
y1new=y1old+sy;
line(xnew,ynew,x1new,y1new);
printf(" New coordinates of p1:%d %d",xnew,ynew);
printf(" New coordinates of p2:%d %d",x1new,y1new);
getch();
closegraph();
}
OUTPUT:-
WAP TO ROTATE A LINE.
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
int h,k,x,y,x1,x2,y1,y2,x3,y3,x11,y11,x21,y21,x31,y31;
float theta,t;
initgraph(&gd,&gm,"D:\tc\bgi");
x1=100;
y1=100;
x2=150;
y2=150;
x3=200;
y3=150;
theta=30;
setcolor(3);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x1,y1,x3,y3);
getch();
t=theta*3.14/180;
x11=x1*cos(t)-y1*sin(t);
y11=x1*sin(t)+y1*cos(t);
x21=x2*cos(t)-y2*sin(t);
y21=x2*sin(t)+y2*cos(t);
x31=x3*cos(t)-y3*sin(t);
y31=x3*sin(t)+y3*cos(t);
setcolor(6);
line(x11,y11,x21,y21);
line(x21,y21,x31,y31);
line(x31,y31,x11,y11);
getch();
closegraph();
}
OUTPUT:-
WAP TO REFLECT A TRIANGLE
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int gd=DETECT,gm;
int m,x1,x2,y1,y2,x3,y3,xx1,xx2,xx3,yy1,yy2,yy3;
int x11,x22,x33,y11,y22,y33,xmax,ymax,i,a=0,x12,x13,x14,y12,y13,y14;
initgraph(&gd,&gm,"c:\tc\bgi");
xmax=320;
ymax=240;
line(320,0,320,470);
line(0,240,670,240);
outtextxy(325,245,"(0,0)");
outtextxy(5,245,"X");
outtextxy(620,245,"X");
outtextxy(320,5,"Y");
outtextxy(320,450,"Y");
printf("enter the value of x1=");
scanf("%d",&xx1);
printf("enter the value of y1=");
scanf("%d",&yy1);
printf("enter the value of x2=");
scanf("%d",&xx2);
printf("enter the value of y2=");
scanf("%d",&yy2);
printf("enter the value of x3=");
scanf("%d",&xx3);
printf("enter the value of y3=");
scanf("%d",&yy3);
x1=xx1+xmax;
x2=xx2+xmax;
x3=xx3+xmax;
y1=ymax-yy1;
y2=ymax-yy2;
y3=ymax-yy3;
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
x12=xmax-xx1;
x13=xmax-xx2;
x14=xmax-xx3;
y12=ymax-yy1;
y13=ymax-yy2;
y14=ymax-yy3;
line(x12,y12,x13,y13);
line(x13,y13,x14,y14);
line(x12,y12,x14,y14);
getch();
x12=xmax+xx1;
x13=xmax+xx2;
x14=xmax+xx3;
y12=ymax+yy1;
y13=ymax+yy2;
y14=ymax+yy3;
line(x12,y12,x13,y13);
line(x13,y13,x14,y14);
line(x12,y12,x14,y14);
getch();
closegraph();
}
OUTPUT:-
WAP FOR FLOODFILL ALGORITHM.
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
void floodfil(int x,int y,int fillcolor,int oldcolor);
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"D:\tc\bgi");
setbkcolor(15);
setfillstyle(SOLID_FILL,10);
bar(50,50,100,100);
floodfil(70,70,12,10);
getch();
closegraph();
}
void floodfil(int x,int y,int fillcolor,int oldcolor)
{
if(getpixel(x,y)==oldcolor)
{
setcolor(fillcolor);
putpixel(x,y,fillcolor);
delay(2);
floodfil(x+1,y,fillcolor,oldcolor);
floodfil(x-1,y,fillcolor,oldcolor);
floodfil(x,y-1,fillcolor,oldcolor);
floodfil(x,y+1,fillcolor,oldcolor);
}
}
OUTPUT:
WAP FOR 2-D TRANSFORMATION
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
void main()
{
int ch;
int gd=DETECT,gm;
void translate();
void scale();
void rotate();
initgraph(&gd,&gm,"D:\tc\bgi");
printf("1)Translate 2)Scale 3) Rotate");
printf(" Enter a choice=");
scanf("%d",&ch);
switch(ch)
{
case 1: translate();
break;
case 2: scale();
break;
case 3: rotate();
break;
default: printf("you have entered wrong choice");break;
}
getch();
closegraph();
}
void translate()
{
int tx,ty;
setcolor(15);
outtextxy(240,50,"TRANSLATION");
outtextxy(238,60,"------------");
printf(" Enter tx: ");
scanf("%d",&tx);
printf(" Enter ty: ");
scanf("%d",&ty);
rectangle(200,150,150,200);
printf(" After Translation");
rectangle(200+tx,150+ty,150+tx,200+ty);
}
void scale()
{
int sx,sy;
setcolor(15);
outtextxy(240,50,"SCALING");
outtextxy(238,60,"--------");
printf(" Enter sx: ");
scanf("%d",&sx);
printf(" Enter sy: ");
scanf("%d",&sy);
rectangle(200,150,150,200);
printf(" After Scaling");
rectangle(200+sx,150+sy,50*sx,200+sy);
}
void rotate()
{
float theta;
int x1,x2,x3,x4,y1,y2,y3,y4;
int ax1,ax2,ax3,ax4,ay1,ay2,ay3,ay4;
int refx,refy;
printf("Enter the angle of rotation=");
scanf("%f",theta);
theta=theta*(3.14/180);
setcolor(15);
outtextxy(240,80,"ROTATE");
outtextxy(238,90,"-------");
refx=100;
refx=100;
x1=100;
y1=100;
x2=150;
y2=100;
x3=150;
y3=150;
x4=100;
y4=150;
ax1=refy+(x1-refx)*cos(theta)-(y1-refy)*sin(theta);
ay1=refy+(x1-refx)*sin(theta)+(y1-refy)*cos(theta);
ax2=refy+(x2-refx)*cos(theta)-(y2-refy)*sin(theta);
ay2=refy+(x2-refx)*sin(theta)+(y2-refy)*cos(theta);
ax3=refy+(x3-refx)*cos(theta)-(y3-refy)*sin(theta);
ay3=refy+(x3-refx)*sin(theta)+(y3-refy)*cos(theta);
ax4=refy+(x4-refx)*cos(theta)-(y4-refy)*sin(theta);
ay4=refy+(x4-refx)*sin(theta)+(y4-refy)*cos(theta);
rectangle(100,150,150,100);
line(ax1,ay1,ax2,ay2);
line(ax2,ay2,ax3,ay3);
line(ax3,ay3,ax4,ay4);
line(ax4,ay4,ax1,ay1);
}
OUTPUT:-
1. What are some basic programs of Computer Graphics in C language? |
2. What is Computer Graphics? |
3. Why is C language used for Computer Graphics? |
4. What are the applications of Computer Graphics? |
5. How can one learn Computer Graphics programming in C language? |
|
Explore Courses for Computer Science Engineering (CSE) exam
|