0% found this document useful (0 votes)
393 views26 pages

C G Sample Programs

This document contains C code for several computer graphics programs that draw basic shapes like lines, circles, ellipses, and more complex images like a fish and flag. It includes programs that demonstrate different algorithms for drawing lines and circles like DDA, Bresenham's and midpoint algorithms. Programs are included that animate shapes by moving them and changing their colors. The programs provide examples of using graphics functions in C like line, circle, ellipse, rectangle to draw images and floodfill to fill areas with color.

Uploaded by

saviodev
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
393 views26 pages

C G Sample Programs

This document contains C code for several computer graphics programs that draw basic shapes like lines, circles, ellipses, and more complex images like a fish and flag. It includes programs that demonstrate different algorithms for drawing lines and circles like DDA, Bresenham's and midpoint algorithms. Programs are included that animate shapes by moving them and changing their colors. The programs provide examples of using graphics functions in C like line, circle, ellipse, rectangle to draw images and floodfill to fill areas with color.

Uploaded by

saviodev
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 26

C G sample programs

Program to Draw a Line using DDA Algorithm CG


#include <stdio.h>
#include <dos.h>
#include <graphics.h>
void lineDDA(int, int, int, int);
void main()
{
int x1, y1, xn, yn;
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
printf("Enter the starting coordinates of line: ");
scanf("%d %d", &x1, &y1);
printf("Enter the ending coordinates of line: ");
scanf("%d %d", &xn, &yn);
lineDDA(x1, y1, xn, yn);
getch();
}
void lineDDA(int x1, int y1, int xn, int yn)
{
int dx, dy, m, i;
m = (yn-y1)/(xn-x1);
for (i=x1; i<=xn; i++)
{
if (m <= 1)
{
dx = 1;
dy = m * dx;
}
else
{
dy = 1;
dx = dy / m;
}
x1 = x1 + dx;
y1 = y1 + dy;
putpixel(x1, y1, RED);
delay(20);
}
}

Program to Draw a Line using Bresenham's


#include <stdio.h>
#include <dos.h>
#include <graphics.h>
void lineBres(int, int, int, int);
void main()
{
int x1, y1, xn, yn;
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
printf("Enter starting coordinates of line: ");
scanf("%d %d", &x1, &y1);
printf("Enter ending coordinates of line: ");
scanf("%d %d", &xn, &yn);
lineBres(x1, y1, xn, yn);
getch();
}
void lineBres(int x1, int y1, int xn, int yn)
{
int dx = xn - x1, dy = yn - y1;
int di = 2 * dy - dx;
int ds = 2 * dy, dt = 2 * (dy - dx);
putpixel(x1, y1, RED);
while (x1 < xn)
{
x1++;
if (di < 0)
di = di + ds;
else
{
y1++;
di = di + dt;
}
putpixel(x1, y1, RED);
delay(20);
}
}

Program to Draw a Circle using Mid - Point


Algorithm CG
#include <stdio.h>
#include <dos.h>
#include <graphics.h>
void circleMidpoint(int, int, int);
void drawCircle(int, int, int, int);
void main()
{
int xc, yc, r;
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
printf("Enter center coordinates of circle: ");
scanf("%d %d", &xc, &yc);
printf("Enter radius of circle: ");
scanf("%d", &r);
circleMidpoint(xc, yc, r);
getch();
}
void circleMidpoint(int xc, int yc, int r)
{
int x = 0, y = r;
int p = 1 - r;
while (x < y)
{
drawCircle(xc, yc, x, y);
x++;
if (p < 0)
p = p + 2 * x + 1;
else
{
y--;
p = p + 2 * (x - y) + 1;
}
drawCircle(xc, yc, x, y);
delay(50);
}
}
void drawCircle(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y, RED);
putpixel(xc-x, yc+y, RED);

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


putpixel(xc-x, yc-y, RED);
putpixel(xc+y, yc+x, RED);
putpixel(xc-y, yc+x, RED);
putpixel(xc+y, yc-x, RED);
putpixel(xc-y, yc-x, RED);
}

