Thursday, September 27, 2012

Graph – DDA and Breshenham Algorithm




Digital differential analyzer (DDA) is used for linear interpolation of variables over an interval between start and end point. DDA is used for plotting horizontal or vertical lines. It is use to plot lines, triangles and polygons. DDA algorithm can be described as naive line-drawing algorithm, with minor variations.

Note : You should have graphics.h header file  in the specified directory otherwise no graphs  will be displayed. You also can copy the said file in  the current directory and declared it as  #include "graphics.h". Some of the software which meant for 64 bit system are without any graphics utility. ‘Turbo C ‘ is providing a lots of graphics utilities.




#include <stdio.h>
#include <conio.h>
#include<graphics.h>

  void ddaGr(int xs,int ys,int xn,int yn)
      {
           int dx,dy,x=0,y=0,len,i,s,j;
              if((xn-xs)>(yn-ys))
                   {
                        len=(xn-xs);
                   }
              else
                   {
                        len=(yn-ys);
                   }

            dx=((xn-xs)/len);
            dy=((yn-ys)/len);
            for(i=1;i<=len;i++)
              {
                   x=x+dx;
                   y=y+dy;
                   putpixel(x,y,4);
                   if(dx>dy)
                        {
                            x++;
                        }
                   else
                        {
                            y++;
                        }
              }
      }

  int main()
      {
           int gd,gm,x1,y1,x2,y2;
           printf("\n\n\tEnter the co-ordinates of first point : ");
           scanf("%d%d",&x1,&y1);
           printf("\n\n\tEnter the co-ordinates of second point : ");
           scanf("%d %d",&x2,&y2);
           detectgraph(&gm,&gd); 
           initgraph(&gm,&gd,""); // mentioned proper path for bgi
           ddaGr(x1,y1,x2,y2);
           getch();
           closegraph();
      }




The Bresenham algorithm  used to draw lines on a computer screen, using only integer operation such as addition, subtraction and bit shifting. A modified extension of the  algorithm also implemented for dawing circles.

#include <stdio.h>
#include <conio.h>
#include<graphics.h>

  int main()
      {
           int dx,dy,x,y,t,x1,y1,x2,y2;
           int gd,gm;
           printf("\n\n\tEnter the co-ordinates of first point : ");
           scanf("%d%d",&x1,&y1);
           printf("\n\n\tEnter the co-ordinates of second point : ");
           scanf("%d %d",&x2,&y2);
           dx = (x2 - x1);
           dy = (y2 - y1);
           t = 2 * (dy) - (dx);
           x = x1;
           y = y1;
           detectgraph(&gd,&gm);
           initgraph(&gd,&gm,"");
           putpixel(x,y,WHITE);
           while(x <= x2)
              {
                   if(t < 0)
                        {
                            x=x+1;
                            y=y;
                            t = t + 2 * (dy);
                        }
                   else
                        {
                            x=x+1;
                            y=y+1;
                            t = t + 2 * (dy - dx);
                        }
                   putpixel(x,y,WHITE);
              }
           getch();
           closegraph();
              return 0;
      }




#include <stdio.h>
#include <conio.h>
#include<graphics.h>

  void plot(int x,int y,int xc,int yc)
      {
           putpixel(x+xc,y+yc,1);
           putpixel(y+xc,x+yc,1);
           putpixel(-y+xc,x+yc,1);
           putpixel(-x+xc,y+yc,1);
           putpixel(-x+xc,-y+yc,1);
           putpixel(-y+xc,-x+yc,1);
           putpixel(y+xc,-x+yc,1);
           putpixel(x+xc,-y+yc,1);
      }
  int main()
      {
           int gdriver, gmode;
           float Rad, xc, yc, x, y, p;
           printf("\n\n\tEnter the radius : ");
           scanf("%f",&Rad);
           printf("\n\tEnter the Center Point : ");
           scanf("%f%f",&xc,&yc);
           detectgraph(&gdriver,&gmode);
           initgraph(&gdriver, &gmode, "");
           x=0;
           y=Rad;
           plot(x,y,xc,yc);
           p=3-2*Rad;
           while(x<=y)
              {
                   if(p<0)
                        {
                            x=x+1;
                            plot(x,y,xc,yc);
                            p=p+4*x+6;
                        }
                   else
                        {
                            x=x+1;
                           y=y-1;
                            plot(x,y,xc,yc);
                            p=p+4*(x-y)+10;
                        }
              }
           getch();
           closegraph();

           return 0;
      }




