You are on page 1of 17

LINE USING DDA ALGORITHM

Experiment no:1 Date:13-12-11 Aim: Write a C program to implement DDA line drawing algorithm. Program #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> int x1,y1,x2,y2,x3,y3,x4,y4; void dda(int xa,int ya,int xb,int yb) { int dx=xb-xa,dy=yb-ya,steps,k; float xinc,yinc,x=xa,y=ya; if(abs(dx)>abs(dy)) steps=abs(dx); else steps=abs(dy); xinc=dx/(float)steps; yinc=dy/(float)steps; for(k=0;k<steps;k++) { x+=xinc; y+=yinc; putpixel(x,y,1); } } void read() { printf("\nEnter the coordinates\n"); printf("\nEnter the x coordinate:"); scanf("%d",&x1); printf("\nEnter the y coordinate:"); scanf("%d",&y1); printf("\nEnter the coordinates\n"); printf("\nEnter the x coordinate:"); scanf("%d",&x2); x4=x2; printf("\nEnter the y coordinate:"); scanf("%d",&y2); } void main() {

int gd=DETECT,gm; clrscr(); read(); initgraph(&gd,&gm,""); dda(x1,y1,x2,y2); getch(); } Result: Program compiled and output obtained.

RECTANGLE USING DDA ALGORITHM Experiment no:2 Date:13-12-11 Aim: Write a C program to draw a rectangle using DDA line drawing algorithm. Program #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> int x1,y1,x2,y2,x3,y3,x4,y4,l,b; void dda(int xa,int ya,int xb,int yb) { int dx=xb-xa,dy=yb-ya,steps,k; float xinc,yinc,x=xa,y=ya; if(abs(dx)>abs(dy)) steps=abs(dx); else steps=abs(dy); xinc=dx/(float)steps; yinc=dy/(float)steps; for(k=0;k<steps;k++) { x+=xinc; y+=yinc; putpixel(x,y,5); } } void read() { printf("\nEnter the coordinates\n"); printf("\nEnter the x coordinate:"); scanf("%d",&x1); printf("\nEnter the y coordinate:"); scanf("%d",&y1); printf("\nEnter the length"); scanf("%d",&l); printf("\nEnter the breadth:"); scanf("%d",&b); x2=x1+l; y2=y1; x3=x1; y3=y1+b; x4=x2; y4=y3;

} void main() { int gd=DETECT,gm; read(); initgraph(&gd,&gm,""); setbkcolor(0); dda(x1,y1,x2,y2); dda(x1,y1,x3,y3); dda(x3,y3,x4,y4); dda(x2,y2,x4,y4); getch(); } Result: Program compiled and output obtained.

LINE USING BRESENHAMS ALGORITHM Experiment no:3 Date:05-01-12 Aim: Write a C program to draw a line using Bresenhams line drawing algorithm.

Program #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> int x1,y1,x2,y2,x3,y3,x4,y4; void bresenham(int xa,int ya,int xb,int yb) { int dx=xb-xa,dy=yb-ya,p[200]={0},i,j,k,l; float x=xa,y=ya; j=2*dy; k=2*dx; p[0]=j-dx; if(dx>0) { for(i=0;i<dx;i++) { if(p[i]<=0) { x++; putpixel(x,y,1); p[i+1]=p[i]+j; } else { x++; y++; putpixel(x,y,1); p[i+1]=p[i]+j-k; } } } for(i=0;i==dx;i++) { while(y<=yb) { y++;

putpixel(x,y,1); } } } void read() { printf("\nEnter the coordinates\n"); printf("\nEnter the x coordinate:"); scanf("%d",&x1); printf("\nEnter the y coordinate:"); scanf("%d",&y1); printf("\nEnter the coordinates\n"); printf("\nEnter the x coordinate:"); scanf("%d",&x2); printf("\nEnter the y coordinate:"); scanf("%d",&y2); } void main() { int gd=DETECT,gm; clrscr(); read(); initgraph(&gd,&gm,""); bresenham(x1,y1,x2,y2); getch(); }

Result: Program compiled and output obtained.

RECTANGLE USING BRESENHAMS ALGORITHM

Experiment no:4 Date:05-01-12

Aim: Write a C program to draw a rectangle using bresenhams line drawing algorithm. Program #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> int x1,y1,x2,y2,x3,x4,y3,y4,l,b; void bresenham(int xa,int ya,int xb,int yb) { int dx=xb-xa,dy=yb-ya,p[200]={0},i,j,k,l; float x=xa,y=ya; j=2*dy; k=2*dx; p[0]=j-dx; if(dx>0) { for(i=0;i<dx;i++) { if(p[i]<=0) { x++; putpixel(x,y,1); p[i+1]=p[i]+j; } else { x++; y++; putpixel(x,y,1); p[i+1]=p[i]+j-k; } } } for(i=0;i==dx;i++) { while(y<=yb)

{ y++; putpixel(x,y,1); } } if(dx<0) { dx=abs(dx); for(i=0;i<dx;i++) { if(p[i]<=0) { x--; y++; putpixel(x,y,1); p[i+1]=p[i]+j; } else { x--; y++; putpixel(x,y,1); p[i+1]=p[i]+j-k; } } } } void read() { printf("\nEnter the coordinates\n"); printf("\nEnter the x coordinate:"); scanf("%d",&x1); printf("\nEnter the y coordinate:"); scanf("%d",&y1); printf("Enter the length:"); scanf("%d",&l); printf("Enter the breadth:"); scanf("%d",&b); x2=x1+l; y2=y1; x3=x1; y3=y1+b; x4=x2; y4=y3; } void main() {

int gd=DETECT,gm; clrscr(); read(); initgraph(&gd,&gm,""); bresenham(x1,y1,x2,y2); bresenham(x1,y1,x3,y3); bresenham(x3,y3,x4,y4); bresenham(x2,y2,x4,y4); getch(); } Result: Program compiled and output obtained.