Program to Draw a Circle using Bresenham's


Algorithm CG
#include <stdio.h>
#include <dos.h>
#include <graphics.h>
void circleBres(int, int, int);
void drawCircle(int, int, int, int);
void main()
{
int xc, yc, r;
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
printf("Enter center coordinates of circle: ");
scanf("%d %d", &xc, &yc);
printf("Enter radius of circle: ");
scanf("%d", &r);
circleBres(xc, yc, r);
getch();
}
void circleBres(int xc, int yc, int r)
{
int x = 0, y = r;
int d = 3 - 2 * r;
while (x < y)
{
drawCircle(xc, yc, x, y);
x++;
if (d < 0)
d = d + 4 * x + 6;
else
{
y--;
d = d + 4 * (x - y) + 10;
}
drawCircle(xc, yc, x, y);
delay(50);
}
}
void drawCircle(int xc, int yc, int x, int y)
{
putpixel(xc+x, yc+y, RED);

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


putpixel(xc+x, yc-y, RED);
putpixel(xc-x, yc-y, RED);
putpixel(xc+y, yc+x, RED);
putpixel(xc-y, yc+x, RED);
putpixel(xc+y, yc-x, RED);
putpixel(xc-y, yc-x, RED);
}

Program to draw a Circle (Simple Prorgam)


#include<math.h>
#include<iostream.h>
#include<conio.h>
#include<graphics.h>

void main()
{
int gd=DETECT,gm;
int x,y,r,c1;
initgraph(&gd,&gm,"");
cout<<"enter x,y,r";
cin>>x>>y>>r;
circle(x,y,r);
c1=rand();
setfillstyle(SOLID_FILL,c1);
floodfill(x,y,2);
getch();
closegraph();
}

Program to draw a Circle having Changing its


Color and Design CG
#include<graphics.h>
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
void main()
{
int gd=DETECT,gm;
int x,y,r,c,mx,my;
initgraph(&gd,&gm," ");
cout<<"enter x, y , r,c";
cin>>x>>y>>r>>c;
mx=getmaxx();
my=getmaxy();
while(!kbhit())
{
if((x>=mx)||(y>=my))
{
while((x>0)||(y>0))
{
cleardevice();
setcolor(c);
circle(x,y,r);
setfillstyle(rand()%11,rand()%16);
floodfill(x,y,c);
x=x-rand()%10;
y=y-rand()%10;
}
}
else
{
while((x<=mx)||(y<=my))
{
cleardevice();
setcolor(c);
circle(x,y,r);
setfillstyle(rand()%11,rand()%16);
floodfill(x,y,c);
x=x+rand()%10;
y=y+rand()%10;
}
}
delay(10);

}
getch();
closegraph();
}

Program to draw a Circle using Direct Algorithm


#include<graphics.h>
#include<iostream.h>
#include<stdio.h>
#include<conio.h>

void main()
{
int gd=DETECT, gm;
float x1,x2,y1,y2,x,y,m,c,dx,dy,clr;
initgraph(&gd,&gm," ");
cout<<"enter the value of x1,y1,x2,y2 and color";
cin>>x1>>y1>>x2>>y2>>clr;
x=x1; y=y1;
dx=x2-x1;
dy=y2-y1;
m=dy/dx;
c=y-m*c;
if(dx==0)
{
for(;y<=y2;y++)
{
x=x1;
putpixel(x,y,clr);
}
}
else if(dy==0)
{
for(;x<=x2;x++)
{
y=y1;
putpixel(x,y,clr);
}
}
else
{
m=dy/dx;
if(m<1)
{
for(;x<=x2;x++)
{
y=m*x+ c;
putpixel(x,y,clr);
}
}
else if(m==1)

{
for(;x<=x2;x++)
{
y=y+1;
putpixel(x,y,clr);
}
}
else
{
for(;y<=y2;y++)
{
x=(y-c)/m;
putpixel(x,y,clr);
}
}
}
getch();
closegraph();
}

