Thursday, June 21, 2018

Implementation of polygon filling algorithm


  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<graphics.h>
  4. #include<stdio.h>
  5. #include<dos.h>
  6. void fill_right(int x,int y); void fill_left(int x,int y); void main()
  7. {

  8. int x,y,a[10][10]; int gd,gm,n,i; detectgraph(&gd,&gm);
  9. initgraph(&gd,&gm,"C:\\TC\\BGI"); cout<<"Enter no of edges of polygon:"; cin>>n;
  10. cout<<"\n Enter the coordinates of polygon"; for(i=0;i<n;i++)
  11. {
  12. cin>>a[i][0]>>a[i][1];
  13. }
  14. a[n][0]=a[0][0];
  15. a[n][1]=a[0][1];
  16. cout<<"\n Enter the seed point"; cin>>x>>y;
  17. cleardevice(); setcolor(WHITE); for(i=0;i<n;i++)
  18. {

  19.  
  20.  
  21. line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
  22. }
  23. fill_right(x,y); fill_left(x-1,y); getch();
  24. }
  25. void fill_right(int x,int y)    //Right fill
  26. {
  27. if((getpixel(x,y) != WHITE) && (getpixel(x,y) != RED))
  28. {
  29. delay(1); putpixel(x,y,RED); fill_right(++x,y); x=x-1; fill_right(x,y+1); fill_right(x,y-1);
  30. }
  31. }
  32. void fill_left(int x,int y) //left fill
  33. {
  34. if((getpixel(x,y) != WHITE) && (getpixel(x,y) != RED))
  35. {
  36. putpixel(x,y,RED); delay(1); fill_left(--x,y); x=x+1; fill_left(x,y-1); fill_left(x,y+1);
  37. }
  38. }

Output:

Implementation of curve drawing using Bezier algorithm


  1. #include<dos.h>
  2. #include<stdio.h>
  3. #include<iostream.h>
  4. #include<graphics.h>
  5. #include<conio.h>
  6. int a[150],b[150],m,n=0,j,d; float bx[5000],by[5000],u; union REGS i,o;
  7. initmouse()
  8. {
  9. i.x.ax=0; int86(0x33,&i,&o); return(o.x.ax);
  10. }
  11. void showmouseptr()
  12. {
  13. i.x.ax=1; int86(0x33,&i,&o);
  14. }
  15. void getmousepos(int *button,int *x,int *y)
  16. {
  17. i.x.ax=3; int86(0x33,&i,&o);
  18. *button=o.x.bx;
  19. *x=o.x.cx;
  20. *y=o.x.dx; i.h.ah=0x02; i.h.dh=0;
  21.  


  22. i.h.dl=0;  int86(0x10,&i,&o);  cout<<"X: "<<*x<<" Y: "<<*y; i.h.ah=0x02; int86(0x10,&i,&o);
  23. }
  24. void hidemouseptr()
  25. {
  26. i.x.ax=2; int86(0x33,&i,&o);
  27. }
  28. int compco()
  29. {
  30. int k,coeff=1;
  31. for(k=d+1;k<=n;k++) coeff=coeff*k; for(k=2;k<=n-d;k++) coeff=coeff/k; return coeff;
  32. }
  33. float blenval()
  34. {
  35. int k;
  36. float blend; blend=compco();
  37. for(k=1;k<=d;k++) blend=blend*u; for(k=1;k<=n-d;k++) blend=blend*(1.0-u); return blend;
  38. }
  39. void bezier()
  40. {
  41. float blend1;
  42.  


  43. bx[j]=0;by[j]=0; for(d=0;d<=n;d++)
  44. {
  45. blend1=blenval(); bx[j]+=a[d]*blend1; by[j]+=b[d]*blend1;
  46. }
  47. }
  48. void curve()
  49. {
  50. for(j=0;j<m;j++) putpixel(bx[j],by[j],GREEN);
  51. }
  52. void drawi()
  53. {
  54. for(j=0;j<=m;j++)
  55. {
  56. u=(float)j/m; bezier();
  57. }
  58. curve();
  59. }
  60. void main()
  61. {
  62. int gd=DETECT,gm; int button,tx,ty; clrscr();  m=4990;
  63. flushall(); initgraph(&gd,&gm,"c:\\tc\\bgi"); cleardevice();
  64.  


  65. showmouseptr(); while(!kbhit())
  66. {
  67. getmousepos(&button,&tx,&ty); if((button & 1) == 1)
  68. {
  69. hidemouseptr(); setcolor(WHITE); a[n]=tx;
  70. b[n]=ty; putpixel(tx,ty,WHITE); circle(tx,ty,2);
  71. n++;
  72. delay(50); showmouseptr();
  73. }
  74. }
  75. n--;
  76. drawi();
  77. getch();
  78. }