Thursday, September 20, 2012

Graphs with Turbo-C–I


Graphics Routine :

Graphics are fun and we can easily make some program based 2-D animation or some fun games.

Turbo C have some built-in graphics command. All graphic utilites are present in the ‘graphics.h’, header file. First we have to find the Graphic mode and Graphic driver for the system we are using. ‘detectgraph’ used for detecting  the graphics adapter and the mode. ‘detectgraph’ needs two integer arguments to detect the driver and mode. Once it is done then we have to initialize the graph by ‘initgraph’ funtion. In 'initgraph' function  we have passed three arguments first is the address of graphic driver, second is the address of graphic mode, that used in ‘detectgraph’ function. The third is the path where your BGI files are present. The BGI path will be vary from machine to machine and one has to adjust this accordingly.

Here are some example of the graphics routine among many functions that are available in the ‘graphics.h’.






#include <stdio.h>
#include<graphics.h>

  void Info()
      {
           int x,y,c;
           char ch;
           x=getmaxx();
           y=getmaxy();
           c=getmaxcolor();
           printf("\n\n\n\tMaximum X and Y coridinate : %d  %d ",x,y);
           printf("\n\n\n\tMaximum Color : %d ",c);
           printf("\n\n\n\tPress Enter to Continue  ");
           ch=getchar();
           cleardevice();
      }
  void LineRect()
      {
           outtextxy(30,20,"Horizontal Line ");
           line(50,50,150,50);
           outtextxy(200,20,"Vertical Line ");
           line(250,50,250,150);
           outtextxy(350,20,"Slant Line ");
           line(350,50,450,150);
           outtextxy(480,20,"Rectangle ");
           rectangle(480,50,580,150);
      }
  void Circular()
      {
           outtextxy(20,200,"Circle ");
           circle(50,250,30);
           outtextxy(120,200,"Ellipse ");
           ellipse(150,250,90,270,30,20);
           outtextxy(200,200,"Sector");
           sector(230,250,270,0,30,20);
      }
  void BarPoly()
      {
           int poly[8];
           char ch;
           outtextxy(300,200,"Bar");
           bar(300,220,330,270);
           outtextxy(390,200,"Bar-3D");
           bar3d(375,230,410,270,10,10);
           outtextxy(490,200,"Polygon");
           poly[0]=525;
           poly[1]=220;
           poly[2]=575;
           poly[3]=270;
           poly[4]=475;
           poly[5]=270;
           poly[6]=poly[0];
           poly[7]=poly[1];
           drawpoly(4,poly);
           outtextxy(200,350,"Press any Key to Cont..");
           ch=getchar();
           cleardevice();
      }
  void ColFill()
      {
           char ch;
           int i,j=1,k;
           outtextxy(120,100,"Fill & Color");
           for(i=50;i<=600;i=i+70)
              {
                   j=j+1;
                   setcolor(j);
                   setfillstyle(j,k);
                   circle(i,150,30);
                   floodfill(i,150,j);
                   if(j>=15)
                        {
                            j=1;
                        }
                   if(k>=10)
                        {
                            k=1;
                        }
                   k=k+1;
              }
           outtextxy(200,350,"Press any Key to Cont..");
           ch=getchar();
           cleardevice();
      }
  void Font()
      {
           char ch;
           int i,j=1,k=1,f=20;
           outtextxy(120,10,"Font Demo");
           for(i=20;i<=560;i=i+120)
              {
                   for(f=20;f<=320;f=f+100)
                   {
                        settextstyle(j,0,k);
                        outtextxy(i,f,"Hi");
                        j++;
                        if(j>5)
                            {
                                j=1;
                            }
                        k++;
                        if(k>10)
                            {
                                k=1;
                            }
                   }
              }
           settextstyle(1,1,1);
           outtextxy(250,350,"Hello");
      }
  int main()
      {
           int gd,gm;
           char ch;
           detectgraph(&gd,&gm); //Detecting the Graphic Driver and mode
           initgraph(&gd,&gm,""); //initializing the graph
           Info();
           LineRect();
           Circular();
           BarPoly();
           ColFill();
           Font();
           settextstyle(1,0,1);
           outtextxy(20,430,"Press any Key to Cont..");
           ch=getchar();
           closegraph();
      }