0% found this document useful (0 votes)
371 views

Cgma Programs

The document contains 12 programs written in C programming language to perform various computer graphics tasks using graphics.h library. The programs include: 1) Drawing basic shapes like rectangle, circle, line, ellipse etc. 2) Drawing bar chart and pie chart 3) Animation programs to show moving car and smiling face 4) Drawing techniques like DDA line generation, Bresenham's line algorithm, Cohen-Sutherland line clipping 5) Rainbow generation and other miscellaneous graphics programs The programs demonstrate basic as well as advanced computer graphics concepts in C.

Uploaded by

Kunal Rana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
371 views

Cgma Programs

The document contains 12 programs written in C programming language to perform various computer graphics tasks using graphics.h library. The programs include: 1) Drawing basic shapes like rectangle, circle, line, ellipse etc. 2) Drawing bar chart and pie chart 3) Animation programs to show moving car and smiling face 4) Drawing techniques like DDA line generation, Bresenham's line algorithm, Cohen-Sutherland line clipping 5) Rainbow generation and other miscellaneous graphics programs The programs demonstrate basic as well as advanced computer graphics concepts in C.

Uploaded by

Kunal Rana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Computer Graphics Programs

1). WAP draw shapes using C graphics

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

main()
{
int gd = DETECT,gm,left=100,top=100,right=200,bottom=200,x=
300,y=150,radius=50;

initgraph(&gd, &gm, "C:\\TC\\BGI");

rectangle(left, top, right, bottom);


circle(x, y, radius);
bar(left + 300, top, right + 300, bottom);
line(left - 10, top + 150, left + 410, top + 150);
ellipse(x, y + 200, 0, 360, 100, 50);
outtextxy(left + 100, top + 325, "My first C graphics program");

getch();
closegraph();
return 0;
}

2). WAP to draw bar chart using c

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

main()
{
int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TC\\BGI");

setcolor(YELLOW);
rectangle(0,30,639,450);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
setcolor(WHITE);
outtextxy(275,0,"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();
return 0;
}

3).WAP Drawing concentric circles using c

#include <graphics.h>

int main()
{
int gd = DETECT, gm;
int x = 320, y = 240, radius;

initgraph(&gd, &gm, "C:\\TC\\BGI");

for ( radius = 25; radius <= 125 ; radius = radius + 20)


circle(x, y, radius);

getch();
closegraph();
return 0;
}

4). WAP to moving car using c

#include <graphics.h>
#include <dos.h>

int main()
{
int i, j = 0, gd = DETECT, gm;

initgraph(&gd,&gm,"C:\\TC\\BGI");

settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
outtextxy(25,240,"Press any key to view the moving car");

getch();

for( i = 0 ; i <= 420 ; i = i + 10, j++ )


{
rectangle(50+i,275,150+i,400);
rectangle(150+i,350,200+i,400);
circle(75+i,410,10);
circle(175+i,410,10);
setcolor(j);
delay(100);

if( i == 420 )
break;
if ( j == 15 )
j = 2;

cleardevice(); // clear screen


}

getch();
closegraph();
return 0;
}

5). WAP to Program to draw a pie chart usinng c

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

int main()
{
int gd = DETECT, gm, midx, midy;

initgraph(&gd, &gm, "C:\\TC\\BGI");

setcolor(MAGENTA);
rectangle(0,40,639,450);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
setcolor(WHITE);
outtextxy(275,10,"Pie Chart");

midx = getmaxx()/2;
midy = getmaxy()/2;

setfillstyle(LINE_FILL,BLUE);
pieslice(midx, midy, 0, 75, 100);
outtextxy(midx+100, midy - 75, "20.83%");

setfillstyle(XHATCH_FILL,RED);
pieslice(midx, midy, 75, 225, 100);
outtextxy(midx-175, midy - 75, "41.67%");

setfillstyle(WIDE_DOT_FILL,GREEN);
pieslice(midx, midy, 225, 360, 100);
outtextxy(midx+75, midy + 75, "37.50%");
getch();
return 0;
}

