# ofPoint

Need to extend the ofPoint class here is what I got so far:

``````
class ofPoint {
public:

// union allows us to access the coordinates through
// both an array 'v' and 'x', 'y', 'z' member varibles
union  {
struct {
float x;
float y;
float z;
};
float v;
};

ofPoint( float _x=0.0f, float _y=0.0f, float _z=0.0f ) {
x = _x;
y = _y;
z = _z;
}

ofPoint set(float _x, float _y, float _z = 0){
x = _x;
y = _y;
z = _z;

return this;
}

ofPoint set(ofPoint _p) {
x = _p.x;
y = _p.y;
z = _p.z;

return this;
}

ofPoint scale(float _s) {
x *= _s;
y *= _s;
z *= _s;

return this;
}

ofPoint scale(ofPoint _p) {
x *= _p.x;
y *= _p.y;
z *= _p.z;

return this;
}

x += _s;
y += _s;
z += _s;

return this;
}

x += _p.x;
y += _p.y;
z += _p.z;

return this;
}

ofPoint random() {
x = ofRandomf();
y = ofRandomf();
z = ofRandomf();
normalize();

return this;
}

ofPoint normalize() {
float mag = sqrt(x * x + y * y + z * z);
if (mag > 0) {
mag = 1f / mag;
x *= mag;
y *= mag;
z *= mag;
}
return this;
}
};

``````

will post when I am done. I could do it with operators but I think this way is better what do you all think?

ding

you migh want to use ofxPoint3f from the vectormath addon instead Thanks. I had not seen ofxPoint3f. It looks like I was about to redo a bunch of stuff already done. It still looks like I have to add 2 or 3 things for my needs. I will post the changes when I am done and if they look useful maybe they could end up being part of ofxPoint3f.

 actually ofxVec3f is more like what I need.
ding

yes, code improvements are generally most welcome and many have already ended up in the OF releases.

Also a common thing to do whenever you need to add functionality to a class is the following: In an object oriented manner you create a class that derives from an existing one. Then you can “overwrite” or add methods as you like without changing release code.

Say you want a your own Point (or vector) class

``````

class myAwesomePoint : public ofxPoint3f {
public:

void aNewMethod(){}

// overwrite an existing method
void set( float _x, float _y, float _z ){
x = 4325;
y = _y*2342;
z = _z*23423489;
}
}

``````

Then when you use myAwesomePoint all the functionality of ofxPoint3f will be available too. Only set(…) will behave differently.

Also, methods that have ofPoint or ofxPoint3f as a parameter will take you new myAwesomePoint object since it is derived from the other two.

Happy Hacking,

Thanks for your suggestions I’ll keep that in mind for future reference but after looking at ofxVec3f that seems like it has all I need.

Sorry for spamming

ding

stefanix, would the class that’s being extended need to be included in the new class?

for example:

``````

#include "ofxPoint3f.h"

class myAwesomePoint : public ofxPoint3f {
public:

void aNewMethod(){}

// overwrite an existing method
void set( float _x, float _y, float _z ){
x = 4325;
y = _y*2342;
z = _z*23423489;
}
}

``````

I’ve had trouble just using : public className with some classes i’ve built. It’s always wanted me to include the extended class. Am I right or am I missing something?