Advertisement
d1i2p3a4k5

cg

Apr 24th, 2015
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.56 KB | None | 0 0
  1. //Scan Line Polygon Fill Algorithm
  2.  
  3. #include<iostream.h>
  4. #include<graphics.h>
  5. #include<conio.h>
  6. #include<stdio.h>
  7. #include<dos.h>
  8. struct data
  9. {
  10.  int x;
  11.  int y;
  12. };
  13.  
  14. void main()
  15. {
  16.   int side,i,j,gm,gd=DETECT,ctr=0;
  17.   int maxy,maxx,miny,minx;
  18.   data arr[20];
  19.   initgraph(&gd,&gm,"c:\\tc\\bgi");
  20.  
  21.   cleardevice();
  22.   printf("\n\nEnter the no of sides of polygon:");
  23.   scanf("%d",&side);
  24.   printf("\nEnter the x & y co-ordinate");
  25.   for(i=0;i<side;i++)
  26.    {
  27.     printf("\n\t x[%d] : ",i+1);
  28.     scanf("%d",&arr[i].x);
  29.     printf("\t y[%d] : ",i+1);
  30.     scanf("%d",&arr[i].y);
  31.    }
  32.   arr[i].x=arr[0].x;
  33.   arr[i].y=arr[0].y;
  34.   cleardevice();
  35.  
  36.   setcolor(RED);       //draw poly
  37.   for(i=0;i<side;i++)
  38.    {
  39.      line(arr[i].x,arr[i].y,arr[i+1].x,arr[i+1].y);
  40.    }
  41.   //to search min & max y coordinate  and max x coordinate
  42.  
  43.   miny=arr[0].y;
  44.   maxy=arr[0].y;
  45.   maxx=arr[0].x;
  46.   minx=arr[0].x;
  47.   setcolor(YELLOW);
  48.  
  49.   for(i=0;i<side;i++)
  50.    {
  51.      if(miny>arr[i].y)
  52.       miny=arr[i].y;
  53.      if(maxy<arr[i].y)
  54.       maxy=arr[i].y;
  55.      if(maxx<arr[i].x)
  56.       maxx=arr[i].x;
  57.      if(minx>arr[i].x)
  58.       minx=arr[i].x;
  59.    }
  60.  
  61.   for(j=miny+1;j<maxy;j++)     // fill poly
  62.     {
  63.       ctr=0;
  64.       delay(20);
  65.       for(i=minx-1;i<=maxx;i++)
  66.     {
  67.       if(getpixel(i,j)!=0)
  68.          {
  69.            arr[ctr].x=i;
  70.            arr[ctr].y=j;
  71.            ctr++;
  72.          }
  73.  
  74.       if(ctr==2)
  75.          {
  76.            delay(20);
  77.            line(arr[0].x+1,arr[0].y,arr[1].x-1,arr[1].y);
  78.            ctr=0;
  79.  
  80.          }
  81.     }
  82.     }
  83.  getch();
  84. }
  85.  
  86.  
  87. //midpoint of a circle
  88.  
  89. #include<graphics.h>
  90. #include<stdio.h>
  91. #include<conio.h>
  92. #include<dos.h>
  93. #include<math.h>
  94.  
  95. void main()
  96. {
  97.     int gd=DETECT,gm,xc,yc,x,y,r,pk;
  98.     initgraph(&gd,&gm,"c:\\tc\\bgi");
  99.     printf("Enter co-ordinates of centre: ");
  100.     scanf("%d%d",&xc,&yc);
  101.     printf("Enter radius: ");
  102.     scanf("%d",&r);
  103.     x=0;
  104.     y=r;
  105.     putpixel(xc+x,yc+y,WHITE);
  106.     pk=1-r;
  107.     while(x<y)
  108.     {
  109.         if(pk<0)
  110.         {
  111.             x=x+1;
  112.             pk=pk+(2*x)+1;
  113.         }
  114.         else
  115.         {
  116.             x=x+1;
  117.             y=y-1;
  118.             pk=pk+(2*x)-(2*y)+1;
  119.         }
  120.         putpixel(xc+x,yc+y,WHITE);
  121.         putpixel(xc+y,yc+x,WHITE);
  122.         putpixel(xc-y,yc+x,WHITE);
  123.         putpixel(xc+x,yc-y,WHITE);
  124.         putpixel(xc-x,yc-y,WHITE);
  125.         putpixel(xc-y,yc-x,WHITE);
  126.         putpixel(xc+y,yc-x,WHITE);
  127.         putpixel(xc-x,yc+y,WHITE);
  128.     }
  129.     getch();
  130.     closegraph();
  131. }
  132.  
  133. //bressenham's line generation algorithm
  134.  
  135. #include<graphics.h>
  136. #include<stdio.h>
  137. #include<conio.h>
  138. #include<dos.h>
  139. #include<math.h>
  140.  
  141. void main()
  142. {
  143.     int gd=DETECT,gm,x1,x2,y1,y2,dx,dy,p0,pk,x,y;
  144.     float xinc,yinc;
  145.     initgraph(&gd,&gm,"c:\\tc\\bgi");
  146.     printf("Enter end point co-ordinates of line: ");
  147.     scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
  148.     dx=x2-x1;
  149.     dy=y2-y1;
  150.     x=x1;
  151.     y=y1;
  152.     putpixel(x,y,WHITE);
  153.     if(abs(dx)>=abs(dy))
  154.     {
  155.     pk=2*abs(dy)-abs(dx);
  156.     while(x!=x2||y!=y2)
  157.     {
  158.         if (pk<0)
  159.         {
  160.             xinc=dx/abs(dx);
  161.             yinc=0;
  162.             pk=pk+2*abs(dy);
  163.         }
  164.         else
  165.         {
  166.             xinc=dx/abs(dx);
  167.             yinc=dy/abs(dy);
  168.             pk=pk+(2*abs(dy))-(2*abs(dx));
  169.         }
  170.         x=x+xinc;
  171.         y=y+yinc;
  172.         putpixel(x,y,WHITE);
  173.     }
  174.     }
  175.     else
  176.     {
  177.     pk=2*abs(dx)-abs(dy);
  178.     while(x!=x2||y!=y2)
  179.     {
  180.         if (pk<0)
  181.         {
  182.             xinc=0;
  183.             yinc=dy/abs(dy);
  184.             pk=pk+2*abs(dx);
  185.         }
  186.         else
  187.         {
  188.             xinc=dx/abs(dx);
  189.             yinc=dy/abs(dy);
  190.             pk=pk+(2*abs(dx))-(2*abs(dy));
  191.         }
  192.         x=x+xinc;
  193.         y=y+yinc;
  194.         putpixel(x,y,WHITE);
  195.     }
  196.     }
  197.     getch();
  198.     closegraph();
  199. }
  200.  
  201. //dda line drawing algorithm
  202.  
  203. #include<graphics.h>
  204. #include<stdio.h>
  205. #include<conio.h>
  206. #include<dos.h>
  207. #include<math.h>
  208.  
  209. void main()
  210. {
  211.     int gd=DETECT,gm,x1,y1,x2,y2,step,dx,dy,x,y;
  212.     float xinc,yinc;
  213.     initgraph(&gd,&gm,"C:\\TC\\BGI");
  214.     printf("Enter end pt co-ordinates of line as x1 y1 x2 y2: ");
  215.     scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
  216.     dx=x2-x1;
  217.     dy=y2-y1;
  218.     if(abs(dx)>=abs(dy))
  219.         step=abs(dx);
  220.     else
  221.         step=abs(dy);
  222.     xinc=dx/step;
  223.     yinc=dy/step;
  224.     putpixel(x1,y1,WHITE);
  225.     x=x1;
  226.     y=y1;
  227.     while(x!=x2||y!=y2)
  228.     {
  229.         x=x+xinc;
  230.         y=y+yinc;
  231.         putpixel(x,y,WHITE);
  232.     }
  233.     getch();
  234.     closegraph();
  235. }
  236. //2d
  237.  
  238. #include<stdio.h>
  239.  
  240. #include<graphics.h>
  241.  
  242. #include<conio.h>
  243.  
  244. #include<math.h>
  245.  
  246.  
  247.  
  248. int x[20],y[20],dx[20],dy[20],ch,n,i,sx,sy,tx,ty;
  249.  
  250. float rad,angle;
  251.  
  252.  
  253.  
  254. void draw()
  255.  
  256. {
  257.  
  258.     for(i=0;i<n-1;i++)
  259.  
  260.         line(x[i],y[i],x[i+1],y[i+1]);
  261.  
  262.     line(x[i],y[i],x[0],y[0]);
  263.  
  264. }
  265.  
  266.  
  267.  
  268.  
  269.  
  270. void accept()
  271.  
  272. {
  273.  
  274.     printf("Enter no. of vertices: \n");
  275.  
  276.     scanf("%d",&n);
  277.  
  278.     for(i=0;i<n;i++)
  279.  
  280.     {
  281.  
  282.         printf("Enter coordinates of vertice %d ",i+1);
  283.  
  284.         scanf("%d %d",&x[i],&y[i]);
  285.  
  286.     }
  287.  
  288.     cleardevice();
  289.  
  290.     draw();
  291.  
  292. }
  293.  
  294.  
  295.  
  296. void scaling()
  297.  
  298. {
  299.  
  300.     printf("Enter scaling factor Sx and Sy: \n");
  301.  
  302.     scanf("%d %d",&sx,&sy);
  303.  
  304.     for(i=0;i<n;i++)
  305.  
  306.     {
  307.  
  308.         x[i]*=sx;
  309.  
  310.         y[i]*=sy;
  311.  
  312.     }
  313.  
  314.     cleardevice();
  315.  
  316.     draw();
  317.  
  318.     }
  319.  
  320.  
  321.  
  322. void rotation()
  323.  
  324. {
  325.  
  326.     printf("Enter rotation angle\n");
  327.  
  328.     scanf("%d",&angle);
  329.  
  330.     rad=3.14/angle*180;
  331.  
  332.     for(i=0;i<n;i++)
  333.  
  334.     {
  335.  
  336.         x[i]=dx[i];
  337.  
  338.         y[i]=dy[i];
  339.  
  340.     }
  341.  
  342.     for(i=1;i<n;i++)
  343.  
  344.     {
  345.  
  346.         x[i]=(int)(dx[i]*cos(rad)-dy[i]*sin(rad));
  347.  
  348.  
  349.  
  350.         y[i]=(int)(dx[i]*sin(rad)+dy[i]*cos(rad));
  351.  
  352.     }
  353.  
  354.     cleardevice();
  355.  
  356.     draw();
  357.  
  358. }
  359.  
  360.  
  361.  
  362. void translation()
  363.  
  364. {
  365.  
  366.     printf("Enter Tx and Ty factor\n");
  367.  
  368.     scanf("%d %d",&tx,ty);
  369.  
  370.     for(i=0;i<n;i++)
  371.  
  372.     {
  373.  
  374.         x[i]+=tx;
  375.  
  376.         y[i]+=ty;
  377.  
  378.     }
  379.  
  380.     cleardevice();
  381.  
  382.     draw();
  383.  
  384. }
  385.  
  386.  
  387.  
  388. void main()
  389.  
  390. {
  391.  
  392.     int gd,gm;
  393.  
  394.     detectgraph(&gd,&gm);
  395.  
  396.     initgraph(&gd,&gm,"C:\\TC\\BGI");
  397.  
  398.     while(1)
  399.  
  400.     {
  401.  
  402.         printf("Enter choice:\n1.Accept\n2.Scaling\n3.Rotation\n4.Translation\n5.Exit\n");
  403.  
  404.         scanf("%d",&ch);
  405.  
  406.         if(ch==5)
  407.  
  408.             break;
  409.  
  410.         switch(ch)
  411.  
  412.         {
  413.  
  414.             case 1: accept();
  415.  
  416.                 break;
  417.  
  418.             case 2: scaling();
  419.  
  420.                 break;
  421.  
  422.             case 3: rotation();
  423.  
  424.                 break;
  425.  
  426.             case 4: translation();
  427.  
  428.                 break;
  429.  
  430.             default:printf("Invalid input\n");
  431.  
  432.                 break;
  433.  
  434.         }
  435.  
  436.     }
  437.  
  438. }
  439.  
  440. //bezier
  441.  
  442. #include<stdio.h>
  443.  
  444. #include<conio.h>
  445.  
  446. #include<graphics.h>
  447.  
  448.  
  449.  
  450. int i;
  451.  
  452. float xxx[4][2],temp1,temp2;
  453.  
  454.  
  455.  
  456. void line1(float xb,float yb)
  457.  
  458. {
  459.  
  460.     line(xxx[0][0],xxx[0][1],xb,yb);
  461.  
  462.     xxx[0][0]=xb;
  463.  
  464.     xxx[0][1]=yb;
  465.  
  466. }
  467.  
  468.  
  469.  
  470. void bezier(float x2,float y2,float x3,float y3,float x4,float y4,int n)
  471.  
  472. {
  473.  
  474.     float x12,y12,x23,y23,x34,y34;
  475.  
  476.     float x123,y123,x234,y234;
  477.  
  478.     float x1234,y1234;
  479.  
  480.     if(n==0)
  481.  
  482.     {
  483.  
  484.         line1(x2,y2);
  485.  
  486.         line1(x3,y3);
  487.  
  488.         line1(x4,y4);
  489.  
  490.     }
  491.  
  492.     else
  493.  
  494.     {
  495.  
  496.         x12=(xxx[0][0]+x2)/2;
  497.  
  498.         y12=(xxx[0][1]+y2)/2;
  499.  
  500.         x23=(x2+x3)/2;
  501.  
  502.         y23=(y2+y3)/2;
  503.  
  504.         x34=(x3+x4)/2;
  505.  
  506.         y34=(y3+y4)/2;
  507.  
  508.         x123=(x12+x23)/2;
  509.  
  510.         y123=(y12+y23)/2;
  511.  
  512.         x234=(x23+x34)/2;
  513.  
  514.         y234=(y23+y34)/2;
  515.  
  516.         x1234=(x123+x234)/2;
  517.  
  518.         y1234=(y123+y234)/2;
  519.  
  520.         n=n-1;
  521.  
  522.         bezier(x12,y12,x123,y123,x1234,y1234,n);
  523.  
  524.         bezier(x234,y234,x34,y34,x4,y4,n);
  525.  
  526.     }
  527.  
  528. }
  529.  
  530.  
  531.  
  532. void main()
  533.  
  534. {
  535.  
  536.     int gd,gm;
  537.  
  538.     detectgraph(&gd,&gm);
  539.  
  540.     initgraph(&gd,&gm,"C:\\TC\\BGI");
  541.  
  542.     for(i=0;i<4;i++)
  543.  
  544.     {
  545.  
  546.         printf("Enter coordinates of point %d ",i+1);
  547.  
  548.         scanf("%f %f",&temp1,&temp2);
  549.  
  550.         xxx[i][0]=temp1;
  551.  
  552.         xxx[i][1]=temp2;
  553.  
  554.     }
  555.  
  556.     bezier(xxx[1][0],xxx[1][1],xxx[2][0],xxx[2][1],xxx[3][0],xxx[3][1],8);
  557.  
  558.     getch();
  559.  
  560.     closegraph();
  561.  
  562. }
  563.  
  564. //boundary fill
  565.  
  566. #include<stdio.h>
  567.  
  568. #include<conio.h>
  569.  
  570. #include<graphics.h>
  571.  
  572.  
  573.  
  574. void b_fill(int x,int y,int f_col,int b_col)
  575.  
  576. {
  577.  
  578.     if(getpixel(x,y)!=f_col && getpixel(x,y)!=b_col)
  579.  
  580.     {
  581.  
  582.         putpixel(x,y,f_col);
  583.  
  584.         b_fill(x+1,y,f_col,b_col);
  585.  
  586.         b_fill(x-1,y,f_col,b_col);
  587.  
  588.         b_fill(x,y+1,f_col,b_col);
  589.  
  590.         b_fill(x,y-1,f_col,b_col);
  591.  
  592.     }
  593.  
  594. }
  595.  
  596.  
  597.  
  598. void main()
  599.  
  600. {
  601.  
  602.     int gd,gm;
  603.  
  604.     int a[20][2],i,n,x,y;
  605.  
  606.     detectgraph(&gd,&gm);
  607.  
  608.     initgraph(&gd,&gm,"C:\\TC\\BGI");
  609.  
  610.     printf("Enter no. of edges: ");
  611.  
  612.     scanf("%d",&n);
  613.  
  614.     for(i=0;i<n;i++)
  615.  
  616.     {
  617.  
  618.         printf("\nEnter coordinates for edge %d ",i+1);
  619.  
  620.         scanf("%d %d",&a[i][0],&a[i][1]);
  621.  
  622.     }
  623.  
  624.     a[n][0]=a[0][0];
  625.  
  626.     a[n][1]=a[0][1];
  627.  
  628.     cleardevice();
  629.  
  630.     for(i=0;i<n;i++)
  631.  
  632.         line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
  633.  
  634.     printf("\nEnter seed point: ");
  635.  
  636.     scanf("%d %d",&x,&y);
  637.  
  638.     b_fill(x,y,BLUE,15);
  639.  
  640.     getch();
  641.  
  642. }
  643.  
  644. //bresenham
  645.  
  646. #include <stdio.h>
  647.  
  648. #include <conio.h>
  649.  
  650. #include <graphics.h>
  651.  
  652. #include <math.h>
  653.  
  654.  
  655.  
  656. void bresenham(int x1,int y1,int x2,int y2)
  657.  
  658. {
  659.  
  660.     int xk,yk,e,s1,s2,dx,dy,xi,yi,i;
  661.  
  662.     xk=x1;
  663.  
  664.     yk=y1;
  665.  
  666.     dx=x2-x1;
  667.  
  668.     dy=y2-y1;
  669.  
  670.     putpixel(x1,y1,WHITE);
  671.  
  672.     if(dx==0 && dy==0)
  673.  
  674.         return;
  675.  
  676.     if(x2>x1)
  677.  
  678.         s1=1;
  679.  
  680.     else    s1=-1;
  681.  
  682.     if(y2>y1)
  683.  
  684.         s2=1;
  685.  
  686.     else    s2=-1;
  687.  
  688.     if(dx>dy)
  689.  
  690.     {
  691.  
  692.         e=2*dy-dx;
  693.  
  694.         for(i=0;i<=dx;i++)
  695.  
  696.         {
  697.  
  698.             if(e>0)
  699.  
  700.             {
  701.  
  702.                 yk=yk+s2;
  703.  
  704.                 e=e-2*dx;
  705.  
  706.             }
  707.  
  708.             xk=xk+s1;
  709.  
  710.             e=e+2*dy;
  711.  
  712.             putpixel(xk,yk,WHITE);
  713.  
  714.         }
  715.  
  716.     }
  717.  
  718.     else
  719.  
  720.     {
  721.  
  722.         e=2*dx-dy;
  723.  
  724.         for(i=0;i<=dy;i++)
  725.  
  726.         {
  727.  
  728.             if(e>=0)
  729.  
  730.             {
  731.  
  732.                 xk=xk+s1;
  733.  
  734.                 e=e-2*dy;
  735.  
  736.             }
  737.  
  738.             yk=yk+s2;
  739.  
  740.             e=e+2*dx;
  741.  
  742.             putpixel(xk,yk,WHITE);
  743.  
  744.         }
  745.  
  746.     }
  747.  
  748. }
  749.  
  750.  
  751.  
  752. void main()
  753.  
  754. {
  755.  
  756.     int gd,gm,x1,y1,x2,y2;
  757.  
  758.     detectgraph(&gd,&gm);
  759.  
  760.     initgraph(&gd,&gm,"C:\\TC\\BGI");
  761.  
  762.     printf("Enter starting co-ordinates\n");
  763.  
  764.     scanf("%d %d",&x1,&y1);
  765.  
  766.     printf("Enter ending co-ordinates\n");
  767.  
  768.     scanf("%d %d",&x2,&y2);
  769.  
  770.     bresenham(x1,y1,x2,y2);
  771.  
  772.     getch();
  773.  
  774.     closegraph();
  775.  
  776. }
  777.  
  778. //midpoint circle
  779.  
  780. #include<stdio.h>
  781.  
  782. #include <conio.h>
  783.  
  784. #include <math.h>
  785.  
  786. #include <graphics.h>
  787.  
  788.  
  789.  
  790. void display(int x1,int y1,int x,int y)
  791.  
  792. {
  793.  
  794.     putpixel(x1+x,y1+y,WHITE);
  795.  
  796.     putpixel(x1+x,y1-y,WHITE);
  797.  
  798.     putpixel(x1-x,y1+y,WHITE);
  799.  
  800.     putpixel(x1-x,y1-y,WHITE);
  801.  
  802.     putpixel(x1+y,y1+x,WHITE);
  803.  
  804.     putpixel(x1+y,y1-x,WHITE);
  805.  
  806.     putpixel(x1-y,y1+x,WHITE);
  807.  
  808.     putpixel(x1-y,y1-x,WHITE);
  809.  
  810. }
  811.  
  812.  
  813.  
  814. void midpointcircle(int x1,int y1,int r)
  815.  
  816. {
  817.  
  818.     int x,y;
  819.  
  820.     float p;
  821.  
  822.     x=0;
  823.  
  824.     y=r;
  825.  
  826.     p=1-r;
  827.  
  828.     while(x<=y)
  829.  
  830.     {
  831.  
  832.         if(p<=0)
  833.  
  834.         {
  835.  
  836.             x++;
  837.  
  838.             p=p+2*x+1;
  839.  
  840.         }
  841.  
  842.         else
  843.  
  844.         {
  845.  
  846.             x++;
  847.  
  848.             y--;
  849.  
  850.             p=p+2*(x-y)+1;
  851.  
  852.         }
  853.  
  854.         display(x1,y1,x,y);
  855.  
  856.     }
  857.  
  858. }
  859.  
  860.  
  861.  
  862. void main()
  863.  
  864. {
  865.  
  866.     int x1,y1,r,gd,gm;
  867.  
  868.     gd=DETECT;
  869.  
  870.     initgraph(&gd,&gm,"C:\\TC\\BGI");
  871.  
  872.     printf("Enter center co-ordiantes\n");
  873.  
  874.     scanf("%d %d",&x1,&y1);
  875.  
  876.     printf("Enter radius\n");
  877.  
  878.     scanf("%d",&r);
  879.  
  880.     midpointcircle(x1,y1,r);
  881.  
  882.     getch();
  883.  
  884.     closegraph();
  885.  
  886. }
  887.  
  888. //dda
  889.  
  890. #include <stdio.h>
  891.  
  892. #include <conio.h>
  893.  
  894. #include <graphics.h>
  895.  
  896. #include <math.h>
  897.  
  898.  
  899.  
  900. void dda(int x1,int y1,int x2,int y2)
  901.  
  902. {
  903.  
  904.     int xk,yk,step,dx,dy,xi,yi,i;
  905.  
  906.     xk=x1;
  907.  
  908.     yk=y1;
  909.  
  910.     dx=x2-x1;
  911.  
  912.     dy=y2-y1;
  913.  
  914.     if(abs(dx)>=abs(dy))
  915.  
  916.         step=abs(dx);
  917.  
  918.     else    step=abs(dy);
  919.  
  920.     xi=float(dx)/step;
  921.  
  922.     yi=float(dy)/step;
  923.  
  924.     for(i=0;i<=step;i++)
  925.  
  926.     {
  927.  
  928.         xk=xk+xi;
  929.  
  930.         yk=yk+yi;
  931.  
  932.         putpixel(int xk,int yk,WHITE);
  933.  
  934.         delay(2);
  935.  
  936.     }
  937.  
  938. }
  939.  
  940.  
  941.  
  942. void main()
  943.  
  944. {
  945.  
  946.     int gd,gm,x1,y1,x2,y2;
  947.  
  948.     detectgraph(&gd,&gm);
  949.  
  950.     initgraph(&gd,&gm,"C:\\TC\\BGI");
  951.  
  952.     printf("Enter starting co-ordinates\n");
  953.  
  954.     scanf("%d %d",&x1,&y1);
  955.  
  956.     printf("Enter ending co-ordinates\n");
  957.  
  958.     scanf("%d %d",&x2,&y2);
  959.  
  960.     dda(x1,y1,x2,y2);
  961.  
  962.     getch();
  963.  
  964.     closegraph();
  965.  
  966. }
  967.  
  968. //floodfill
  969.  
  970. #include<stdio.h>
  971.  
  972. #include<conio.h>
  973.  
  974. #include<graphics.h>
  975.  
  976.  
  977.  
  978. void f_fill(int x,int y,int f_col)
  979.  
  980. {
  981.  
  982.     if(getpixel(x,y)!=f_col)
  983.  
  984.     {
  985.  
  986.         putpixel(x,y,f_col);
  987.  
  988.         f_fill(x,y+1,f_col);
  989.  
  990.         f_fill(x,y-1,f_col);
  991.  
  992.         f_fill(x-1,y,f_col);
  993.  
  994.         f_fill(x+1,y,f_col);
  995.  
  996.     }
  997.  
  998. }
  999.  
  1000.  
  1001.  
  1002. void main()
  1003.  
  1004. {
  1005.  
  1006.     int gd,gm;
  1007.  
  1008.     int a[20][2],i,n,x,y;
  1009.  
  1010.     detectgraph(&gd,&gm);
  1011.  
  1012.     initgraph(&gd,&gm,"C:\\TC\\BGI");
  1013.  
  1014.     printf("Enter no. of edges: ");
  1015.  
  1016.     scanf("%d",&n);
  1017.  
  1018.     for(i=0;i<n;i++)
  1019.  
  1020.     {
  1021.  
  1022.         printf("\nEnter coordinates for edge %d ",i+1);
  1023.  
  1024.         scanf("%d %d",&a[i][0],&a[i][1]);
  1025.  
  1026.     }
  1027.  
  1028.     a[n][0]=a[0][0];
  1029.  
  1030.     a[n][1]=a[0][1];
  1031.  
  1032.     cleardevice();
  1033.  
  1034.     for(i=0;i<n;i++)
  1035.  
  1036.         line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
  1037.  
  1038.     printf("\nEnter seed point: ");
  1039.  
  1040.     scanf("%d %d",&x,&y);
  1041.  
  1042.     f_fill(x,y,BLUE);
  1043.  
  1044.     getch();
  1045.  
  1046. }
  1047.  
  1048. //piechart
  1049.  
  1050. #include<stdio.h>
  1051.  
  1052. #include<conio.h>
  1053.  
  1054. #include<graphics.h>
  1055.  
  1056. #include<math.h>
  1057.  
  1058.  
  1059.  
  1060. struct piechart
  1061.  
  1062. {
  1063.  
  1064.     float x,y;
  1065.  
  1066. }pts[2];
  1067.  
  1068.  
  1069.  
  1070. int i,n,xc,yc;
  1071.  
  1072. float r,data[5];
  1073.  
  1074.  
  1075.  
  1076. void piechart(int xc,int yc,int r)
  1077.  
  1078. {
  1079.  
  1080.     int x2=540,y2=240;
  1081.  
  1082.     float is,theta=0,total=0;
  1083.  
  1084.     circle(xc,yc,r);
  1085.  
  1086.     for(i=0;i<n;i++)
  1087.  
  1088.         total+=data[i];
  1089.  
  1090.     pts[0].x=xc;
  1091.  
  1092.     pts[0].y=yc;
  1093.  
  1094.     line(xc,yc,x2,480-y2);
  1095.  
  1096.     for(i=0;i<n-1;i++)
  1097.  
  1098.     {
  1099.  
  1100.         theta+=2*3.14*data[i]/total;
  1101.  
  1102.         pts[i].x=xc+r*cos(theta);
  1103.  
  1104.         pts[i].y=yc+r*sin(theta);
  1105.  
  1106.         x2=pts[i].x;
  1107.  
  1108.         y2=pts[i].y;
  1109.  
  1110.         setcolor(2);
  1111.  
  1112.         line(xc,480-yc,x2,480-y2);
  1113.  
  1114.     }
  1115.  
  1116. }
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122. void main()
  1123.  
  1124. {
  1125.  
  1126.     int gd,gm;
  1127.  
  1128.     detectgraph(&gd,&gm);
  1129.  
  1130.     initgraph(&gd,&gm,"C:\\TC\\BGI");
  1131.  
  1132.     printf("Enter no. of points\n");
  1133.  
  1134.     scanf("%d",&n);
  1135.  
  1136.     printf("Enter data\n");
  1137.  
  1138.     for(i=0;i<n;i++)
  1139.  
  1140.         scanf("%f",&data[i]);
  1141.  
  1142.     xc=640/2;
  1143.  
  1144.     yc=480/2;
  1145.  
  1146.     r=200;
  1147.  
  1148.     piechart(xc,yc,r);
  1149.  
  1150.     getch();
  1151.  
  1152. }
  1153.  
  1154. //scanline
  1155.  
  1156. #include<stdio.h>
  1157.  
  1158. #include<conio.h>
  1159.  
  1160. #include<graphics.h>
  1161.  
  1162.  
  1163.  
  1164. int isvertex(int x,int y,int a[20][2],int n)
  1165.  
  1166. {
  1167.  
  1168.     int i;
  1169.  
  1170.     for(i=0;i<n;i++)
  1171.  
  1172.         if(a[i][0]==x)
  1173.  
  1174.             if(a[i][1]==y)
  1175.  
  1176.                 return 1;
  1177.  
  1178.     return 0;
  1179.  
  1180. }
  1181.  
  1182.  
  1183.  
  1184. void main()
  1185.  
  1186. {
  1187.  
  1188.     int gd,gm;
  1189.  
  1190.     int a[20][2],k,n,left=640,right=0,top=480,bottom=0,i,j;
  1191.  
  1192.     detectgraph(&gd,&gm);
  1193.  
  1194.     initgraph(&gd,&gm,"C:\\TC\\BGI");
  1195.  
  1196.     printf("Enter no. of vertices\n");
  1197.  
  1198.     scanf("%d",&n);
  1199.  
  1200.     for(i=0;i<n;i++)
  1201.  
  1202.     {
  1203.  
  1204.         printf("Enter co-ordinates for vertice %d ",i+1);
  1205.  
  1206.         scanf("%d %d",&a[i][0],&a[i][1]);
  1207.  
  1208.         if(a[i][0]<left)
  1209.  
  1210.             left=a[i][0];
  1211.  
  1212.         if(a[i][0]>right)
  1213.  
  1214.             right=a[i][0];
  1215.  
  1216.         if(a[i][1]<top)
  1217.  
  1218.             top=a[i][1];
  1219.  
  1220.         if(a[i][1]>bottom)
  1221.  
  1222.             bottom=a[i][1];
  1223.  
  1224.     }
  1225.  
  1226.     a[i][0]=a[0][0];
  1227.  
  1228.     a[i][1]=a[0][1];
  1229.  
  1230.     cleardevice();
  1231.  
  1232.     for(i=0;i<n;i++)
  1233.  
  1234.         line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
  1235.  
  1236.     for(j=top;j<bottom;j++)
  1237.  
  1238.     {
  1239.  
  1240.         k=0;
  1241.  
  1242.         for(i=left;i<right;i++)
  1243.  
  1244.         {
  1245.  
  1246.             if(getpixel(i,j)==15 && getpixel(i+1,j)!=15)
  1247.  
  1248.             {
  1249.  
  1250.                 if(isvertex(i,j,a,n)==1)
  1251.  
  1252.                     k++;
  1253.  
  1254.                 k++;
  1255.  
  1256.             }
  1257.  
  1258.             if(k%2==1)
  1259.  
  1260.                 putpixel(i,j,15);
  1261.  
  1262.         }
  1263.  
  1264.     }
  1265.  
  1266.     getch();
  1267.  
  1268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement