Search This Blog

Most Viewed

Thursday, February 10, 2011

Function Plotting using C/C++

/*
Copy This Whole Blog and Past into a CPP file. Compile it and Enjoy.


This is demo of Function Plotting using turbo C++ 3.0 compiler.

Function is a kind of relation in which every element in Range must be related to one of more domain's elements.

Function y=f{x}.
*/


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


#define pi 3.141592

void main()
{
  int driver=DETECT,mode;  //Request for Auto detect
  double x=0,y=0;   //Our independent and Dependent variables
  int ox=320,oy=240;   //Computer Coordinates of origin
  double scl=10;   //Scale Factor
  double acc=.001;   //Sampling Rate


  //Initilizing the Graphics mode
  initgraph(&driver,&mode,"Pankaj");

  acc=.001;   //Changing the quality of curve
       //Better quallity takes more time
       //decrease for increase the quality
  scl=10;     //Scale factor, increase for zoom in and decrease for zoom out
  ox=320;     //Origin's horizontal computer coordinate
  oy=240;     //Origin's vertical computer coordinate

  y=acc;
  for(x=-32;x<=32;x+=acc)    //Defining Range of independent Var
  {
    y=10*sin(x/y);    //Our Function Goes Here

    //Cartesian to computer Coordinate Conversion
    double posx=ox+scl*x;    //X coordinate conversion
    double posy=oy-scl*y;    //Y coordinate conversion

    //Drawing of Curve
    putpixel(posx,posy,WHITE);

    //Axis Drawing
    putpixel(ox,posy,RED);
    putpixel(posx,oy,RED);

  }

  cout<<"Pushpkant";
  getch();
  closegraph();
}


/*
We can draw simultaneously as many function as we wish, just add more dependent variable and their computer conversions.


One should follow the mathematical rule for the function plotting. for example never take log of negative number, or divide by zero.
*/

No comments:

Post a Comment

If the contents is insufficient or if there any error, please write here....


Suggestion are welcome:

Popular Posts