Computer Graphics Practical File
Computer Graphics Practical File
Computer Graphics Practical File
PRACTICAL FILE
BASED ON
ROLL NO.:15
#include<graphics.h>
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void main()
int gd=DETECT,gm;
initgraph(&gd, &gm,"c:\\turboc3\\bgi");
//for head
ellipse(320,95,360,0,25,20);
line(298,85,341,85);
circle(310,90,2);
circle(330,90,2);
arc(320,100,200,-20,10);
//for neck
line(313,115,313,125);
line(328,115,328,125);
arc(320,225,72,107,100);
line(290,129,290,200);
line(350,129,350,200);
line(290,193,350,193);
line(290,200,350,200);
//for legs
line(290,200,285,280);
line(320,225,305,280);
line(322,225,335,280);
line(350,200,355,280);
line(290,129,255,165);
line(255,165,290,200);
line(290,149,275,165);
line(275,165,290,182);
line(350,129,385,165);
line(385,165,350,200);
line(350,149,365,165);
line(365,165,350,182);
getch();
closegraph();
}
Program-2: Write a program to draw a Rectangle using Line Function.
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
void main()
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
line(300,100,500,100);
line(300,100,300,200);
line(300,200,500,200);
line(500,100,500,200);
getch();
#include<iostream.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
void main()
int i,gd=DETECT,gm;
initgraph (&gd,&gm,"C:\\turboc3\\bgi");
int x0,x1,y0,y1;
float x,y,dx,dy,steps;
x0=100,y0=200,x1=500,y1=300;
dx=(float)(x1-x0);
dy=(float)(y1-y0);
steps=dx;
else{
steps=dy;
dx=dx/steps;
dy=dy/steps;
x=x0;
y=y0;
i=1;
while(i<= steps)
putpixel(x,y,5);
x+=dx;
y+=dy;
i=i+1;
getch();
closegraph();
}
Program 4: Write a program to draw a line using Bresenhem Line Algorithm.
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
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;
getch();
int main()
cin>>x0>>y0;
cin>>x1>>y1;
return 0;
}
Program 5: Write a program to draw a circle using midpoint algorithm.
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
int x = radius;
int y = 0;
int p = 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 (p <= 0)
y += 1;
p+= 2*y + 1;
if (p > 0)
x -= 1;
p -= 2*x + 1;
getch();
int main()
cin>>r;
cin>>x>>y;
drawcircle(x, y, r);
return 0;
}
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
cin>>xc>>yc;
cin>>r;
p = 3 - 2*r;
x=0; y = r;
drawCircle(x,y,xc,yc);
while(x < y)
{
if(p <= 0)
p= p + (4*x) + 6;
drawCircle(++x,y,xc,yc);
else
p = p + (4*(x-y)) + 10;
drawCircle(++x,--y,xc,yc);
getch();
closegraph();
putpixel(x+xc,y+yc,1);
putpixel(-x+xc,y+yc,2);
putpixel(x+xc,-y+yc,3);
putpixel(-x+xc,-y+yc,4);
putpixel(y+xc,x+yc,5);
putpixel(y+xc,-x+yc,6);
putpixel(-y+xc,x+yc,7);
putpixel(-y+xc,-x+yc,8);
}
Program 7: Write a program to draw an ellipse using Bresenham Algorithm.
Program 11: Write a program for line clipping using Cohen-Sutherland Algorithm.
Program 12: Write a program for polygon clipping using Sutherland-Hodgemann Algorithm.