6). WAP to draws a 3d bar chart. Using c

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

int main()
{
int gd = DETECT, gm;

initgraph(&gd, &gm, "C:\\TC\\BGI");

setcolor(YELLOW);
rectangle(0,30,639,450);
settextstyle(SANS_SERIF_FONT,HORIZ_DIR,2);
setcolor(WHITE);
outtextxy(275,0,"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();
return 0;
}

7). WAP to draw. Smiling face animation using c

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

main()
{
int gd = DETECT, gm, area, temp1, temp2, left = 25, top = 75;
void *p;

initgraph(&gd, &gm, "C:\\TC\\BGI");

setcolor(YELLOW);
circle(50, 100, 25);
setfillstyle(SOLID_FILL, YELLOW);
floodfill(50, 100, YELLOW);

setcolor(BLACK);
setfillstyle(SOLID_FILL, BLACK);
fillellipse(44, 85, 2, 6);
fillellipse(56, 85, 2, 6);

ellipse(50, 100, 205, 335, 20, 9);


ellipse(50, 100, 205, 335, 20, 10);
ellipse(50, 100, 205, 335, 20, 11);
area = imagesize(left, top, left + 50, top + 50);
p = malloc(area);

setcolor(WHITE);
settextstyle(SANS_SERIF_FONT, HORIZ_DIR, 2);
outtextxy(155, 451, "Smiling Face Animation");

setcolor(BLUE);
rectangle(0, 0, 639, 449);

while(!kbhit())
{
temp1 = 1 + random (588);
temp2 = 1 + random (380);

getimage(left, top, left + 50, top + 50, p);


putimage(left, top, p, XOR_PUT);
putimage(temp1 , temp2, p, XOR_PUT);
delay(100);
left = temp1;
top = temp2;
}

getch();
closegraph();
return 0;
}

8). WAP to make a rainbow using c

#include<stdio.h>
#include<graphics.h>;
#include<dos.h>;
  
// function for making of rainbow
void rainbow()
{
    // auto detection
    int gdriver = DETECT,gmode;
    int x, y, i;
  
    // initialize graphics mode(passed three arguments to
    // initgraph function)
    // &gdriver is the address of gdriver variable, &gmode is
    // the address of gmode and
    //  "C:\\Turboc3\\BGI" is the directory path where BGI files are stored
    initgraph(&gdriver,&gmode,"C:\\Turboc3\\BGI");
  
    x = getmaxx() / 2;//finding centre x-ordinate of screen
    y = getmaxy() / 2;//finding centre y-ordinate of screen
  
    for (i=30; i<200; i++)
    {
        // delay function under dos.h for holding the
        // function for some time
        delay(100);
  
        // selecting  color for making of rainbow
        setcolor(i/10);
  
        // making of arc with fixed centre and increasing radius
        arc(x, y, 0, 180, i-10);
    }
}
// driver program
int main()
{
    rainbow();
    return 0;
}

9.) WAP to draw ellipse using c

#include <graphics.h>
  
int main()
{
    // gm is Graphics mode which is a computer display
    // mode that generates image using pixels.
    // DETECT is a macro defined in "graphics.h" header file
    int gd = DETECT, gm;
  
    // location of ellipse
    int x = 250, y = 200;
  
    // here is the starting angle
    // and end angle
    int start_angle = 0;
    int end_angle = 360;
  
    // radius from x axis and y axis
    int x_rad = 100;
    int y_rad = 50;
  
    // initgraph initializes the graphics system
    // by loading a graphics driver from disk
    initgraph(&gd, &gm, "");
  
    // ellipse fuction
    ellipse(x, y, start_angle,
     end_angle, x_rad, y_rad);
  
    getch();
  
    // closegraph function closes the graphics
    // mode and deallocates all memory allocated
    // by graphics system .
    closegraph();
  
    return 0;
}

10.) WAP for DDA line generation


#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <dos.h>
 
