tags:

views:

78

answers:

2

I have the following functor for redefining the less operator of the sort method for the vector type:

typedef vector< vector<int> > MxInt2d;
typedef vector< vector<double> > MxDouble2d;

class QSweep{   
public:
....
static MxDouble2d myPoints_;
    MxDouble2d myEdges_;

class order{
public:
 bool operator() (const vector<int>& edge1, const vector<int>& edge2){
  //std::cout<<"inside sort"<<endl;
  //3 sort criteria
  return (myPoints_[edge1[0]][0]<myPoints_[edge2[0]][0])|| 
                           (myPoints_[edge1[0]][0]==myPoints_[edge2[0]][0]&& 
                            myPoints_[edge1[0]][1]<myPoints_[edge2[0]][1]) 
         ||
          (myPoints_[edge1[0]][0]==myPoints_[edge2[0]][0]&& 
                             myPoints_[edge1[0]][1]==myPoints_[edge2[0]][1]&& 
           getSlope(myPoints_[edge1[0]][0],myPoints_[edge1[0][1],  
                                      myPoints_[edge1[1]][0],myPoints_[edge1[1]][0])
           <
                             getSlope(myPoints_[edge2[0][0],myPoints_[edge2[0][1],    
                                      myPoints_[edge2[1]][0],myPoints_[edge2[1]][0]));
   }
};
static double getSlope(double a, double b, double c, double d);

};

where getSlope is a function defined in the following way:

double QSweep::getSlope(double a, double b, double c, double d){
double slope=0.0;
//std::cout<<"slope criteria"<<endl;
double denum=c-a;
if (denum==0){
 std::cout<<"zero denominator"<<endl;
}
else{
 slope=(d-b)/denum;
}
return slope;
}

Each point is given by an index, an x, and a y coordinate; Each edge is given by source-edge[0] and destination-edge[1],where edge[0],edge[1] are indexes of the points). I want to arrange the edge: - by the x coordinate of their edge[0] coordinate (iff the x's of 2 edges are different) - by the y coordinate of their edge[0] coordinate (iff the x's of 2 edges are equal) - by their corresponding slopes (iff the x's of edges and the y's of edges are equal).

I declared the getSLope function static, but when I sort the edges with

    sort(myEdges_.begin(),myEdges_.end(),order());

the last condition is not fullfilled. if I have two edges with the same x's and y's for edgep[0] but with different slopes, e.g. slope(edge1)=1 ,slope(edge2)=1/2 than I would want to get [edge2, edge1]; instead I get [edge1,edge2]. So my getSlope criteria is not computed.

is it because I declared getSlope static? what should I change so that the criteria is fulfilled? thank you in advance for your suggestions, madalina

A: 

Maybe it happened because expression result could be calculated without it.
E.g.:

false && someFunction(); - function will not be called
true || someFunction(); - function will not be called

This is nice rule, it allow us write next:

if ( ptr && ptr->someMethod() ) 
// call method will not be called for NULL pointers
bb
madalina
check this with debugger or printing parts of equation. if yes - someFunction()<someFunction() - will not be equated.
bb
+1  A: 

crikey! It looks quite complex, firstly have you tried with a less complex set of values to compute - ie I see you have cout statements, I trust that they work correctly when you run it?

If so, can iut be that your inputs to getSlope are wrong - shouldn't

getSlope(myPoints_[edge2[0][0],myPoints_[edge2[0][1],    
         myPoints_[edge2[1]][0],myPoints_[edge2[1]][0])

be

getSlope(myPoints_[edge2[0][0],myPoints_[edge2[0][1],    
         myPoints_[edge2[1]][0],myPoints_[edge2[1]][1])
gbjbaanb
YES!! it was this wrong index for edge2[1]. Thanks.yepp it looks quite complex.
madalina
my pleasure. now gimme a point, gimme a point, :)
gbjbaanb