304 Introduction to C++ Programming and Graphics
The circle is described by a collection ofN+ 1 marker points tracing
its perimeter, where the last point is the periodic image of the first point. The
coordinates of the marker points are stored in the vectorsxandy. The motion is
computed in the C++ domain and then transferred to theMatlabworkspace
for visualization:
#include <iostream>
#include <cmath>
#include "engine.h"
using namespace std;
int main()
{
//--- Data:
const double pi=3.14159265358;
const short N=32;
double centerx = 0.0;
double centery = 0.0;
double step = 2*pi/N;
double x[N+1], y[N+1];
for (int i=0; i<=N; i++)
{
double arg = i*step;
x[i]= cos(arg)+centerx;
y[i]= sin(arg)+centery;
}
//--- Start a matlab session:
Engine * cokar = engOpen("matlab -nodesktop -nosplash");
/*--- Reserve the vector ‘‘xx’’
copy into memory
import the data */
mxArray * xx = mxCreateDoubleMatrix(1, N+1, mxREAL);
memcpy((void *)mxGetPr(xx), (void *)x, sizeof(x));
engPutVariable(cokar, "xx", xx);
/*--- Reserve the vector ‘‘yy’’
copy into memory
import the data */
mxArray * yy = mxCreateDoubleMatrix(1, N+1, mxREAL);
memcpy((void *)mxGetPr(yy), (void *)y, sizeof(y));
engPutVariable(cokar, "yy", yy);