Monday, 30 April 2012

Handling Graphs in C++


#include <graphics.h>
#include <conio.h>
#include <math.h>
#define MX 640
#define MY 480
float scry(float, float, float);
float scry(float, float, float);
float perform(float);

float a,b,c,d,e,f,g,h,i,j;

float scrx(float xb, float x, float xe)
{
 return ((x-xb)/(xe-xb)*MX);
}

float scry(float yb, float y, float ye)
{
 return (MY-(y-yb)/(ye-yb)*MY);
}

float perform(float x)
{
 return a *  sin( ( b * x + c ) * 3.1415 / 180.0) ;
}

void main(void)
{
 int drv=VGA, mode=VGAHI;
 float xb,xe,yb,ye,x,y,potx,poty;
 char C;

 a  = 1;
 b  = 1;
 c  = 0;
 xb = -360;
 xe = 360;
 yb = -10;
 ye = 10;

 initgraph(&drv,&mode,"");

 setcolor(4);
 line(0,scry(yb,0,ye),MX,scry(yb,0,ye));
 line(scrx(xb,0,xe),0,scrx(xb,0,xe),MY);

 setcolor(15);
 moveto(0,scry(yb,perform(xb),ye));
 for(x=xb;x < =xe;x+=(xe-xb)/MX)
 {
  y = perform(x);
  lineto(scrx(xb,x,xe),scry(yb,y,ye));
 }

 while((C=getch())!=27) 
 {
  potx = xb;
  poty = yb;
  switch(C)
  {
   case 'z':  
     xb -= (xe-xb)/4.0;
     xe -= (xe-potx)/4.0;
     break;
   case 'x':       
     xb += (xe-xb)/4.0;
     xe += (xe-potx)/4.0;
     break;
   case 'q':      
     yb += (ye-yb)/4.0;
     ye += (ye-poty)/4.0;
     break;
   case 'w':      
     yb -= (ye-yb)/4.0;
     ye -= (ye-poty)/4.0;
     break;
   case '+':  
     yb -= (yb-ye)/4.0;
     ye += (poty-ye)/4.0;
     break;
   case '-':      
     yb += (yb-ye)/4.0;
     ye -= (poty-ye)/4.0;
     break;
   case '[':  
     xb += (xb-xe)/4.0;
     xe -= (potx-xe)/4.0;
     break;
   case ']':  
     xb -= (xb-xe)/4.0;
     xe += (potx-xe)/4.0;
     break;
   case '/':  
     xb = -360;
     xe = 360;
     yb = 10;
     ye = -10;
     break;
   default:
     continue;
     break;
  }

  cleardevice();
  
  setcolor(4);
  line(0,scry(yb,0,ye),MX,scry(yb,0,ye));
  line(scrx(xb,0,xe),0,scrx(xb,0,xe),MY);

  setcolor(15);
  moveto(0,scry(yb,perform(xb),ye));
  for(x=xb;x<=xe;x+=(xe-xb)/MX)
  {
   y = perform(x);
   lineto(scrx(xb,x,xe),scry(yb,y,ye));
  }
 }
 closegraph();
 return;
}

No comments:

Post a Comment