Program to Draw an Ellipse using Mid - Point


Algorithm
#include <stdio.h>
#include <dos.h>
#include <graphics.h>
void ellipseMidpoint(float, float, float, float);
void drawEllipse(float, float, float, float);
void main()
{
float xc, yc, rx, ry;
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
printf("\nEnter the center coordinates of ellipse: ");
scanf("%f %f", &xc, &yc);
printf("\nEnter x-radius coordinate: ");
scanf("%f", &rx);
printf("\nEnter y-radius coordiante: ");
scanf("%f", &ry);
ellipseMidpoint(xc, yc, rx, ry);
getch();
}
void ellipseMidpoint(float xc, float yc, float rx, float ry)
{
float rxSq = rx * rx;
float rySq = ry * ry;
float x = 0, y = ry, p;
float px = 0, py = 2 * rxSq * y;
drawEllipse(xc, yc, x, y);
//Region 1
p = rySq - (rxSq * ry) + (0.25 * rxSq);
while (px < py)
{
x++;
px = px + 2 * rySq;
if (p < 0)
p = p + rySq + px;
else
{
y--;
py = py - 2 * rxSq;
p = p + rySq + px - py;
}
drawEllipse(xc, yc, x, y);
delay(30);
}

//Region 2
p = rySq*(x+0.5)*(x+0.5) + rxSq*(y-1)*(y-1) - rxSq*rySq;
while (y > 0)
{
y--;
py = py - 2 * rxSq;
if (p > 0)
p = p + rxSq - py;
else
{
x++;
px = px + 2 * rySq;
p = p + rxSq - py + px;
}
drawEllipse(xc, yc, x, y);
delay(30);
}
}
void drawEllipse(float xc, float yc, float x, float y)
{
putpixel(xc+x, yc+y, RED);
putpixel(xc-x, yc+y, RED);
putpixel(xc+x, yc-y, RED);
putpixel(xc-x, yc-y, RED);
}

Program to draw an Ellipse with Different Colours


#include<graphics.h>
#include<iostream.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
clrscr();
setcolor(1);
ellipse(100,100,0,90,50,30);
outtextxy(100,70,"90 degree");
setcolor(2);
ellipse(100,100,90,180,50,30);
outtextxy(50,100,"180 degree");
setcolor(3);
ellipse(100,100,180,270,50,30);
outtextxy(100,150,"270 degree");
setcolor(4);
ellipse(100,100,270,360,50,30);
outtextxy(150,100,"360 degree");
setcolor(5);
W3Professors.Com Hartej Singh Page No. 2
W3Professors.Com Hartej Singh
line(100,50,100,150);
setcolor(6);
line(30,100,170,100);
getch();
closegraph();
}

Program to draw Polar Ellipse


#include<graphics.h>
#include<conio.h>
#include<iostream.h>
#include<math.h>
#include<stdlib.h>
#include<DOS.h>
void ep(float x,float y,float xc,float yc)
{
putpixel(x+xc,y+yc,RED);
putpixel(-x+xc,y+yc,RED);
putpixel(-x+xc,-y+yc,RED);
putpixel(x+xc,-y+yc,RED);
delay(100);
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
cleardevice();
int x, y,xc,yc,xr,yr,theta,theta_end;
cout<<"enter values for xc,yc,xr,yr";
cin>>xc>>yc>>xr>>yr;
theta=0;
theta_end=90;
while (theta<=theta_end)
{
x=xr*cos (theta) + xc;
y=yr*sin (theta) + yc;
ep(x, y, xc, yc);
theta=theta+1;
}
getch();
closegraph();
}

Program to draw an Ellipse Showing Two Axis


