Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You had a number of things wrong with your operators, although most of them weren't used. I modified your class. I notice you have not shown your code for the <code>getx()</code> (etc) members. I implemented those inline, but perhaps they were responsible for the garbage? Here is the test program with my changes. It seems to work fine:</p> <pre><code>class vector3 { public: vector3::vector3() :x(0), y(0), z(0) {} vector3::~vector3() {} vector3(float inx,float iny,float inz) : x(inx), y(iny), z(inz) {} //Vector operators _inline vector3&amp; operator=(const vector3&amp; invec) { // Assignment x = invec.x; y = invec.y; z = invec.z; return *this; } _inline vector3 operator+(const vector3&amp; invec) const {//Addition return vector3(x+invec.x,y+invec.y,z+invec.z); } _inline vector3 operator-(const vector3&amp; invec) const {//Subtraction return vector3(x-invec.x,y-invec.y,z-invec.z); } _inline vector3 operator*(const vector3&amp; invec) const {//Multiplication return vector3(x*invec.x,y*invec.y,z*invec.z); } _inline vector3 operator/(const vector3&amp; invec) const {//Division return vector3(x/invec.x,y/invec.y,z/invec.z); } //scalar operators _inline vector3&amp; operator+=(const float&amp; scalar){//Addition self-assignment x+=scalar,y+=scalar,z+=scalar; return *this; } _inline vector3&amp; operator-=(const float&amp; scalar){//Subtraction self-assignment x-=scalar,y-=scalar,z-=scalar; return *this; } _inline vector3&amp; operator*=(const float&amp; scalar){//Multiplication self-assignment x*=scalar,y*=scalar,z*=scalar; return *this; } _inline vector3 operator*(const float&amp; scalar) const { return vector3(x*scalar, y*scalar, z*scalar); } //Math methods _inline vector3 operator^(const vector3&amp; invec) const {//Cross product return vector3( (y*invec.z-z*invec.y), (z*invec.x-x*invec.z), (x*invec.y-y*invec.x)); } _inline float operator&amp;(const vector3&amp; invec) const {//Dot product return (x*invec.x)+(y*invec.y)+(z*invec.z); } _inline float distance(vector3&amp;) const;//Distance _inline void normalize(){ float mag = sqrtf(x*x+y*y+z*z); x/=mag; y/=mag; z/=mag; } float x; float y; float z; _inline float getx() const { return x; } _inline float gety() const { return y; } _inline float getz() const { return z; } float getMagnitude() const; }; _inline vector3 createCatmulRomSpline(vector3 P0, vector3 P1, vector3 P2, vector3 P3, float t) { float t2 = t*t; float t3 = t2*t; vector3 result = ((P1*2) + (P2 - P0) * t + (P0*2 - P1 * 5 + P2*4 - P3)*t2 + (P1*3 - P0- P2*3 + P3) * t3)*0.5f; return result; } int main() { vector3 vectorOne(0,0,0); vector3 vectorTwo(5,10,1); vector3 vectorThree(10,10,2); vector3 vectorFour(15,0,3); const int ndiv = 10; for( int i = 0; i &lt;= ndiv; i++ ) { float t = (float)i / ndiv; vector3 SplineVector = createCatmulRomSpline(vectorOne, vectorTwo, vectorThree, vectorFour, t); cout &lt;&lt; "curve pos X: " &lt;&lt; SplineVector.getx() &lt;&lt; "\n"; cout &lt;&lt; "curve pos Y: " &lt;&lt; SplineVector.gety() &lt;&lt; "\n"; cout &lt;&lt; "curve pos Z: " &lt;&lt; SplineVector.getz() &lt;&lt; "\n\n"; } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload