0% found this document useful (0 votes)
13 views2 pages

BCA4001_Computer_Graphics_Practical

The document contains four programs demonstrating different graphics algorithms: DDA Line Drawing, Bresenham Line Drawing, Bresenham Circle Drawing, and Midpoint Ellipse Drawing. Each program includes code written in C that initializes a graphics window and uses specific algorithms to draw lines, circles, or ellipses. The algorithms utilize pixel plotting techniques to render shapes on the screen.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

BCA4001_Computer_Graphics_Practical

The document contains four programs demonstrating different graphics algorithms: DDA Line Drawing, Bresenham Line Drawing, Bresenham Circle Drawing, and Midpoint Ellipse Drawing. Each program includes code written in C that initializes a graphics window and uses specific algorithms to draw lines, circles, or ellipses. The algorithms utilize pixel plotting techniques to render shapes on the screen.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Program 1: DDA Line Drawing Algorithm

Code:
#include<graphics.h>
void main() {
int gd=DETECT,gm; initgraph(&gd,&gm,"C:\\Turboc3\\BGI");
int x1=50,y1=50,x2=150,y2=100;
float dx=x2-x1, dy=y2-y1, step=abs(dx)>abs(dy)?abs(dx):abs(dy);
float x=x1, y=y1, xi=dx/step, yi=dy/step;
for(int i=0;i<=step;i++){ putpixel(x,y,15); x+=xi; y+=yi; }
getch(); closegraph();
}

Program 2: Bresenham Line Drawing Algorithm


Code:
#include<graphics.h>
void main(){
int gd=DETECT,gm,x1=20,y1=20,x2=200,y2=100;
initgraph(&gd,&gm,"C:\\Turboc3\\BGI");
int dx=x2-x1, dy=y2-y1, p=2*dy-dx, x=x1, y=y1;
for(int i=0;i<=dx;i++){
putpixel(x,y,15);
x++;
if(p<0) p+=2*dy;
else{ y++; p+=2*(dy-dx); }
}
getch(); closegraph();
}

Program 3: Bresenham Circle Drawing Algorithm


Code:
#include<graphics.h>
void main(){
int gd=DETECT,gm,r=50,x=0,y=r,d=3-2*r;
initgraph(&gd,&gm,"C:\\Turboc3\\BGI");
while(x<=y){
putpixel(200+x,200+y,15); putpixel(200+y,200+x,15);
putpixel(200-x,200+y,15); putpixel(200-y,200+x,15);
putpixel(200+x,200-y,15); putpixel(200+y,200-x,15);
putpixel(200-x,200-y,15); putpixel(200-y,200-x,15);
if(d<0) d += 4*x+6;
else{ d += 4*(x-y)+10; y--; }
x++;
}
getch(); closegraph();
}

Program 4: Midpoint Ellipse Drawing


Code:
#include<graphics.h>
#include<math.h>
void main(){
int gd=DETECT,gm,x=0,y,r1=100,r2=50;
float p;
initgraph(&gd,&gm,"C:\\Turboc3\\BGI");
y=r2; p=r2*r2 - r1*r1*r2 + 0.25*r1*r1;
while(2*r2*r2*x < 2*r1*r1*y){
putpixel(200+x,200+y,15); putpixel(200-x,200+y,15);
putpixel(200+x,200-y,15); putpixel(200-x,200-y,15);
if(p<0) p += 2*r2*r2*x + r2*r2;
else{ y--; p += 2*r2*r2*x - 2*r1*r1*y + r2*r2; }
x++;
}
p = r2*r2*(x+0.5)*(x+0.5) + r1*r1*(y-1)*(y-1) - r1*r1*r2*r2;
while(y>=0){
putpixel(200+x,200+y,15); putpixel(200-x,200+y,15);
putpixel(200+x,200-y,15); putpixel(200-x,200-y,15);
if(p>0) p -= 2*r1*r1*y - r1*r1;
else{ x++; p += 2*r2*r2*x - 2*r1*r1*y + r1*r1; }
y--;
}
getch(); closegraph();
}

You might also like