ofVec3f Comparison Issues

I have a question regarding binary operators- greater than, less than. I am not able to compare two ofPoints unless I do ofPoint1.X compared to ofPoint2.X, and have the same problem with GLM Vec2 comparisons. Is there no quick way of handling this apart from comparing individual elements in this structure?

Hmm.

I am not sure how you would define one being less than the other? Would it be the sum of X Y and Z? Would it be the length / magnitude of the vector?

You can define a custom sorting function though for a vector <ofVec3f> to decide how to compare:

static bool sortMyVecs( const ofVec3f & A, const ofVec3f & B){
    return A.lengthSquared() > B.lengthSquared(); // if you want the larger ofVec3fs at the beginning 
}

vector <ofVec3fs> myVecs; 

//once you have a bunch of ofVec3fs added to myVecs
ofSort(myVecs, sortMyVecs); 

You could also inherit the class and override the the < and > operators to define how they should work if you just want to do < or > comparisons.

1 Like

Thanks for the response.

I did not pay attention to mathematical logic, while both writing the code and asking this question

What I wanted is to determine if a set of coordinates was within (0, 0) and (ofGetWidth( )/4, ofGetHeight( )/4)

And I tried to do this by saying-

if(glm::vec2 myVec > zeroPoint && myVec < quarterPoint){foo+=bar)

Of course now I realize there is no comparing coordinates!!

Like you suggested I could compare lengths(squared)

Thanks

This is a really clever idea!! thanks I will try this

Ahh. Well a very easy way to do that is:

  ofRectangle checkRect(0, 0, ofGetWidth( )/4, ofGetHeight( )/4);
  if( checkRect.inside(myVec.x, myVec.y) ){
     //do something here 
  }

Hope that helps!
Theo

This is a perfect solution! Thank you.