I have a std::vector that holds a Point struct (x,y,z and some other non-pointer types).
These points are control points for drawing a bspline curve. I’m not having trouble drawing the curve, but complications arise when I have to close the curve, which involves adding control points (alredy existing inside the container) in certain order.
For example, if I have 5 control points
A B C D E
I would have to get 5 sequences like this:
A B C D //curve is drawn from B to C
B C D E //curve is drawn from C to D
C D E A //curve is drawn from D to E
D E A B //curve is drawn from E to A
E A B C //curve is drawn from A to B
Initially, I went with std::rotate, but then realized it wasn’t what I was looking for.
I’m having trouble implementing this. Best I got is a non-working version in C++ (the reason of this failing is not the question, here’s a snippet).
static char letters = 'A';
typedef struct Point{
float x,y,z;
char name;
Point(float x,float y,float z=0):name(letters++){}
}Point;
typedef std::vector<Point> lpoints;
void
rotate(lpoints& points)
{
for (unsigned int i =0;i<5;i++){
lpoints::iterator beg = lista.begin() + (i%5);
lpoints::iterator dernier=lista.begin()+(4+i)%6;
lpoints subseq(beg,dernier); //4 points in subseq
//do stuff with subseq
}
}
Is there a way to do this? I know I can implement it with many nested for loops, but i’m trying to avoid that, looking for something more elegant (if the word fits).
Thanks in advance.
What, exactly, is wrong with using
std::rotate()? For example,firstFourthen contains the first four elements from the rotated vector. If you use this in a loop and run itv.size()times, you will get the five vectors you have in the question.