ofVectorMath interpolated

I recently needed to use this little function for something I am doing and I found it very useful. It would be nice if it was integrated into all of the ofVectorMath stuff.

  
/**  
	 * Interpolates the vector towards the given target vector,  
	 * using linear interpolation  
	 *   
	 * @param vec  
	 *            target vector  
	 * @param f  
	 *            interpolation factor (should be in the range 0..1)  
	 */  
	ofxVec3f& interpolated(const ofxVec3f& vec, float f) {  
		x += (vec.x - x) * f;  
		y += (vec.y - y) * f;  
		z += (vec.z - z) * f;  
		  
		return * this;  
	}  
  

Maybe it could have a check to make sure it stayed between 0-1 like:

  
if (f > 1) f = 1;  
if (f < 0) f = 0;  

ding

If you are trying to interpolate between two locations you might want to use points.
In ofxPoint3f you will also find the following lerp method. ofxVectorMath lets you get away with using vectors for everything (got to quick hack sometimes) but it really works best when using points and vectors in a mathematical way. Points are locations, vectors are directions with magnitude. At least that was the idea.

  
  
    // Linear interpolation.  
    //  
    //  
    /**  
    * p==0.0 results in this point, p==0.5 results in the  
    * midpoint, and p==1.0 results in pnt being returned.  
    */  
    ofxPoint3f interpolated( const ofxPoint3f& pnt, float p ) const {  
        return ofxPoint3f( x*(1-p) + pnt.x*p,  
                          y*(1-p) + pnt.y*p,  
                          z*(1-p) + pnt.z*p );  
    }  
  
    ofxPoint3f& interpolate( const ofxPoint3f& pnt, float p ) {  
        x = x*(1-p) + pnt.x*p;  
        y = y*(1-p) + pnt.y*p;  
        z = z*(1-p) + pnt.z*p;  
        return *this;  
    }  
  
    ofxPoint3f middled( const ofxPoint3f& pnt ) const {  
        return ofxPoint3f( (x+pnt.x)/2.0, (y+pnt.y)/2.0, (z+pnt.z)/2.0 );  
    }  
  
    ofxPoint3f& middle( const ofxPoint3f& pnt ) {  
        x = (x+pnt.x)/2.0;  
        y = (y+pnt.y)/2.0;  
        z = (z+pnt.z)/2.0;  
        return *this;  
    }  
  

I think I understand the reasoning. But for what I was doing ofVec3f had all the functions I needed and so I opted for that. That interpolation stuff was the only thing that was missing from ofVec3f for my immediate use. I didn’t notice that ofPoint3f had that in it. For example I needed to use rescaled & normalized which ofPoint3f didn’t have. I bet if I looked at my code I could probably try to separate the two and just use ofVec3f for some things and ofPoint3f for others but I think (had I known about ofPoint3f) that I would have chosen the path of less resistance and just hacked ofVec3f for that functionality in the end.

Thanks,

ding