void main( )
{
float x,y,x1,y1,x2,y2,dx,dy,step;
int i,gd=DETECT,gm;
 
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
 
printf("Enter the value of x1 and y1 : ");
scanf("%f%f",&x1,&y1);
printf("Enter the value of x2 and y2: ");
scanf("%f%f",&x2,&y2);
 
dx=abs(x2-x1);
dy=abs(y2-y1);
 
if(dx>=dy)
step=dx;
else
step=dy;
 
dx=dx/step;
dy=dy/step;
 
x=x1;
y=y1;
 
i=1;
while(i<=step)
{
putpixel(x,y,5);
x=x+dx;
y=y+dy;
i=i+1;
delay(100);
}
 
closegraph();
}
Outptut
11.) WAP to Bresenham’s Line Drawing Algorithm in C
#include<stdio.h>
#include<graphics.h>
 
void drawline(int x0, int y0, int x1, int y1)
{
    int dx, dy, p, x, y;
 
dx=x1-x0;
dy=y1-y0;
 
x=x0;
y=y0;
 
p=2*dy-dx;
 
while(x<x1)
{
if(p>=0)
{
putpixel(x,y,7);
y=y+1;
p=p+2*dy-2*dx;
}
else
{
putpixel(x,y,7);
p=p+2*dy;
}
x=x+1;
}
}
 
int main()
{
int gdriver=DETECT, gmode, error, x0, y0, x1, y1;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
 
printf("Enter co-ordinates of first point: ");
scanf("%d%d", &x0, &y0);
 
printf("Enter co-ordinates of second point: ");
scanf("%d%d", &x1, &y1);
drawline(x0, y0, x1, y1);
 
return 0;
}

12.) Program for Cohen Sutherland Line Clipping Algorithm in C


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
 
typedef struct coordinate
{
int x,y;
char code[4];
}PT;
 
void drawwindow();
void drawline(PT p1,PT p2);
PT setcode(PT p);
int visibility(PT p1,PT p2);
PT resetendpt(PT p1,PT p2);
 
void main()
{
int gd=DETECT,v,gm;
PT p1,p2,p3,p4,ptemp;

printf("\nEnter x1 and y1\n");


scanf("%d %d",&p1.x,&p1.y);
printf("\nEnter x2 and y2\n");
scanf("%d %d",&p2.x,&p2.y);

initgraph(&gd,&gm,"c:\\turboc3\\bgi");
drawwindow();
delay(500);

drawline(p1,p2);
delay(500);
cleardevice();

delay(500);
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
delay(500);

switch(v)
{
case 0: drawwindow();
delay(500);
drawline(p1,p2);
break;
case 1: drawwindow();
delay(500);
break;
case 2: p3=resetendpt(p1,p2);
p4=resetendpt(p2,p1);
drawwindow();
delay(500);
drawline(p3,p4);
break;
}

delay(5000);
closegraph();
}
 