CIRCLE USING BRESENHAMS ALGORITHM Experiment no:5 Date:10-01-12 Aim: Write a C program to draw a circle using bresenhams circle drawing algorithm. Program #include<stdio.h> #include<conio.h> #include<graphics.h> int d,a,b,c,q,x,y,r1,i,xa,ya; float p; void plot(int xa,int ya,int x,int y) { putpixel(xa+x,ya+y,10); putpixel(xa-x,ya+y,10); putpixel(xa+x,ya-y,10); putpixel(xa-x,ya-y,10); putpixel(xa+y,ya+x,10); putpixel(xa-y,ya+x,10); putpixel(xa+y,ya-x,10); putpixel(xa-y,ya-x,10); } void bresen(int xa,int ya,int ra) { float x=xa,y=ya,r=ra; x=0; y=r; a=x+1; b=y-1; c=y-0.5; q=x+0.5; d=(a*a)+(b*b)-(r*r); if(y<=0) { exit(1); } while(x<=y) { if(d<0) { p=(a*a)+(c*c)-(r*r); if(p<=0) { x++; d=d+(2*x)+1; }

else { x++; y--; d=d+(2*x)-(2*y)+2; } plot(xa,ya,x,y); } else if(d>0) { p=(q*q)+(b*b)-(r*r); if(p<=0) { x++; y--; d=d+(2*x)-(2*y)+2; } else { y--; d=d-(2*y)+1; } plot(xa,ya,x,y); } else { x++; y--; d=d+(2*x)-(2*y)+2; } plot(xa,ya,x,y); } } void read( { printf(Enter the circle coordinates:); scanf(%d%d,&x1,&y1); printf(Enter the radius:); scanf(%d,&r1); } void main() { int gd=DETECT,gm; clrscr(); read(); initgraph(&gd,&gm,""); bresen(x1,y1,r1); getch(); closegraph();} Result: Program compiled and output obtained.

CIRCLE USING MIDPOINT ALGORITHM Experiment no:6 Date:10-01-12 Aim: Write a C program to draw a circle using midpoint circle drawing algorithm.

Program #include<stdio.h> #include<conio.h> #include<graphics.h> int d,a,b,c,q,x,y,r1,i,xa,ya; float p; void plot(int xa,int ya,int x,int y) { putpixel(xa+x,ya+y,10); putpixel(xa-x,ya+y,10); putpixel(xa+x,ya-y,10); putpixel(xa-x,ya-y,10); putpixel(xa+y,ya+x,10); putpixel(xa-y,ya+x,10); putpixel(xa+y,ya-x,10); putpixel(xa-y,ya-x,10); } void midpoint(int xa,int ya,int ra) { float x=xa,y=ya,r=ra; x=0; y=r; p=1-r; while(x<y) { x++; if(p<0) { p=p+2*x+1; } else { y--; p=p+2*(x-y)+1; } plot(xa,ya,x,y); } } void read(

{ printf(Enter the circle coordinates:); scanf(%d%d,&x1,&y1); printf(Enter the radius:); scanf(%d,&r1); } void main() { int gd=DETECT,gm; clrscr(); read(); initgraph(&gd,&gm,""); midpoint(x1,y1,r1); getch(); closegraph(); } Result: Program compiled and output obtained.

BOUNDARY FILL ALGORITHM Experiment no:7 Date:10-01-12 Aim: Write a C program to implement boundary fill algorithm. Program #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> #include<dos.h> int x1,y1,r; void boundaryfill1(int x,int y,int fill,int boundary) { int c; c=getpixel(x,y); if((c!=boundary)&&(c!=fill)) { putpixel(x,y,10); delay(1); boundaryfill1(x+1,y,fill,boundary); boundaryfill1(x-1,y,fill,boundary); boundaryfill1(x,y+1,fill,boundary); boundaryfill1(x,y-1,fill,boundary); } } void read() { printf("\nEnter the coordinates\n"); printf("\nEnter the x coordinate:"); scanf("%d",&x1); printf("\nEnter the y coordinate:"); scanf("%d",&y1); printf("\nEnter the radius:\n"); scanf("%d",&r); } void main() { int gd=DETECT,gm; clrscr(); read(); initgraph(&gd,&gm,""); setcolor(5);

circle(x1,y1,r); boundaryfill1(x1,y1,10,getpixel(x1+r,y1)); getch(); }

Result: Program compiled and output obtained.

FLOOD FILL ALGORITHM Experiment no:8 Date:17-01-12 Aim: Write a C program to implement floodfill algorithm. Program #include<stdio.h> #include<conio.h> #include<graphics.h> #include<math.h> #include<dos.h> int x1,y1,r; void floodfill1(int x,int y,int fill,int old) { if(getpixel(x,y)==old) { putpixel(x,y,10); delay(1); floodfill1(x+1,y,fill,old); floodfill1(x-1,y,fill,old); floodfill1(x,y+1,fill,old); floodfill1(x,y-1,fill,old); } } void read() { printf("\nEnter the coordinates\n"); printf("\nEnter the x coordinate:"); scanf("%d",&x1); printf("\nEnter the y coordinate:"); scanf("%d",&y1); printf("\nEnter the radius:\n"); scanf("%d",&r); } void main() { int gd=DETECT,gm; clrscr(); read(); initgraph(&gd,&gm,""); setcolor(5); circle(x1,y1,r);

floodfill1(x1,y1,10,getpixel(x1,y1)); getch(); } Result: Program compiled and output obtained.

You might also like