/1. BRESENHAMS LINE ALGORITHM// Problem: Write a program to implement Bresenham’s Line Algorithm. Algorithm:
1: Start
2. print ("enter the value for x1,y1,x2,y2 :") 3. Input x1,y1,x2,y2
4. dx=abs(x2-x1); 5. dy=abs(y2-y1); 6. x=x1, y=y1; 7. e=2*dy-dx;
8 repeat \until (i<=dx) 1. putpixel(x,y,15); 2. while(e>=0) do a. y=y+1; b. e=e-2*dx; c. end while 3. x=x+1; 4. e=e+2*dy; 5. i=i+1; 6. End while 9. Stop.
Program: #include<stdio.h> #include<graphics.h> #include<math.h> void main() { float x,y,x1,y1,x2,y2,dx,dy,e; int i,gd,gm; clrscr();
printf("enter the value for x1 :"); scanf("%f",&x1);
printf("enter the value for y1:"); scanf("%f",&y1);
printf("enter the value for x2:"); scanf("%f",&x2);
printf("enter the value for y2:"); scanf("%f",&y2); detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:\\tc\\bgi"); dx=abs(x2-x1); `dy=abs(y2-y1); x=x1; y=y1; e=2*dy-dx; i=1; do { putpixel(x,y,15); while(e>=0) { y=y+1; e=e-2*dx; } x=x+1; e=e+2*dy; i=i+1; } while(i<=dx); getch(); closegraph(); } Output:
2. DDA CIRCLE DIAGRAM Problem: write a program to implement DDA Circle algorithm. Algorithm:
1. Start
2. print("enter the radius of a circle") 3. input r
4. x1=r*cos(0) 5. y1=r*sin(0)
6. startx=x1, starty=y1 7. i=0;
8. repeat until (val<r) a. val=pow(2,i); b. i++;
9. epsilon=1/pow(2,i-1)
10. repeat until y1-starty)<epsilon||(startx-x1)>epsilon) a. x2=x1+y1*epsilon; b. y2=y1-epsilon*x2; c. putpixel(200+x2,200+y2,15); d. x1=x2; e. y1=y2; f. delay(50); 11. Stop
Program: include<stdio.h> include<graphics.h> include<math.h> void main() { float x1,y1,x2,y2,startx,starty,epsilon; int gd,gm,i,val; int r; clrscr();
printf("enter the radius of a circle"); scanf("%d",&r); detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:\\tc\\bgi"); x1=r*cos(0); y1=r*sin(0); startx=x1; starty=y1; i=0; do { val=pow(2,i); i++; }while(val<r); epsilon=1/pow(2,i-1); do { x2=x1+y1*epsilon; y2=y1-epsilon*x2; putpixel(200+x2,200+y2,15); x1=x2; y1=y2; delay(50); } while((y1-starty)<epsilon||(startx-x1)>epsilon); getch(); closegraph(); } Output:
3. BRESENHAM CIRCLE DIAGRAM*/ Problem: write a program to implement Bresenham’s circle algorithm. Algorithm:
1. Start
2. print("enter the radius") 3. input r
4. x=0, y=r 5. d=3-2*r
6. repeat until x<y)
a. putpixel(200+x,200+y,15); b. putpixel(200+y,200+x,15); c. putpixel(200+y,200-x,15); d. putpixel(200+x,200-y,15); e. putpixel(200-x,200-y,15); f. putpixel(200-y,200-x,15); g. putpixel(200-y,200+x,15); h. putpixel(200-x,200+y,15); i. if(d<=0) then j. d=d+4*x+6;} k. Otherwise l. d=d+4*(x-y)+10; m. y=y-1; n. x=x+1; o. delay(50); 7. Stop
program: include<stdio.h> include<graphics.h> include<math.h> void main() { float d; int gd,gm,x,y; int r; clrscr();
printf("enter the radius"); scanf("%d",&r); detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:\\tc\\bgi"); x=0; y=r; d=3-2*r; do { putpixel(200+x,200+y,15); putpixel(200+y,200+x,15); putpixel(200+y,200-x,15); putpixel(200+x,200-y,15); putpixel(200-x,200-y,15); putpixel(200-y,200-x,15); putpixel(200-y,200+x,15); putpixel(200-x,200+y,15); if(d<=0) { d=d+4*x+6;} else {d=d+4*(x-y)+10; y=y-1; } x=x+1; delay(50); }while(x<y); getch(); closegraph(); } Output:
4.MIDPOINT CIRCLE DRAWING ALGORITHM Problem: write a program to implement Midpoint Circle drawing algorithm Algorithm:
1. Start
2. print("enter the radius") 3. input r
4. x=0,y=r 5. p=1.25-r
6. repeat until (x<y)
a. putpixel(200+x,200+y,15) b. putpixel(200+y,200+x,15) c. putpixel(200+y,200-x,15) d. putpixel(200+x,200-y,15) e. putpixel(200-x,200-y,15) f. putpixel(200-y,200-x,15) g. putpixel(200-y,200+x,15) h. putpixel(200-x,200+y,15) i. if(p<0) then j. x=x+1; k. y=y; l. p=p+2*x+2; m. otherwise n. x=x+1; o. y=y-1; p. p=p+2*(x-y)+1; q. delay(50);} 7. stop
Program include<stdio.h> include<graphics.h> include<math.h> void main() { float p; int i,gd,gm,x,y; int r; clrscr();
printf("enter the radius"); scanf("%d",&r); detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:\\tc\\bgi"); x=0; y=r; p=1.25-r; do { putpixel(200+x,200+y,15); putpixel(200+y,200+x,15); putpixel(200+y,200-x,15); putpixel(200+x,200-y,15); putpixel(200-x,200-y,15); putpixel(200-y,200-x,15); putpixel(200-y,200+x,15); putpixel(200-x,200+y,15); if(p<0) { x=x+1; y=y; p=p+2*x+2;} else {x=x+1; y=y-1; p=p+2*(x-y)+1;} delay(50);} while(x<y); getch(); closegraph(); } Output:
5.Midpoint Ellipse Drawing Algorithm*
Program: write a program to implement Midpoint Ellipse drawing algrothm Algorithm:
Program: include<stdio.h> include<graphics.h> include<math.h> void main() { long d1,d2; int i,gd,gm,x,y; long rx,ry,rxsq,rysq,tworxsq,tworysq,dx,dy; printf("enter the x radius of the ellipse:"); scanf("%ld",&rx);
printf("enter the y radius of the ellipse:"); scanf("%ld",&ry); detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:\\tc\\bgi"); rxsq=rx*rx; rysq=ry*ry; tworxsq=2*rxsq; tworysq=2*rysq; x=0; y=ry; d1=rysq-rxsq*ry+(0.25*rxsq); dx=tworysq*x; dy=tworxsq*y; do{ 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(d1<0) {x=x+1; y=y; dx=dx+tworysq; d1=d1+dx+rysq;} else { x=x+1; y=y-1; dx=dx+tworysq; dy=dy-tworxsq; d1=d1+dx-dy+rysq;} delay(1000); } while(dx<dy); d2=rysq*(x+0.5)*(x+0.5)+rxsq*(y-1)*(y-1)-rxsq*rysq; do { 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(d2>0) { x=x; y=y-1; dy=dy-tworxsq; d2=d2-dy+rxsq; } else { x=x+1; y=y-1; dy=dy-tworxsq; dx=dx+tworysq; d2=d2+dx-dy+rxsq; } }while(y>0); getch(); closegraph(); } Output:
6. Generation of text "A" Problem: write a program to generate a text “A”
Algorithm: 1. Start
2. Declare array a[13][9]={ {0,0,0,0,1,0,0,0,0}, {0,0,0,1,0,1,0,0,0}, {0,0,1,0,0,0,1,0,0}, {0,1,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,1,0}, {0,1,1,1,1,1,1,1,0}, {0,1,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,1,0},} 3. Repeat for i=0 to 13 4. Repeat for j=0 to 9 a. putpixel(200+j,200+i,15*a[i][j]); b. j=j-1 c. i=i-1 5. end for j 6. end for i 7. stop
Program: include<stdio.h> include<graphics.h> oid main() int gd,gm,i,j; int a[13][9]={ {0,0,0,0,1,0,0,0,0}, {0,0,0,1,0,1,0,0,0}, {0,0,1,0,0,0,1,0,0}, {0,1,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,1,0}, {0,1,1,1,1,1,1,1,0}, {0,1,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,1,0}, {0,1,0,0,0,0,0,1,0},}; detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:\\tc\\bgi"); for(i=0;i<13;i++) { for(j=0;j<9;j++) {putpixel(200+j,200+i,15*a[i][j]); } } getch(); closegraph(); } Output:
7. Boundary fill algorithm Program: write a program to implement Boundary fill algorithm Algorithm:
Program: include<stdio.h> include<graphics.h> void main() { int gd,gm; detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:\\tc\\bgi"); rectangle(50,50,100,100); flood(55,55,4,15); getch(); closegraph(); } flood(seed_x,seed_y,foreground_col,background_col) {
if(getpixel(seed_x,seed_y) != background_col && getpixel(seed_x,seed_y) round_col) { putpixel(seed_x,seed_y,foreground_col);delay(100); seed_x+1,seed_y,foreground_col,background_col);delay(100); seed_x-1,seed_y,foreground_col,background_col);delay(100); seed_x,seed_y+1,foreground_col,background_col);delay(100); seed_x,seed_y-1,foreground_col,background_col);delay(100); seed_x+1,seed_y+1,foreground_col,background_col);delay(100); seed_x-1,seed_y-1,foreground_col,background_col);delay(100); seed_x+1,seed_y-1,foreground_col,background_col);delay(100); seed_x-1,seed_y+1,foreground_col,background_col); } } Output:
8. DDA LINE Program: write a program to implement a DDA Line Algorithm:
1. Start
2. print ("enter the value for x1,y1,x2,y2 :") 3. Input x1,y1,x2,y2 4. dx=abs(x2-x1) 5. dy=abs(y2-y1) 6. if(dx>=dy) then a. length=dx b. otherwise c. length=dy 7. dx= (x2-x1)/length 8. dy=(y2-y1)/length 9. x=x1+0.5 10. y=y1+0.5 11. i=1; 12. while(i<=length) do a. putpixel(x,y,15) b. x=x+dx c. y=y+dy d. i=i+1 e. delay(50); 13. stop
Program include<stdio.h> include<graphics.h> include<math.h> void main() { float x,y,x1,x2,y1,y2,dx,dy,length; int i,gd,gm; clrscr();
printf("enter the values for x1"); scanf("%f",&x1);
printf("enter the value for y1"); scanf("%f",&y1);
printf("enter the value for x2"); scanf("%f",&x2);
printf("enter the value for y2"); scanf("%d",&y2); detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:\\tc\\bgi"); dx=abs(x2-x1); dy=abs(y2-y1); if(dx>=dy) { length=dx; } else { length=dy; } dx= (x2-x1)/length; dy=(y2-y1)/length; x=x1+0.5; y=y1+0.5; i=1; while(i<=length) { putpixel(x,y,15); x=x+dx; y=y+dy; i=i+1; delay(50); } getch(); closegraph(); } Output:
9. COHEN-SUTHERLAND LINE CLIPPING
Program: write a program to implement COHEN-SUTHERLAND LINE CLIPPING. Algorithm:
Program: #include<stdio.h> #include<stdlib.h> #include<conio.h> #include<graphics.h> #include<math.h> void clipline(int,int,int,int);
long int xstart,ystart,xend,yend,a1,a2,b1,b2; void main()
{
int gm,gd=DETECT,temp,i,j,k,n,m; clrscr();
printf("Enter left,top,right,bottom values of the window:");
scanf("%d%d%d%d",&xstart,&ystart,&xend,¥d); printf("Enter co-ordintes for the line :");
scanf("%d%d%d%d",&a1,&b1,&a2,&b2); initgraph(&gd,&gm,"c:\\tc\\bgi"); if(a1>a2) { temp=a1; a1=a2; a2=temp; temp=b1; b1=b2; b2=temp; } rectangle(xstart,ystart,xend,yend); line(a1,b1,a2,b2); getch(); cleardevice(); clipline(a1,b1,a2,b2); getch(); closegraph(); }
void clipline(int x1,int y1,int x2,int y2) {
int temp;
if((x1>xend && x2>xend) || (y1<ystart && y2<ystart) ||
(x1<xstart && x2<xstart) || (y1>yend && y2>yend)) {
x1=x2=y1=y2=0;
rectangle(xstart,ystart,xend,yend); exit(0);
if(x1>xend) { x1=x2=y1=y2=0; } if(x1<xstart) { y1=y1+(long)(xstart-x1)+(y2-y1)/(x2-x1); x1=xstart; } if(y1<ystart) { x1=(long)(ystart-y1)*(x2-x1)/(y2-y1)+x1; y1=ystart; } if(y1>yend) { x1=(long)(yend-y1)*(x2-x1)/(y2-y1)+x1; y1=yend; } if(x2<0) { x1=x2=y1=y2=0; } if(x2>xend) { y2=y1+(long)(xend-x1)*(y2-y1)/(x2-x1); x2=xend; } if(y2<ystart) { x2=(long)(ystart-y1)*(x2-x1)/(y2-y1)+x1; y2=ystart; } if(y2>yend) { x2=(long)(yend-y1)*(x2-x1)/(y2-y1)+x1; y2=yend; } rectangle(xstart,ystart,xend,yend); line(x1,y1,x2,y2); } Output:
/10. Line width
Problem: write a program to implement Line width . Algorithm:
1. Start
2. print("\n enter the end points and width of a line...:") 3. Input xa, ya,xb, yb, w
4. m= (yb-ya)/ (xb-xa); 5. if(w-2) then a. line(xa, ya ,xb, yb); b. otherwise c. w=w/2; 6. if(m<1) then
a. repeat for(i=0; i<=w ;i++) b. line(xa, ya+i, xb+i, yb); c. if(w>1) then
1. if(m<1)
2. Repeat for(i=0; i<=w; i++) line (xa, ya-1, xb, yb-i); 3. otherwise
4. repeat for(i=0; i<=w; i++) 5. line(xa-i, ya, xb-i, yb); 7. Stop
Program: #include<stdio.h> #include<conio.h> #include<graphics.h> void main() { float xa,ya,xb,yb,w,m; int gd=DETECT,gm,i; initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\n enter the end points and width of a line...:"); scanf("%f%f%f%f%f", &xa, &ya, &xb, &yb, &w); m= (yb-ya)/ (xb-xa); if(w-2) line(xa, ya ,xb, yb); else w=w/2; if(m<1)
for(i=0; i<=w ;i++) line(xa, ya+i, xb+i, yb); if(w>1)
{ if(m<1) {
for(i=0; i<=w; i++) line (xa, ya-1, xb, yb-i); }
else {
for(i=0; i<=w; i++) line(xa-i, ya, xb-i, yb); } } getch(); closegraph(); } Output: