You can do it without iterators. May be you'll find this code more clean:
for(int i = 0; i <= list.count() - 2; i++) {
QgsPoint a = list[i], b = list[i + 1];
//...
}
Frage
I am working in QT Creator. I want to find slope of two points. For Two points A(x1,y1) , B(x2,y2) using the slope formula
m = (y2-y1)/(x2-x1).
Problem :
Points are stored in list p. I want to take two points at a time from this list and find the slope of two points till the end of list.
For Example if the list contains 5 points {a,b,c,d,e}. I want to find slope of 1. ab 2. bc 3. cd 4. de
Code:
QList< QgsPoint > p;
{
/* some Process */
p.push_front( path->vertex( e.inVertex() ).point() ); /* some points are added to list*/
}
QList< QgsPoint>::iterator it;
for ( it = p.begin(); it != p.end(); ++it )
{
mrbPath->addPoint( *it );
}
Lösung
You can do it without iterators. May be you'll find this code more clean:
for(int i = 0; i <= list.count() - 2; i++) {
QgsPoint a = list[i], b = list[i + 1];
//...
}
Andere Tipps
You could use a java-style iterator:
QListIterator<QgsPoint> i(p);
while(i.hasNext())
{
if(!i.hasPrevious())
{
i.next();
continue;
}
m = (i.peekNext().y() - i.peekPrevious().y()) / (i.peekNext().x()-i.peekPrevious().x());
i.next();
}
@itwasntpete's comment is the right answer. But you can do it with one iterator if you want:
for( QList< QgsPoint >::iterator it = p.empty() ? p.begin() - 1 : p.begin(); it + 1 != p.end(); ++it )
{
const float slope = ( it[1].y() - it->y() ) / ( it[1].x() - it->x() );
//Do something with slope here
}