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:

No comments:

Post a Comment