void drawwindow()
{
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
 
void drawline(PT p1,PT p2)
{
line(p1.x,p1.y,p2.x,p2.y);
}
 
PT setcode(PT p) //for setting the 4 bit code
{
PT ptemp;

if(p.y<100)
ptemp.code[0]='1'; //Top
else
ptemp.code[0]='0';

if(p.y>350)
ptemp.code[1]='1'; //Bottom
else
ptemp.code[1]='0';

if(p.x>450)
ptemp.code[2]='1'; //Right
else
ptemp.code[2]='0';
if(p.x<150)
ptemp.code[3]='1'; //Left
else
ptemp.code[3]='0';

ptemp.x=p.x;
ptemp.y=p.y;

return(ptemp);
}
 
int visibility(PT p1,PT p2)
{
int i,flag=0;

for(i=0;i<4;i++)
{
if((p1.code[i]!='0') || (p2.code[i]!='0'))
flag=1;
}

if(flag==0)
return(0);

for(i=0;i<4;i++)
{
if((p1.code[i]==p2.code[i]) && (p1.code[i]=='1'))
flag='0';
}

if(flag==0)
return(1);

return(2);
}
 
PT resetendpt(PT p1,PT p2)
{
PT temp;
int x,y,i;
float m,k;

if(p1.code[3]=='1')
x=150;

if(p1.code[2]=='1')
x=450;

if((p1.code[3]=='1') || (p1.code[2]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(p1.y+(m*(x-p1.x)));
temp.y=k;
temp.x=x;

for(i=0;i<4;i++)
temp.code[i]=p1.code[i];

if(temp.y<=350 && temp.y>=100)


return (temp);
}

if(p1.code[0]=='1')
y=100;

if(p1.code[1]=='1')
y=350;

if((p1.code[0]=='1') || (p1.code[1]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(float)p1.x+(float)(y-p1.y)/m;
temp.x=k;
temp.y=y;

for(i=0;i<4;i++)
temp.code[i]=p1.code[i];

return(temp);
}
else
return(p1);
}

Output

Before Clipping

After Clipping

13.) WAP for midpoint circle algorithm in c.

#include<stdio.h>
#include<graphics.h>
 
void drawcircle(int x0, int y0, int radius)
{
    int x = radius;
    int y = 0;
    int err = 0;
 
    while (x >= y)
    {
putpixel(x0 + x, y0 + y, 7);
putpixel(x0 + y, y0 + x, 7);
putpixel(x0 - y, y0 + x, 7);
putpixel(x0 - x, y0 + y, 7);
putpixel(x0 - x, y0 - y, 7);
putpixel(x0 - y, y0 - x, 7);
putpixel(x0 + y, y0 - x, 7);
putpixel(x0 + x, y0 - y, 7);
 
if (err <= 0)
{
    y += 1;
    err += 2*y + 1;
}
 
if (err > 0)
{
    x -= 1;
    err -= 2*x + 1;
}
    }
}
 
int main()
{
int gdriver=DETECT, gmode, error, x, y, r;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
 
printf("Enter radius of circle: ");
scanf("%d", &r);
 
printf("Enter co-ordinates of center(x and y): ");
scanf("%d%d", &x, &y);
drawcircle(x, y, r);
 
return 0;
}

Output

14.) WAP to draw a hut using c


#include<graphics.h>
#include<conio.h>
 
int main(){
int gd =0,gm;
initgraph(&gd, &gm, “”);
setcolor(WHITE);
rectangle(150,180,250,300);
rectangle(250,180,420,300);
rectangle(180,250,220,300);
line(200,100,150,180);
line(200,100,250,180);
line(200,100,370,100);
line(370,100,420,180);
setfillstyle(SOLID_FILL, BROWN);
floodfill(152, 182, WHITE);
floodfill(252, 182, WHITE);
setfillstyle(SLASH_FILL, BLUE);
floodfill(182, 252, WHITE);
setfillstyle(HATCH_FILL, GREEN);
floodfill(200, 105, WHITE);
floodfill(210, 105, WHITE);
 
getch();
closegraph();
return 0;
}
 
Output:

15.) WAP for draw a star

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\TC\\bgi");
line(150,100,100,200);
line(100,200,200,200);
line(200,200,150,100);
line(100,125,200,125);
line(100,125,150,225);
line(150,225,200,125);
getch();
closegraph();
}
 
OUTPUT:
16.) WAP for 100 twinkling star using for loop
#include <conio.h>
#include <graphics.h>
#include <dos.h>
#include <stdlib.h>
 
int main() {
    int gd = DETECT, gm;
    int i, x, y;
    initgraph(&gd, &gm, "C:\\TC\\BGI");
        
 
 while (!kbhit()) {
      /* color 500 random pixels on screen */
   for(i=0; i<=500; i++) {
       x=rand()%getmaxx();
          y=rand()%getmaxy();
          putpixel(x,y,15);
      }
      delay(500);
 
      /* clears screen */
      cleardevice();
    }
 
    getch();
    closegraph();
    return 0;
}
Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy