Monday, 30 April 2012

Convert Polar to Quadratic Coordinates in C++


#include <graphics.h>
#include <conio.h>
#include <math.h>
#define MAXX 640
#define MAXY 480

float sy(float, float, float);
float sy(float, float, float);
float px(float, float);
float py(float, float);

float sx(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 px(float r, float phi)
{
 return r * cos(phi);
}

float py(float r, float phi)
{
 return r * sin(phi);
}

/**********************************************************/
void main(void)
{
 int drv = VGA, mode = VGAHI;
 float phi, r, xb, xe, yb, ye, x, y;

 xb = -3;
 xe = 3;
 yb = -3;
 ye = 3;

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

 setcolor(15);
 line( 0, scry(yb, 0, ye), MX, scry(yb, 0, ye) );
 line( sx(xb, 0, xe), 0, sx(xb, 0, xe), MY );

 moveto( sx(xb, 0, xe), scry(yb, 0, ye) );
 for(phi = 0; phi < 2 * 3.1415; phi += 0.02)
 {
  r = sin(2 * phi) + 2 * cos(3 * phi);
  x = px(r, phi);
  y = py(r, phi);
  lineto(sx(xb, x, xe), scry(yb, y, ye) );
 }
 getch();
 closegraph();
 return;
}

No comments:

Post a Comment