Loading

Archive for 8 Aug 2012

c program for graphics editor

C - Program for Graphics Editor

-------------------------------------------------------------------------------------------------------------
Title: assignment to understand functions available in graphics liabrary such as,
1. Text  & graphics mode,initialization of graphics mode, graphics drivers, switching between text & graphics mode,error handling.
2. Colour,colour palette,aspect ratio,text:fonts,alignments,size,orientation &justification.
3. Graphics primitives: pixel,line,circle,ellipse,polygons,line styles,bar graphs,pie charts,histogram,filling a polygon, windowing.
4. Writing a graphics editor.
------------------------------------------------------------------------------------------------------------

 
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>        //include header file
#include <conio.h>
#include<iostream.h>
class graph_edit           //class declraration
{               
   int x1,y1,x,y;

   public:
   void drawcircal();
   void drawline();
   void drawellips();
   void draw_poly();
   void fill_poly();
   void barchar();
   void piechart();
   void pixel();
   void text();
   graph_edit();
   };


graph_edit::graph_edit()    //constructor declaration
{
x1=y1=0;
}

void graph_edit::piechart() //function for piechart
{
      x=getmaxx()/2;
      y=getmaxy()/2;

      setfillstyle(SOLID_FILL, 5);
      pieslice(x,y,0,60,150);
      setfillstyle(SOLID_FILL, 10);
      pieslice(x,y,61,180,150);
      setfillstyle(SOLID_FILL, 12);
      pieslice(x,y,181,360,150);
   }

void graph_edit::barchar() //function for barchart
{
      x=getmaxx();
      y=getmaxy();

      setfillstyle(BKSLASH_FILL, getmaxcolor());
      line(50,20,50,y-20);
      line(20,y-50,x-20,y-50);
      bar(100,y-50,x-500,200);
      bar(200,y-50,x-400,150);
      bar(300,y-50,x-300,250);
      bar(400,y-50,x-200,350);
      outtextxy(40,22,"Y");
      outtextxy(x-30,y-40,"x");
      outtextxy(10,y-30,"(0,0)");
}

void graph_edit::drawcircal() //function for circal
{
      int rad;
      x=getmaxx()/2;
      y=getmaxy()/2;

      cout<<"\nEnter the radius";
      cin>>rad;

      setcolor(getmaxcolor());
      clrscr();
      circle(x,y,rad);
}

void graph_edit::draw_poly()  //function for polygon
{
      int poly[15];
      x=getmaxx();
      y=getmaxy();

      poly[0] = 20;
      poly[1] = x/2;

      poly[2] = x-20;
      poly[3] = 20;

      poly[4] = x-50;
      poly[5] = y-20;

      poly[6] = x/2;
      poly[7] = y/2;

      poly[8] = poly[0];
      poly[9] = poly[1];

      clrscr();
      drawpoly(5, poly);

}

void graph_edit::text()  //function for textstyle
{
      char str[20];
      int si;
      y=getmaxy()/2;
      cout<<"\nEnter the size of Text:";
      cin>>si;
      settextstyle(SANS_SERIF_FONT,HORIZ_DIR, si);
      sprintf(str,"Graphics");
      clrscr();
      outtextxy(0,y,str);
}
void graph_edit::fill_poly() //function for fillpolygon
{
      int poly[15];
      x=getmaxx();
      y=getmaxy();

      poly[0] = 20;
      poly[1] = x/2;

      poly[2] = x-20;
      poly[3] = 20;

      poly[4] = x-50;
      poly[5] = y-20;

      poly[6] = x/2;
      poly[7] = y/2;

      poly[8] = poly[0];
      poly[9] = poly[1];

      clrscr();

      drawpoly(5, poly);
      setfillstyle(SLASH_FILL,12);
      fillpoly(5,poly);
      getch();
}



void graph_edit::drawellips() //function for ellips
{
      int angle_s,angle_e,x_rad,y_rad;
      angle_s=0;
      angle_e=360;
      x=getmaxx()/2;
      y=getmaxy()/2;

      cout<<"\n Enter x and y radius:";
      cin>>x_rad>>y_rad;

      clrscr();
      ellipse(x,y,angle_s,angle_e,x_rad,y_rad);
      cout<<x1;
}
void graph_edit::pixel()  //function for draw pixel
{
int x,y;
cout<<" Enter X and Y Position";
cin>>x>>y;
putpixel(x,y,10);
}

void graph_edit::drawline()   //function for draw line
{

      int x2,y2;
      cout<<"Enter starting point (x1,y1)";
      cin>>x1>>y1;
      cout<<"Enter ending point (x2,y2)";
      cin>>x2>>y2;
      clrscr();
      line(x1,y1,x2,y2);

}


int main(void)     //main function declaration
{
graph_edit g;
int ch;           //initialize graphic mode
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
errorcode = graphresult();
if(errorcode != grOk)
{
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);
}
do              //display menu
{

      cout<<"\n\n\t\tMENU";
      cout<<"\n\n\t1:Circle\n\t2:Line\n\t3:ellips";
      cout<<"\n\t4:polygon\t\n\t5:FillPolygon\n\t6:Text";
      cout<<"\n\t7:Pixel\n\t8:Barchart\n\t9:Pie Chart\n\t10:Exit";
      cout<<"\n\nEnter your choice:";
      cin>>ch;

switch(ch)
{

case 1: g.drawcircal();
      getch();          //case for circal drawing
      cleardevice();
      break;
case 2: g.drawline();
      cleardevice();    //case for line drawing
      getch();
      break;
case 3: g.drawellips();
      getch();          //case for ellips drawing
      cleardevice();
      break;
case 4: g.draw_poly();
      getch();          //case for polygon drawing
      cleardevice();
      break;
case 5: g.fill_poly();
      cleardevice();    //case for polygon filling
      getch();
      break;
case 6: g.text();
      getch();          //case for text style
      cleardevice();
      break;
case 7: cleardevice();
      g.pixel();        //case for pixel drawing
      getch();
      break;
case 8:     cleardevice();
      g.barchar();      //case for barchart
      getch();
      break;
case 9:     cleardevice();
      g.piechart();     //case for barchart
      getch();
case 10:exit(0);
      getch();

}

      cleardevice();
}while(ch!=10);

   getch();               //closing graph
   closegraph();
   return 0;
}

Posted in | Leave a comment

c program dda bresenham line and circle drawing algorithm

C - Program to Implement DDA and Bresenhams Line and Circle Drawing Algorithm



#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<stdlib.h>
#include<dos.h>
class shape                           // Declare a class shape
{
            float x1,y1,x2,y2;
public:
            void dda(float x1,float x2,float y1,float y2);                    // pass rthe parameters to draw a line
            void Bresenhamsline(float x1,float x2,float y1,float y2);               // pass the co-ordinates to draw the line
            using bresenhamsline drawing algorithm
            void Bresenhamscircle();                                                //pass the co-ordinates to draw the circle                                     using bresenhams circle drawing algorithm

};

void shape::dda(float x1,float x2,float y1,float y2 )
{
      int dx,dy,step,xinc,yinc;

      float slop,x3,y3;

      dx=x2-x1;                           //passing the parameters to         starting and ending points of parameter
      dy=y2-y1;

      if(abs(dx)>abs(dy))
            step=abs(dx);                 // calculate the absolute value
      else
            step=abs(dy);
      if(dx==0)
            slop=float(dy/(dx+0.1));      //find the slope of line
      else
            slop=float(dy/dx);

      putpixel(x1,y1,WHITE);
                                         
      x3=x1;
      y3=y1;

      for(int i=1;i<step;i++)
      {
            If(abs(dx)>abs(dy)&&x3<x2)
{
      x3=x3+1;
      y3=float(y3+slop);
}
else if(abs(dx)>abs(dy) && x3>x2)
{
      x3=x3-1;
      y3=y3-slop;
}
else if(abs(dx)<abs(dy) && y3<y2)
{
      x3=float(x3+(1/slop));
      y3=y3+1;
}
else if(abs(dx)<abs(dy) && y3>y2)
{
      x3=float(x3-(1/slop));
      y3=y3-1;
}
xinc=(x3+0.5);
yinc=(y3+0.5);
putpixel(xinc,yinc,WHITE);
}
}

void shape::Bresenhamsline(float x1,float x2,float y1,float y2)
{
      float Dx,Dy,x,y,e;
      Dx = x2 - x1;
      Dy = y2 - y1;
      Dx = fabs(Dx);
      Dy = fabs(Dy);
      x = x1;
      y = y1;
      e = 2 * (Dy-Dx);
      int i;
      i = 1;
      cleardevice();
      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);
}

void shape::Bresenhamscircle()
{
      float r,d,x,y;
      cout<<"\n\tEnter the radius of a circle->";
      cin>>r;
      clrscr();
      cleardevice();
      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(10);
      }while(x<y);
}

