Hello,
I'm currently programming, as a sub-project, a voronoi diagram generator. In short, I roll a set of points stored as
Code:
std:: vector <std:: complex <float>> vertices
and then generate the Voronoi diagram accordingly. I've got the algorithm more or less down.
My question is: how much more effective is it to use
Code:
std:: vector< std :: complex <float> > :: iterator
opposed to using a simple
Code:
for (int k=0; k < vertices. size; ++ k) { ...
the reason I ask is that I cannot do specific things with the vector iterator that I can do with the more classic
Code:
vertices [k]. something
approach.
The following shows my two approaches, the first uses the vector iterator and the second code snippet uses a simple for loop.
(note: this is just the code for initializing the vertices as complex points on the plane.)
Code:
void Voronoi::InitVertices() {
for ( int k = 0; k < vertices.size(); ++k ) {
std::complex<float> dump( 0.f, 0.f );
vertices.push_back( dump );
}
for ( std::vector<std::complex<float>>::iterator i = vertices.begin(); i != vertices.end(); ++i ) {
bool checker = true;
while ( checker ) {
checker = false;
for ( std::vector<std::complex<float>>::iterator j = vertices.begin(); j != vertices.end(); ++j ) {
if ( ( point_distance( &(*i), &(*j) ) < MINIMUM_DIST ) && ( &(*i) != &(*j) ) ) {
(*i).real( std::rand() % RMW );
(*i).imag( std::rand() % RMH );
PrintPt( &(*i), true );
checker = true;
break;
}
}
}
}
}
Code:
void Voronoi::InitVertices2() {
for ( int k = 0; k < vertices.size(); ++k ) {
std::complex<float> dump(0.f,0.f);
vertices.push_back( dump );
}
for ( int k = 0; k < vertices.size(); ++k ) {
//for ( std::vector<std::complex<float>>::iterator i = vertices.begin(); i != vertices.end(); ++i ) {
bool checker = true;
while ( checker ) {
checker = false;
for ( int j = 0; j < vertices.size();++j ) {
//for ( std::vector<std::complex<float>>::iterator j = vertices.begin(); j != vertices.end(); ++j ) {
if ( ( point_distance( &vertices[k], &vertices[j]) < MINIMUM_DIST ) && ( (*k) != (*j) ) ) {
vertices[k].real( std::rand() % RMW );
vertices[k].imag( std::rand() % RMH );
PrintPt( &vertices[k], true );
checker = true;
break;
}
}
}
}
}
Now to the differences:
1) I cannot compare two
Code:
std:: vector <std:: complex<float>> iterator
with each other directly. My work-around is to instead check if the two iterators point to the same address, which is OK I guess but not something I particularly like.
2) When I do compare two vector iterators though, if the two std:: complex being compared have the same coordinates, lets say (0,0) then they will be seen as the same point. (which is not always wanted. Sometimes I need to differentiate between std:: complex's even if they have the same coordinates).
So, how bad is it if I use the normal for loop? Is there a better option for me?
to space,
M.