Output:

Implementation of ellipse drawing algorithm


  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<graphics.h>
  4. #include<dos.h>

  5. void main()
  6. {
  7. clrscr();
  8. float rx=0,ry=0,x=0,y=0,dx=0,dy=0,d1=0,d2=0; 
  9. float rxSqr=0,rySqr=0,rx2Sqr=0,ry2Sqr=0;
  10. int x1=0,y1=0; int gd,gm;

  11. detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:\\tc\\bgi");

  12. //textbackground(15);
  13. //textcolor(8);
  14. //setbkcolor(15);
  15. //setcolor(8);


  16. cout<<"Enter pixels for centre of an ellipse (x,y): "; cin>>x1>>y1;

  17. cout<<"Enter rx radius: "; cin>>rx;
  18. cout<<"Enter ry radius: "; cin>>ry;
  19.  
  20. line(0,getmaxy()/2,getmaxx(),getmaxy()/2); line(getmaxx()/2,0,getmaxx()/2,getmaxy());

  21. x=0;
  22. y=ry;

  23. rxSqr=rx*rx; rx2Sqr=2*rxSqr; rySqr=ry*ry; ry2Sqr=2*rySqr; dx=ry2Sqr*x; dy=rx2Sqr*y;
  24. d1=rySqr-rxSqr*ry+(.25*rxSqr);
  25. do
  26. {
  27. putpixel(getmaxx()/2+(x1+x),getmaxy()/2-(y1+y),8); 
  28. putpixel(getmaxx()/2+(x1-x),getmaxy()/2-(y1+y),8); 
  29. putpixel(getmaxx()/2+(x1+x),getmaxy()/2-(y1-y),8); 
  30. putpixel(getmaxx()/2+(x1-x),getmaxy()/2-(y1-y),8); 
  31. if(d1<0)
  32. {
  33. x+=1;
  34. y=y; dx+=ry2Sqr; d1+=dx+rySqr;
  35. }
  36. else
  37. {
  38. x+=1;
  39. y-=1;
  40. dx+=ry2Sqr; dy-=rx2Sqr;
  41. d1+=dx-dy+rySqr;
  42. }
  43. delay(30);
  44. }while(dx<dy);
  45. d2=rySqr*((x+(.5))*(x+(.5)))+rxSqr*((y-1)*(y-1))-rxSqr*rySqr; do
  46. {
  47. putpixel(getmaxx()/2+(x1+x),getmaxy()/2-(y1+y),8); 
  48. putpixel(getmaxx()/2+(x1-x),getmaxy()/2-(y1+y),8);
  49.  putpixel(getmaxx()/2+(x1+x),getmaxy()/2-(y1-y),8); 
  50. putpixel(getmaxx()/2+(x1-x),getmaxy()/2-(y1-y),8); 
  51. if(d2>0)
  52. {
  53. x=x; y-=1;
  54. dy-=rx2Sqr; d2-=dy+rxSqr;
  55. }
  56. else
  57. {
  58. x+=1; y-=1;
  59. dy-=rx2Sqr; dx+=ry2Sqr; d2+=dx-dy+rxSqr;
  60. }
  61. delay(10);
  62. }while(y>0);
  63. getch();
  64. }

Output:

Implementation of circle drawing algorithm


  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<graphics.h>
  4. #include<dos.h> 
  5. void main()
  6. {
  7. int gm,gd=DETECT; initgraph(&gd,&gm,"c:\\tc\\bgi"); 
  8. int x1,y1;
  9. int x=0,y=0,r=0; float d;
  10. //textbackground(15);
  11. //textcolor(8);
  12. //setbkcolor(15);
  13. //setcolor(8);
  14. cout<<"Enter pixels for centre of a circle (x,y): "; cin>>x1>>y1;
  15. cout<<"\nEnter radius of circle: "; cin>>r;
  16. x=0;
  17. y=r; d=1.25-r;
  18. line(0,getmaxy()/2,getmaxx(),getmaxy()/2); line(getmaxx()/2,0,getmaxx()/2,getmaxy()); 
  19. do
  20. {
  21. putpixel(getmaxx()/2+(x1+x),getmaxy()/2-(y1+y),8); 
  22. putpixel(getmaxx()/2+(x1-x),getmaxy()/2-(y1+y),8);
  23. putpixel(getmaxx()/2+(x1-x),getmaxy()/2-(y1-y),8); 

  24. putpixel(getmaxx()/2+(x1+x),getmaxy()/2-(y1-y),8); 

  25. putpixel(getmaxx()/2+(x1+y),getmaxy()/2-(y1+x),8); 

  26. putpixel(getmaxx()/2+(x1+y),getmaxy()/2-(y1-x),8); 

  27. putpixel(getmaxx()/2+(x1-y),getmaxy()/2-(y1-x),8); 

  28. putpixel(getmaxx()/2+(x1-y),getmaxy()/2-(y1+x),8); 

  29. if(d<0)
  30. {
  31. x+=1;
  32. y=y; 
  33. d+=2*x+1;}
  34. else
  35. {
  36.              x+=1; 
  37.              y-=1;
  38. d+=2*(x-y)+1;
  39. }
  40. delay(50);
  41. }while(x<y);
  42. getch();
  43. }