void main()
{
int ch;
float x1,x2,y1,y2;
shape s;
    int gdriver = DETECT, gmode, errorcode;
   initgraph(&gdriver, &gmode, "c:\\tc\\bgi");
   errorcode = graphresult();
   if (errorcode != grOk)
   {
      printf("Graphics error: %s\n", grapherrormsg(errorcode));
      printf("Press any key to halt:");
      getch();
      exit(1);
   }
   do
   {
cout<<"\n\n\t\tMENU";
cout<<"\n\t1:DDA line drawing";
cout<<"\n\t2:Bresenhamsline";
cout<<"\n\t3:Bresenhamscircle;";
cout<<"\n\nEnter Your choice";
cin>>ch;
switch(ch)
{
case 1:cout<<"\nEnter (x1,y1) point";
      cin>>x1>>y1;
      cout<<"\nEnter (x2,y2) point";
      cin>>x2>>y2;               ;
      clrscr();
      s.dda(x1,x2,y1,y2);
      break;
case 2:
      cout<<"\nEnter (x1,y1) point";
      cin>>x1>>y1;
      cout<<"\nEnter (x2,y2) point";
      cin>>x2>>y2;               ;
      clrscr();
      s.Bresenhamsline(x1,x2,y1,y2);
      break;
case 3: clrscr();
      s.Bresenhamscircle();
      break;
}
}while(ch!=4);
getch();
}

Posted in , , | Leave a comment

c/c++ program to implement 2D polygon transformation

 

C/C++ Program to Implement 2D Polygon Transformation

  
 #include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>

int x1,y1,x[20],y[20],n;
float theta;

void draw_poly()
{
  int i;
  for(i=0;i<n-1;i++)
  {
    line(x[i],y[i],x[i+1],y[i+1]);
  }
    line(x[0],y[0],x[n-1],y[n-1]);
}

void translate(int x1,int y1)
{
  int a;
  for(a=0;a<n;a++)
  {
    x[a]=x[a]+x1;
    y[a]=y[a]+y1;

  }
}

void scale(int x1,int y1)
{
  int a,b;
  a=x[0];
  b=y[0];
  translate(-a,-b);
  int i;
  for(i=0;i<n;i++)
  {
    x[i]=x[i]*x1;
    y[i]=y[i]*y1;

  }
  translate(a,b);

}

void rotate(float theta)
{
  int a,b,i,c,d;
  a=x[0];
  b=y[0];
  translate(-a,-b);
  for(i=0;i<n;i++)
  {
    c=x[i]*cos(theta)-y[i]*sin(theta);
    d=x[i]*sin(theta)+y[i]*cos(theta);
    x[i]=c;
    y[i]=d;
  }
  translate(a,b);
}


int main()
{
  /* request auto detection */
int gdriver = DETECT, gmode, errorcode;

/* initialize graphics and local variables */
initgraph(&gdriver, &gmode, "");

/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk)  /* an error occurred */
{
   printf("Graphics error: %s\n", grapherrormsg(errorcode));
   printf("Press any key to halt:");
   getch();
   exit(1); /* terminate with an error code */
}
int i;
clrscr();
cout<<"\nEnter no. of Sides:: ";
cin>>n;
cout<<"\nEnter the Co-ordinate of Vertices::";
for(i=0;i<n;i++)
{
    cout<<"\n"<<i+1<<"::";
    cin>>x[i]>>y[i];

}
   draw_poly();
   getch();
   cleardevice();

   clrscr();
   int k;
   do
   {
    clrscr();
    cleardevice();
    cout<<"\n1)Translate\n2)Rotate\n3)Scale\n4)Exit.";
    cout<<"\nEnter Ur Choice::";

    cin>>k;
    switch(k)
    {
        case 1:
            cout<<"\nFor x-axis::";
            cin>>x1;
            cout<<"\nFor y-axis::";
            cin>>y1;
            translate(x1,y1);
            draw_poly();
            getch();
            cleardevice();
            break;

        case 2:
            cout<<"\nEnter the Angle::";
            cin>>theta;
            rotate(theta);
            draw_poly();
            getch();
            cleardevice();
            break;

        case 3:
            cout<<"\nFor x-axis::";
            cin>>x1;
            cout<<"\nFor y-axis::";
            cin>>y1;
            scale(x1,y1);
            draw_poly();
            getch();
            cleardevice();
            break;

    }
    } while(k!=4);
   return 0;
}


Posted in | 1 Comment