#include<graphics.h>
#include<iostream.h>
#include<conio.h>
void main()
{
int gd=DETECT, gm, a, b, c, d, e, f;
initgraph(&gd, &gm, " ");
cout<<"enter 6 colours";
cin>>a>>b>>c>>d>>e>>f;
setcolor(a);
line(200,100,200,300);
setcolor(b);
line(000,200,400,200);
setcolor(c);
ellipse(200,200,0,90,100,50);
setcolor(d);
ellipse(200,200,90,180,100,50);
setcolor(e);
ellipse(200,200,180,270,100,50);
setcolor(f);
ellipse(200,200,270,360,100,50);
getch();
closegraph();
}

Program to Make a Fish


#include<graphics.h>
#include<conio.h>
#include<iostream.h>
void main()
{
int gd = DETECT,gm;
initgraph(&gd,&gm ,"");
ellipse(200,200,0,360,50,30);
line(250,200,280,170);
line(280,170,280,230);
line(280,230,250,200);
circle(160,190,3);
getch();
closegraph();
}

Program to Move a Fish


#include<graphics.h>
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
void main()
{
int gd=DETECT,gm;
int x,y,r,c,mx,my;
initgraph(&gd,&gm," ");
cout<<"enter x, y ,c";
cin>>x>>y>>c;
mx=getmaxx();
my=getmaxy();
while(!kbhit())
{
if((x>=mx)||(y>=my))
{
while((x>0)||(y>0))
{
cleardevice();
setcolor(c);
ellipse(x,y,0,360,50,20);
circle(x-40,y-5,2);
line(x+50,y,x+80,y-30);
line(x+80,y-30,x+80,y+30);
line(x+80,y+30,x+50,y);
setfillstyle(1,0);
floodfill(x,y,c);
x=x-rand()%10;
y=y-rand()%10;
}
}
else
{
while((x<=mx)||(y<=my))
{
cleardevice();
setcolor(c);
ellipse(x,y,0,360,50,20);
circle(x-40,y-5,2);
line(x+50,y,x+80,y-30);
line(x+80,y-30,x+80,y+30);
line(x+80,y+30,x+50,y);
setfillstyle(1,0);
floodfill(x,y,c);

x=x+rand()%10;
y=y+rand()%10;
}
}
delay(200);
}
getch();
closegraph();
}

Program to Make a Flag


#include<graphics.h>
#include<conio.h>
#include<iostream.h>
void main()
{
int gd = DETECT,gm;
clrscr();
initgraph(&gd,&gm," ");
setcolor(5);
line(20,50,20,180);
line(20,50,100,50);
line(20,70,100,70);
line(20,90,100,90);
line(20,110,100,110);
line(100,50,100,110);
circle(60,80,10);
line(52,80,68,80);
line(60,72,60,88);
setfillstyle(SOLID_FILL,22);
floodfill(21,51,5);
setfillstyle(SOLID_FILL,7);
floodfill(21,75,5);
setfillstyle(SOLID_FILL,2);
floodfill(21,95,5);
setfillstyle(SOLID_FILL,7);
floodfill(81,75,5);
setfillstyle(SOLID_FILL,7);
floodfill(61,81,5);
getch();
closegraph();
}

Program to Make a Hut


#include<graphics.h>
#include<conio.h>
#include<iostream.h>
void main()
{
int gd = DETECT,gm;
clrscr();
initgraph(&gd,&gm," ");
setcolor(6);
rectangle(50,180,150,300);
rectangle(150,180,320,300);
rectangle(80,250,120,300);
line(100,100,50,180);
line(100,100,150,180);
line(100,100,300,100);
line(300,100,320,180);
getch();
closegraph();
}

Program to Make a Kite


#include<graphics.h>
#include<conio.h>
#include<iostream.h>
void main()
{
int a,b,gd=DETECT,gm,i;
initgraph(&gd,&gm," ");
line(100,100,50,180);
line(100,100,150,180);
line(50,180,100,250);
line(150,180,100,250);
line(100,100,100,250);
line(50,180,150,180);
line(100,250,70,300);
line(100,250,130,300);
line(70,300,130,300);
line(100,300,120,320);
line(120,320,80,340);
line(80,340,120,360);
line(120,360,80,380);
setcolor(4);
getch();
closegraph();
}

Program of Road Animation


#include<graphics.h>
#include<iostream.h>
#include<conio.h>

void main()
{
int gd=DETECT, gm, c, d, e, f, g;
clrscr();
initgraph(&gd, &gm, " ");
cout<<"enter 5 colours";
cin>>c>>d>>e>>f>>g;
setcolor(c);
rectangle(100,100,300,150);
setfillstyle(SOLID_FILL,d);
floodfill(120,120,c);
rectangle(100,150,300,200);
setfillstyle(SOLID_FILL,e);
floodfill(120,160,c);
rectangle(100,200,300,250);
line(100,250,100,475);
setfillstyle(SOLID_FILL,f);
floodfill(120,220,c);
circle(200,175,20);
setcolor(g);
line(200,160,200,190);
line(180,175,220,175);
line(190,167,210,183);
line(190,183,210,167);
getch();
closegraph();
}

Program of Scaling Transformation


#include<graphics.h>
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
int x1,y1,x2,y2,sx,sy,x3,y3,x4,y4,x5,y5,x6,y6;
cout<<"x1,y1,x2,y2";
cin>>x1>>y1>>x2>>y2;
rectangle(x1,y1,x2,y2);
cout<<"sx,sy";
cin>>sx>>sy;
x3=x1*sx;
y3=y1*sy;
x4=x2*sx;
y4=y2*sy;
rectangle(x3,y3,x4,y4);
cout<<"-sx,-sy";
cin>>sx>>sy;
x5=x1/sx;
y5=y1/sy;
x6=x2/sx;
y6=y2/sy;
rectangle(x5,y5,x6,y6);
getch();
closegraph();
}

Program to Move a Person having Balloons


#include<graphics.h>
#include<conio.h>
#include<iostream.h>
#include<DOS.h>
#include<stdlib.h>
#include<math.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
int x,y,xm;
cout<<"enter x,y";
cin>>x>>y;
xm=getmaxx();
circle(x,y,50);
ellipse(x-25,y-15,0,360,3,2);
ellipse(x+25,y-15,0,360,3,2);
line(x,y-10,x,y+10);
arc(x,y+15,180,360,15);
line(x,y+50,x,y+130);
line(x,y+130,x-30,y+180);
line(x-30,y+180,x-50,y+170);
line(x,y+130,x+30,y+180);
line(x+30,y+180,x+50,y+170);
line(x,y+75,x+75,y+75);
line(x+75,y+75,x+170,y-10);
ellipse(x+170,y-50,0,360,28,40);
while(!kbhit())
{
if(x<=xm)
{
cleardevice();
x=x+20;
circle(x,y,50);
ellipse(x-25,y-15,0,360,3,2);
ellipse(x+25,y-15,0,360,3,2);
line(x,y-10,x,y+10);
arc(x,y+15,180,360,15);
line(x,y+50,x,y+130);
line(x,y+130,x-30,y+180);
line(x-30,y+180,x-50,y+170);
line(x,y+130,x+30,y+180);
line(x+30,y+180,x+50,y+170);
line(x,y+75,x+75,y+75);
line(x+75,y+75,x+170,y-10);

ellipse(x+170,y-50,0,360,28,40);
delay(100);
}
else
{
do
{
cleardevice();
x=x-20;
circle(x,y,50);
ellipse(x-25,y-15,0,360,3,2);
ellipse(x+25,y-15,0,360,3,2);
line(x,y-10,x,y+10);
arc(x,y+15,180,360,15);
line(x,y+50,x,y+130);
line(x,y+130,x-30,y+180);
line(x-30,y+180,x-50,y+170);
line(x,y+130,x+30,y+180);
line(x+30,y+180,x+50,y+170);
line(x,y+75,x+75,y+75);
line(x+75,y+75,x+170,y-10);
ellipse(x+170,y-50,0,360,28,40);
delay(100);
}
while(x!=0);
}
}
getch();
closegraph();
}

You might also like