Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Programs of CG

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Programs of Computer Graphics

1. Write a Program to draw basic graphics construction like line,


circle, arc, ellipse and rectangle.
#include<graphics.h>
#include<conio.h>
void main()
{
intgd=DETECT,gm;
initgraph (&gd,&gm,"c:\\tc\\bgi");
setbkcolor(GREEN);
printf("\t\t\t\n\nLINE");
line(50,40,190,40);
printf("\t\t\n\n\n\nRECTANGLE");
rectangle(125,115,215,165);
printf("\t\t\t\n\n\n\n\n\n\nARC");
arc(120,200,180,0,30);
printf("\t\n\n\n\nCIRCLE");
circle(120,270,30);
printf("\t\n\n\n\nECLIPSE");
ellipse(120,350,0,360,30,20);
getch();
}

2. Write a Program to draw animation using increasing circles filled with different
colors and patterns.

#include<graphics.h>
#include<conio.h>
void main()
{
intgd=DETECT, gm, i, x, y;
initgraph(&gd, &gm, "C:\\TC\\BGI");
x=getmaxx()/3;
y=getmaxx()/3;
setbkcolor(WHITE);
setcolor(BLUE);
for(i=1;i<=8;i++)
{
setfillstyle(i,i);
delay(20);
circle(x, y, i*20);
floodfill(x-2+i*20,y,BLUE);
}
getch();
closegraph();
}

3. Write a Program to implement bouncing ball using sine wave form.


#include<stdio.h>
#include<graphics.h>
#define HEIGHT getmaxy()
#define WIDTH getmaxx()
#define GROUND 450
#define MAXHEIGHT 420
void main()
{
int x,y=0,t=MAXHEIGHT,c=1;
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\T urboC3\\BGI");
for(x=40;x<=getmaxx();x=x+2)
{
rectangle (0,MAXHEIGHT,getmaxx(),MAXHEIGHT+5);
floodfill (5,MAXHEIGHT+3,WHITE);
//Draw Ball
pieslice(x,y,0,360,20);
//floodfill(x,y,RED);
delay(100);
if(y>MAXHEIGHT-20)
{
c=0;
t=t-40;
}
if(y<=(MAXHEIGHT-t))
{
c=1;
}
if(t>=40)
y=y+(c? 15:-15);
cleardevice();
//Exit upon keypress
if(kbhit())
break;
}
getch();
}

4. Program to draw circle using c graphics.


#include<stdio.h>
#include<graphics.h>
int main(){
int gd = DETECT,gm;
int x ,y ,radius=80;
initgraph(&gd, &gm, "location\of your BIN");
x = getmaxx()/2;
y = getmaxy()/2;
outtextxy(x-100, 50, "CIRCLE Using Graphics in C");
circle(x, y, radius);
closegraph();
return 0;
}

5. Program that draws a red circle

#include <graphics.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
setcolor(RED);
circle(100, 100, 50);
getch();
closegraph();
return 0;
}

6. Program to implement DDA Line Drawing Algorithm.

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
intgd = DETECT ,gm, i;
float x, y,dx,dy,steps;
int x0, x1, y0, y1;
initgraph(&gd, &gm, "C:\\TC\\BGI");
setbkcolor(WHITE);
x0 = 100 , y0 = 200, x1 = 500, y1 = 300;
dx = (float)(x1 - x0);
dy = (float)(y1 - y0);
if(dx>=dy)
{
steps = dx;
}
else
{
steps = dy;
}
dx = dx/steps;
dy = dy/steps;
x = x0;
y = y0;
i = 1;
while(i<= steps)
{
putpixel(x, y, RED);
x += dx;
y += dy;
i=i+1;
}
getch();
closegraph();
}

7. Program to implement Bresenham's Line Drawing Algorithm.

#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;
}

8. Write a program toChange Background Colour


#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
int main(void)
{
int gdriver = EGA, gmode = EGAHI, errorcode;
int x, y;
initgraph(&gdriver, &gmode, "c:/tc/bgi");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1);
}
settextjustify(CENTER_TEXT, CENTER_TEXT);
x = getmaxx() / 2;
y = getmaxy() / 2;
setbkcolor(GREEN);
setcolor(BLUE);
outtextxy(x, y, "MY NEW PAGE");
getch();
closegraph();
return 0;
}

9. Program to make screen saver in that display different size circles filled with
different colors and at random places.
#include<stdio.h>
#include<conio.h>
#include"graphics.h"
#include"stdlib.h"
void main()
{
intgd=DETECT,gm,i=0,x,xx,y,yy,r;
//Initializes the graphics system
initgraph(&gd,&gm,"c:\\tc\\bgi");
x=getmaxx();
y=getmaxy();
while(!kbhit())
{
i++;
setfillstyle(random(i),random(30));
circle(xx=random(x),yy=random(y),random(30));
setfillstyle(random(i),random(30));
floodfill(xx,yy,getmaxcolor());
delay(200);
}
getch();
}

10. Write a Program to implement Bouncing Ball in vertical direction.

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
int main()
{
int gd = DETECT, gm;
int i, x, y, flag=0;
initgraph(&gd, &gm, "C:\\TC\\BGI");
x = getmaxx()/2;
y = 30;
while (!kbhit())
{
if(y >= getmaxy()-30 || y <= 30)
flag = !flag;
setcolor(RED);
setfillstyle(SOLID_FILL, RED);
circle(x, y, 30);
floodfill(x, y, RED);
delay(50);
cleardevice();
if(flag)
{
y = y + 5;
}
else
{
y = y - 5;
}
}
getch();
closegraph();
return 0;
}

You might also like