I am trying to make a function to genereate bezier curves and save each x,y position in a matrix. The row number indicates the number of objects I will set the coordinates to and the number of columns is the number of points (coordinates) generated by the function. I am trying to generate two curves for two objects using the same function but the new values generated by the function the second time are not saved in the vector and I don't know why.
This is my code
typedef struct
{
float coord_x, coord_y;
} coor_bezier;
std::vector <coor_bezier> coordinates_bz; //Save x,y
void beziercurves(int x0, int y0)
{
int x[4] = {x0,x0+200,x0,x0+300}, y[4] = {y0,y0+250,y0+550,H};
coor_bezier arr;
float u;
for(u=0.0; u<=1; u+=0.005)
{
arr.coord_x=x[0]*pow(1-u, 3) + x[1]*3*u*pow(1-u, 2) + x[2]*pow(u, 2)*3*(1-u) + x[3]*pow(u,3);
arr.coord_y=y[0]*pow(1-u, 3) + y[1]*3*u*pow(1-u, 2) + y[2]*pow(u, 2)*3*(1-u) + y[3]*pow(u,3);
coordinates_bz.push_back(arr);
}
}
////GENERATE COORDINATES BEZIER CURVES
coor_bezier position, positionbz[2][201];
int k=100;
for(int i=0; i<2; i++)
{
beziercurves(k, 0);
for(int j=0; j<201; j++)
{
position = coordinates_bz.at(j);
positionbz[i][j] = position ;
}
k=200;
}
The new values are saved to the vector, but you didn't read the new values and instead of that you are reading the values firstly added twice.
The new values are added to the vector without clearing the vector, so the new values will be added after the values that are already added.
If you want to read new values, you can store the offset to read before adding elements and use the information to read new values.
////GENERATE COORDINATES BEZIER CURVES
coor_bezier position, positionbz[2][201];
int k=100;
for(int i=0; i<2; i++)
{
size_t offset = coordinates_bz.size(); // save the offset
beziercurves(k, 0);
for(int j=0; j<201; j++)
{
position = coordinates_bz.at(offset + j); // use the offset to read new values
positionbz[i][j] = position ;
}
k=200;
}