Search This Blog

Most Viewed

Thursday, February 10, 2011

Second Order Curve Plotting Using C/C++

/*
Copy this whole blog to a text file and change it's extension from .txt to .cpp
then copy this file to your default file location of turbo C++ compiler.
Open this in Compiler and run...
*/

//-----------------------------------------------------//
/*
Second Order Equation is as follow

a*x² + 2*h*x*y +b*y² +2*g*x +2*f*y +c =0

Solution of this second order equation may be
1. Parabola
2. Hyperbola
3. Circle
4. Ellipse
5. Pair of straight line

All the four type of curve depend on the coefficients of equation,
a= Coeff of x square
b= coeff of y square
c= constant term
f= halved coeff of y
g= halved coeff os x
h= coeff of xy

let:
Δ = abc +2fgh -af² -bg² -ch²

now the Quadratic equation will represents various things depending on nature of Δ and other coeff. these are

1. Straight Line Pair: Δ = 0, h² ╪ ab
2. Parallel Line Pair: Δ = 0, h² = ab
3. Perpendicular Line Pair: Δ = 0, a+b = 0
4. Parabola: Δ ╪ 0, h² = ab
5. Hyperbola: Δ ╪ 0, h² > ab
6. Ellipse: Δ ╪ 0, h² < ab
7. Rectangular Hyperbola: Δ ╪ 0, h² = ab, a+b = 0
8. Circle: Δ ╪ 0, a = b, h = 0

All the above Conic Section and many more curves can be drawn by using Quadratic equation.

*/

// Drawing Code Starts Here

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

#define pi 3.141592
void main()
{
  int driver=DETECT,mode;

  double a,b,c,f,g,h;   //Coefficients of Quadratic Equation
  double x,y;   //Variable of Cartesian Coordinate system
  double scl=10,acc=.1;
  int ox=320,oy=240;

  initgraph(&driver,&mode,"Pankaj");
a:
  cout<<"Coeff of x²(a)=";cin>>a;
  cout<<"Coeff of y²(b)=";cin>>b;
  cout<<"Coeff of 2x(g)=";cin>>g;
  cout<<"Coeff of 2y(f)=";cin>>f;
  cout<<"Coeff of xy(h)=";cin>>h;
  cout<<"Constant   (c)=";cin>>c;

  for(x=-20;x<=20;x+=acc)
  {
    for(y=-20;y<=20;y+=acc)
    {
      double curve=a*x*x+2*h*x*y+b*y*y+2*g*x+2*f*y+c;
      int posx=ox+scl*x;
      int posy=oy-scl*y;
      putpixel(ox,posy,RED);
      putpixel(posx,oy,RED);
      if(curve<0)
      {
        putpixel(posx,posy,YELLOW);
      }
      else
      {
        putpixel(posx,posy,BLUE);
      }
    }
  }
  cout<<"Pushpkant"<<endl<<"Press E for Exit";
  char q=getch();
  closegraph();
  if (q==101)
    goto b;
  else
    goto a;
  b:
}

No comments:

Post a Comment

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


Suggestion are welcome:

Popular Posts