Bresenham's algorithm



  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<graphics.h>
  4. #include<math.h>
  5. #include<dos.h>

  6. void main()
  7. {
  8. clrscr();
  9. int gm,gd;
  10. int x1,y1,x2,y2,i; 
  11. float x,y,dx,dy,len,e;
  12. detectgraph(&gd,&gm); 
  13. initgraph(&gd,&gm,"c:\\tc\\bgi"); 
  14. //textbackground(15);
  15. //textcolor(8);
  16. //setbkcolor(15);
  17.           //setcolor(8);
  18.                        
  19.                        cout<<"Enter starting pixels for point (x1,y1): "; cin>>x1>>y1;
  20. cout<<"\nEnter ending pixels for point (x2,y2): "; cin>>x2>>y2;
  21. line(0,getmaxy()/2,getmaxx(),getmaxy()/2); line(getmaxx()/2,0,getmaxx()/2,getmaxy());dx=abs(x2-x1);
  22. dy=abs(y2-y1);
  23. x=x1; 
  24. y=y1; 
  25. e=2*dy-dx;
  26.                     for(i=1;i<=dx;i++)
  27. {
  28. putpixel(getmaxx()/2+x,getmaxy()/2-y,8); while(e>=0)
  29. {
  30. y++;
  31. e-=2*dx; 
  32. delay(20)
  33. }
  34. x++;
  35. e+=2*dy;
  36. }
  37. getch();
  38. }



DDA algorithm (Implementation of line drawing algorithm)


  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<graphics.h>
  4. #include<math.h>
  5. #include<dos.h>

  6. void main()
  7. {
  8. clrscr(); 
  9. int x1,y1,x2,y2,i=1; float x,y,dx,dy,len;

  10. detectgraph(&gd,&gm); initgraph(&gd,&gm,"c:\\tc\\bgi");

  11. textbackground(15); textcolor(15); setbkcolor(15); setcolor(8);

  12. cout<<"Enter starting pixels for point (x1,y1):"; cin>>x1>>y1;
  13. cout<<"\nEnter ending pixels for point (x2,y2):"; cin>>x2>>y2; line(0,getmaxy()/2,getmaxx(),getmaxy()/2); line(getmaxx()/2,0,getmaxx()/2,getmaxy());

  14. dx=abs(x2-x1); 
  15. dy=abs(y2-y1);

  16.          if(dy>dx)len=dy;
  17.           else len=dx;
  18.            dx=((x2-x1)/len);
  19. dy=((y2-y1)/len);
  20. x=x1+0.5*dx; y=y1+0.5*dy; while(i<=len)
  21. { 
  22. putpixel(getmaxx()/2+x,getmaxy()/2-y,8);
  23. }
  24. x+=dx; y+=dy; i++;

  25. delay(20);
  26. }
  27. getch();


Introduction to graphics libraries in c++

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<graphics.h> void main()
{
int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc\\bgi");

//setbkcolor(15);
//setcolor(1); setfillstyle(HATCH_FILL,5);

circle(100,100,50); outtextxy(75,170,"circle");


line(100,250,540,250);
outtextxy(300,260,"line");

ellipse(500,100,0,360,100,50);
outtextxy(480,170,"ellipse");

sector(150,400,30,300,100,50);
outtextxy(120,460,"sector");

int poly[]={350,450,350,410,430,400,350,350,300,430,350,450};

fillpoly(6,poly); outtextxy(340,460,"polygon");

rectangle(200,50,350,150); outtextxy(530,460,"arc");

pieslice(120,320,45,120,40);
outtextxy(110,325,"pieslice");

bar(320,280,350,315);
outtextxy(320,325,"bar");

bar3d(480,280,520,320,10,15);
outtextxy(490,325,"bar3d");

getch(); closegraph();
}