Jump to content
  • Advertisement
Sign in to follow this  
turlisk

[c++] Matrix Scale & translation

This topic is 2704 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Just a warning that this is for a class and is a lab for homework. I'm not expecting an answer but some help for going the right direction would be awesome. I have a class I've created called Vector3D it normally takes in a 3D vector (X, Y, and Z). However we are now working with 4D vectors (X, Y, Z, and W) and I'm modifying the code to use array's of vectors to create a matrix and translate it.

I can get the array to carry over, i can modify the array and set it to a new array after the translation. However when it returns the array it doesn't modify anything. I'm not sure why this happening. I have my main cpp file and the functions I'm using from my Vector3D class.

main.cpp

void main() { Vector3D* matrix; Vector3D* resultMatrix; int verticies; int vec; int vert = 1; float tmpX; float tmpY; float tmpZ; float tmpW; int choice = 0; bool goOn = true; bool validChoice = false; char userInput; cout << "Enter the amount of verticies for the object: "; cin >> verticies; matrix = new Vector3D[verticies]; resultMatrix = new Vector3D[verticies]; for (int i = 0; i < verticies; i++) { cout << "Please enter the X, Y, Z and W coordinates for the " << verticies << "X4 matrix:" << endl; cout << vert <<": X: "; vert++; cin >> tmpX; cout << " Y: "; cin >> tmpY; cout << " Z: "; cin >> tmpZ; cout << " W: "; cin >> tmpW; matrix.set4DVector(tmpX, tmpY, tmpZ, tmpW); } cout << "Enter the Translation you want to do "; cout << "X translation: "; cin >> tmpX; cout << "Y translation: "; cin >> tmpY; cout << "Z translation: "; cin >> tmpZ; cout << "W translation: "; cin >> tmpW; for (int i=0 ; i < verticies; i++) { resultMatrix.translation(&matrix, tmpX, tmpY, tmpZ, tmpW, verticies); } cout << "The new vertices are: " << endl; for (int i = 0; i < verticies; i++) { cout << vec << ": "; vec++; cout << "X: " << matrix.getX(); cout << " Y: " << matrix.getY(); cout << " Z: " << matrix.getZ(); cout << " W: " << matrix.getW(); cout << endl; }


Vector3D.h

#ifndef Vector3D_H #define Vector3D_H #include <cmath> #include <iostream> using namespace std; class Vector3D { public: void set4DVector(float theX, float theY, float theZ, float theW) { vecX = theX; vecY = theY; vecZ = theZ; vecW = theW; } // Returns the X value as a float float getX() { return vecX; } // Returns the Y value as a float float getY() { return vecY; } // Returns the Z value as a float float getZ() { return vecZ; } // Returns the W value as a float float getW() { return vecW; } float dotProduct(Vector3D a, Vector3D b) // * { return ((a.getX() * b.getX()) + (a.getY() * b.getY()) + (a.getZ() * b.getZ()) + (a.getW() * b.getW())); } void* translation(Vector3D a[], float theX, float theY, float theZ, float theW, int vert) { Vector3D * t; t = new Vector3D [3]; t[0].set4DVector(1, 0, 0, theX); t[1].set4DVector(0, 1, 0, theY); t[2].set4DVector(0, 0, 1, theZ); t[3].set4DVector(0, 0, 0, 1); float newX= 1 , newY = 1, newZ = 1; int verticies = vert; Vector3D* result; // resulting vector returns to stack result = new Vector3D[verticies]; for (int i = 0; i < verticies; i++) { newX = newX + (t[0].dotProduct(t[0], a)); newY = newY + (t[1].dotProduct(t[1], a)); newZ = newZ + (t[2].dotProduct(t[2], a)); } for (int i = 0; i < verticies; i++) { result.set4DVector(a.getX() + newX,a.getY() + newY, a.getZ() + newZ, 1 ); } return result; } private: float vecX, vecY, vecZ, vecW; // the different X, Y, and Z coordinates of the Vector. float tempDivideTop; // top value in any division float tempDivideBottom; // bottom value used to catch divide by 0 error }; #endif


Any help would be great.

Share this post


Link to post
Share on other sites
Advertisement

Is the code formatted correctly for you? The newlines seem to be missing here (Safari).


Same for me with both Firefox 4.0 and IE 8 on Vista.

Share this post


Link to post
Share on other sites
Before getting to the heart of the matter:


  • Your code has lots of memory leaks. Remember to delete what you new and delete[] what you new[], or use automatic storage ("the stack") and standard containers more. IMHO, this is a very important point so make sure to read up a little about these things.
  • main() should return an int, really
  • "using namespace std;" in a header is considered bad practice because it has adverse effects on clients of the header
  • if you're learned about constructors, consider using one instead of having a set4DVector method
  • A #include "Vector3D.h" is missing from the top of main.cpp
  • Calling a 4D vector "Vector3D" is going to confuse a lot of people -- including you! At the start of translation(), there's a '3' which should be a '4'.
  • Why does translation() return a void* (as opposed to a Vector3D*) ?


    As for your specific question: think very carefully about the inputs and outputs of Vector3D's translation() method. What does it return and what are you doing with that returned data? What should you be doing with it?

    I suspect you'll run in to other issues with the code once this is sorted, but first things first :)

    For others, here's my attempt at fixing the formatting:


    void main()
    {
    Vector3D* matrix;
    Vector3D* resultMatrix;
    int verticies;
    int vec;
    int vert = 1;
    float tmpX;
    float tmpY;
    float tmpZ;
    float tmpW;
    int choice = 0;
    bool goOn = true;
    bool validChoice = false;
    char userInput;

    cout << "Enter the amount of verticies for the object: ";
    cin >> verticies;

    matrix = new Vector3D[verticies];
    resultMatrix = new Vector3D[verticies];

    for (int i = 0; i < verticies; i++)
    {
    cout << "Please enter the X, Y, Z and W coordinates for the " << verticies << "X4 matrix:" << endl;
    cout << vert <<": X: ";
    vert++;
    cin >> tmpX;
    cout << " Y: ";
    cin >> tmpY;
    cout << " Z: ";
    cin >> tmpZ;
    cout << " W: ";
    cin >> tmpW;
    matrix.set4DVector(tmpX, tmpY, tmpZ, tmpW);
    }

    cout << "Enter the Translation you want to do ";
    cout << "X translation: ";
    cin >> tmpX;
    cout << "Y translation: ";
    cin >> tmpY;
    cout << "Z translation: ";
    cin >> tmpZ;
    cout << "W translation: ";
    cin >> tmpW;

    for (int i=0 ; i < verticies; i++)
    {
    resultMatrix.translation(&matrix, tmpX, tmpY, tmpZ, tmpW, verticies);
    }

    cout << "The new vertices are: " << endl;

    for (int i = 0; i < verticies; i++)
    {
    cout << vec << ": ";
    vec++;
    cout << "X: " << matrix.getX();
    cout << " Y: " << matrix.getY();
    cout << " Z: " << matrix.getZ();
    cout << " W: " << matrix.getW();
    cout << endl;
    }

    }

    The final '}' was missing from the above, so there may be more missing too.


    #ifndef Vector3D_H
    #define Vector3D_H
    #include <cmath>
    #include <iostream>

    using namespace std;

    class Vector3D
    {
    public:
    void set4DVector(float theX, float theY, float theZ, float theW)
    {
    vecX = theX;
    vecY = theY;
    vecZ = theZ;
    vecW = theW;
    }

    // Returns the X value as a float
    float getX()
    {
    return vecX;
    }

    // Returns the Y value as a float
    float getY()
    {
    return vecY;
    }

    // Returns the Z value as a float
    float getZ()
    {
    return vecZ;
    } // Returns the W value as a float

    float getW()
    {
    return vecW;
    }

    float dotProduct(Vector3D a, Vector3D b) // *
    {
    return ((a.getX() * b.getX()) + (a.getY() * b.getY()) + (a.getZ() * b.getZ()) + (a.getW() * b.getW()));
    }

    void* translation(Vector3D a[], float theX, float theY, float theZ, float theW, int vert)
    {
    Vector3D * t;
    t = new Vector3D [3];
    t[0].set4DVector(1, 0, 0, theX);
    t[1].set4DVector(0, 1, 0, theY);
    t[2].set4DVector(0, 0, 1, theZ);
    t[3].set4DVector(0, 0, 0, 1);
    float newX= 1 , newY = 1, newZ = 1;
    int verticies = vert;
    Vector3D* result; // resulting vector returns to stack
    result = new Vector3D[verticies];

    for (int i = 0; i < verticies; i++)
    {
    newX = newX + (t[0].dotProduct(t[0], a));
    newY = newY + (t[1].dotProduct(t[1], a));
    newZ = newZ + (t[2].dotProduct(t[2], a));
    }

    for (int i = 0; i < verticies; i++)
    {
    result.set4DVector(a.getX() + newX,a.getY() + newY, a.getZ() + newZ, 1 );
    }

    return result;
    }

    private:
    float vecX, vecY, vecZ, vecW; // the different X, Y, and Z coordinates of the Vector.
    float tempDivideTop; // top value in any division
    float tempDivideBottom; // bottom value used to catch divide by 0 error
    };

    #endif

Share this post


Link to post
Share on other sites

Before getting to the heart of the matter:

  • Your code has lots of memory leaks. Remember to delete what you new and delete[] what you new[], or use automatic storage ("the stack") and standard containers more. IMHO, this is a very important point so make sure to read up a little about these things.
  • main() should return an int, really
  • "using namespace std;" in a header is considered bad practice because it has adverse effects on clients of the header
  • if you're learned about constructors, consider using one instead of having a set4DVector method
  • A #include "Vector3D.h" is missing from the top of main.cpp
  • Calling a 4D vector "Vector3D" is going to confuse a lot of people -- including you! At the start of translation(), there's a '3' which should be a '4'.
  • Why does translation() return a void* (as opposed to a Vector3D*) ?

    As for your specific question: think very carefully about the inputs and outputs of Vector3D's translation() method. What does it return and what are you doing with that returned data? What should you be doing with it?

    I suspect you'll run in to other issues with the code once this is sorted, but first things first :)

    For others, here's my attempt at fixing the formatting:


    void main()
    {
    Vector3D* matrix;
    Vector3D* resultMatrix;
    int verticies;
    int vec;
    int vert = 1;
    float tmpX;
    float tmpY;
    float tmpZ;
    float tmpW;
    int choice = 0;
    bool goOn = true;
    bool validChoice = false;
    char userInput;

    cout << "Enter the amount of verticies for the object: ";
    cin >> verticies;

    matrix = new Vector3D[verticies];
    resultMatrix = new Vector3D[verticies];

    for (int i = 0; i < verticies; i++)
    {
    cout << "Please enter the X, Y, Z and W coordinates for the " << verticies << "X4 matrix:" << endl;
    cout << vert <<": X: ";
    vert++;
    cin >> tmpX;
    cout << " Y: ";
    cin >> tmpY;
    cout << " Z: ";
    cin >> tmpZ;
    cout << " W: ";
    cin >> tmpW;
    matrix.set4DVector(tmpX, tmpY, tmpZ, tmpW);
    }

    cout << "Enter the Translation you want to do ";
    cout << "X translation: ";
    cin >> tmpX;
    cout << "Y translation: ";
    cin >> tmpY;
    cout << "Z translation: ";
    cin >> tmpZ;
    cout << "W translation: ";
    cin >> tmpW;

    for (int i=0 ; i < verticies; i++)
    {
    resultMatrix.translation(&matrix, tmpX, tmpY, tmpZ, tmpW, verticies);
    }

    cout << "The new vertices are: " << endl;

    for (int i = 0; i < verticies; i++)
    {
    cout << vec << ": ";
    vec++;
    cout << "X: " << matrix.getX();
    cout << " Y: " << matrix.getY();
    cout << " Z: " << matrix.getZ();
    cout << " W: " << matrix.getW();
    cout << endl;
    }

    }

    The final '}' was missing from the above, so there may be more missing too.


    #ifndef Vector3D_H
    #define Vector3D_H
    #include <cmath>
    #include <iostream>

    using namespace std;

    class Vector3D
    {
    public:
    void set4DVector(float theX, float theY, float theZ, float theW)
    {
    vecX = theX;
    vecY = theY;
    vecZ = theZ;
    vecW = theW;
    }

    // Returns the X value as a float
    float getX()
    {
    return vecX;
    }

    // Returns the Y value as a float
    float getY()
    {
    return vecY;
    }

    // Returns the Z value as a float
    float getZ()
    {
    return vecZ;
    } // Returns the W value as a float

    float getW()
    {
    return vecW;
    }

    float dotProduct(Vector3D a, Vector3D b) // *
    {
    return ((a.getX() * b.getX()) + (a.getY() * b.getY()) + (a.getZ() * b.getZ()) + (a.getW() * b.getW()));
    }

    void* translation(Vector3D a[], float theX, float theY, float theZ, float theW, int vert)
    {
    Vector3D * t;
    t = new Vector3D [3];
    t[0].set4DVector(1, 0, 0, theX);
    t[1].set4DVector(0, 1, 0, theY);
    t[2].set4DVector(0, 0, 1, theZ);
    t[3].set4DVector(0, 0, 0, 1);
    float newX= 1 , newY = 1, newZ = 1;
    int verticies = vert;
    Vector3D* result; // resulting vector returns to stack
    result = new Vector3D[verticies];

    for (int i = 0; i < verticies; i++)
    {
    newX = newX + (t[0].dotProduct(t[0], a));
    newY = newY + (t[1].dotProduct(t[1], a));
    newZ = newZ + (t[2].dotProduct(t[2], a));
    }

    for (int i = 0; i < verticies; i++)
    {
    result.set4DVector(a.getX() + newX,a.getY() + newY, a.getZ() + newZ, 1 );
    }

    return result;
    }

    private:
    float vecX, vecY, vecZ, vecW; // the different X, Y, and Z coordinates of the Vector.
    float tempDivideTop; // top value in any division
    float tempDivideBottom; // bottom value used to catch divide by 0 error
    };

    #endif




Thanks Edd,

I'll just post the entire main and vector3d class so you can see what we are working with (those were just snippets of code) so i probably just missed the syntax on my part from not copying and pasting correctly.

Vector3D.h

/**************************************************************************************
* Our Vector class, this will take user input and convert it into a 2D or 3D vector.
* We then take that 2D or 3D vector and by using mathematical formulas obtain the
* Euler Angles and Magnitude, Heading and pitch. We can also input the Magnitude,
* Heading and Pitch, and convert those to X, Y and Z values.
***************************************************************************************/
#ifndef Vector3D_H
#define Vector3D_H
#include <cmath>
#include <iostream>

using namespace std;

class Vector3D
{
public:

// The default constructor of the vector, here we use the values of 0,0,0, and 1
Vector3D(){
vecX = 0;
vecY = 0;
vecZ = 0;
vecW = 1;
}

// setRectGivenRect3D will take in three floats X, Y, and Z from the user
// and set those floats to the private variables in the vector class.
void setRectGivenRect3D(float theX, float theY, float theZ)
{
vecX = theX;
vecY = theY;
vecZ = theZ;
}

// setRectGivenRect2D will take in two floats X, and Y, from the user and
// set those floats to the private variables in the vector class.
void setRectGivenRect2D(float theX, float theY)
{
vecX = theX;
vecY = theY;
vecZ = 0;
}

// setRectGivenPolar will take in two floats Magnitude and Heading from the
// user and then convert those from radians to degrees and set the X and Y
// of the vector accordingly.
void setRectGivenPolar(float magnitude, float heading)
{
vecX = magnitude * (cos(0.0 * 0.01745329) * cos(heading * 0.01745329));
vecY = magnitude * (cos(0.0 * 0.01745329) * sin(heading* 0.01745329));

}

// setRectGivenMagHeadPitch will take in three floats Magnitude, Heading and Pitch
// from the user and then convert those from radians to degrees and set the
// X, Y and Z of the vector accordingly.
void setRectGivenMagHeadPitch(float magnitude, float heading, float pitch)
{
vecX = magnitude * (cos(pitch * 0.01745329) * cos(heading * 0.01745329));
vecY = magnitude * (cos(pitch * 0.01745329) * sin(heading * 0.01745329));
vecZ = magnitude * (sin(pitch * 0.01745329));
}

void set4DVector(float theX, float theY, float theZ, float theW)
{
vecX = theX;
vecY = theY;
vecZ = theZ;
vecW = theW;
}

// printRect prints the X, Y, and Z float values by using class functions
// of getX, getY, and getZ. The values are printed via console output.
void printRect()
{
cout << "The X coordinate is: " << getX() << endl;
cout << "The Y coordinate is: " << getY() << endl;
cout << "The Z coordinate is: " << getZ() << endl;

}

// printMagHeadPitch prints the Magnitude, Heading, and Pitch float values
// by using class functions of getMagnitude, getHeading, and getPitch.
// The values are printed via console output.
void printMagHeadPitch()
{
float rad2deg = 57.29577; // the conversion between radians and degrees
float deg2rad = 0.01745329; // the conversion between degrees and radians
cout << "The Magnitude is: " << getMagnitude() << endl;
cout << "The Heading is: " << getHeading() * rad2deg << endl;
cout << "The Pitch is: " << getPitch() * rad2deg << endl;
}

// printAngles prints the Alpha, Beta, and Gamma angle float values by using
// class functions of getAlpha, getBeta, and getGamma. The values are
// printed via console output.
void printAngles()
{
float rad2deg = 57.29577; // the conversion between radians and degrees
float deg2rad = 0.01745329; // the conversion between degrees and radians
cout << "The Alpha angle is: " << getAlpha() * rad2deg << endl;
cout << "The Beta angle is: " << getBeta() * rad2deg<< endl;
cout << "The Gamma angle is: " << getGamma() * rad2deg<< endl;

}

// getMagnitude returns the float answer of the mathematical equation used to
// obtain the Magnitude.
float getMagnitude()
{
return sqrt(pow(getX(), 2)+ pow(getY(), 2) + pow(getZ(), 2));
}

// getPitch returns the float answer of the mathematical equation used to
// obtain the Pitch. A catch has to be created for divide by 0 errors.
float getPitch()
{
tempDivideTop = getZ();
tempDivideBottom = getMagnitude();
if (tempDivideBottom == 0)
{
return 0;
}
return asin(tempDivideTop / tempDivideBottom);
}

// getHeading returns the float answer of the mathematical equation used to
// obtain the Heading. A catch has to be created for divide by 0 errors.
// The answer is also converted from Radians to Degrees.
float getHeading()
{
tempDivideTop = getX();
tempDivideBottom = sqrt(pow(getX(), 2) + pow(getY(), 2));
if (tempDivideBottom == 0)
{
return 0;
}
else
{

if ((acos(tempDivideTop / tempDivideBottom) >= 0) && (this->getY() <= 0 ))
{
return (360 * 0.01745329) - acos(tempDivideTop / tempDivideBottom);
}
else
{
return acos(tempDivideTop / tempDivideBottom);
}

}
}


// getAlpha returns the float answer of the mathematical equation used to
// obtain the Alpha angle. A catch has to be created for divide by 0 errors.
float getAlpha()
{
if (getMagnitude() == 0)
{
return 0;
}
else
{
return acos(getX() / getMagnitude());
}
}

// getBeta returns the float answer of the mathematical equation used to
// obtain the Beta angle. A catch has to be created for divide by 0 errors.
float getBeta()
{
if (getMagnitude() == 0)
{
return 0;
}
else
{
return acos(getY() / getMagnitude());
}
}

// getGamma returns the float answer of the mathematical equation used to
// obtain the Gamma angle. A catch has to be created for divide by 0 errors.
float getGamma()
{
if (getMagnitude() == 0)
{
return 0;
}
else
{
return acos(getZ() / getMagnitude());
}
}

// Returns the X value as a float
float getX()
{
return vecX;
}

// Returns the Y value as a float
float getY()
{
return vecY;
}

// Returns the Z value as a float
float getZ()
{
return vecZ;
}

// Returns the W value as a float
float getW()
{
return vecW;
}

// This will take two vectors and add them together
Vector3D Vector3D::operator+(Vector3D a) // +
{
Vector3D tempVec; // the temporary vector
tempVec.setRectGivenRect3D(a.getX() + this->getX(), a.getY() + this->getY(), a.getZ() + this->getZ());
return tempVec;
}

// this will take two vecotrs and subtract them
Vector3D Vector3D::operator-(Vector3D a) // -
{
Vector3D tempVec; // the temporary vector
tempVec.setRectGivenRect3D(this->getX()- a.getX(), this->getY() - a.getY(), this->getZ() - a.getZ());
return tempVec;
}

// this will take a number and multiply each value of the vector by that number
Vector3D Vector3D::operator&(float scaler) // &
{
Vector3D tempVec; // the temporary vector
tempVec.setRectGivenRect3D(scaler * this->getX(), scaler * this->getY(), scaler * this->getZ());
return tempVec;
}

// This will normalize the vector to 1/magnitude * each value of the vector
Vector3D Vector3D::operator!() // !
{
float normalizer = 1/this->getMagnitude();
Vector3D tempVec; // the temporary vector
tempVec.setRectGivenRect3D(this->getX() * normalizer, this->getY() * normalizer, this->getZ() * normalizer);
return tempVec;
}

// This will return the dot product of the vector which is x*x + y*y + z*z (returns number)
float Vector3D::operator*(Vector3D a) // *
{
return ((a.getX() * this->getX()) + (a.getY() * this->getY()) + (a.getZ() * this->getZ()));
}

// returns the angle between two vectors
float Vector3D::operator%(Vector3D a) // %
{
Vector3D tempVec;
tempVec.setRectGivenRect3D(getX(), getY(), getZ());
return (acos(((a.getX() * tempVec.getX()) + (a.getY() * tempVec.getY()) + (a.getZ() * tempVec.getZ())) / (a.getMagnitude() * tempVec.getMagnitude()))) * 57.29577;
}

// projects the vector perpendicular on to another vector.
Vector3D Vector3D::operator|(Vector3D a) // |
{
float dot, magSquare;
Vector3D tempVec; // The Temporary Vector
dot = (a.getX() * this->getX()) + (a.getY() * this->getY()) + (a.getZ() * this->getZ());
magSquare = this->getMagnitude() * this->getMagnitude();
if (magSquare == 0)
{
tempVec.setRectGivenRect3D(0, 0, 0);
return tempVec;
}
tempVec.setRectGivenRect3D(this->getX() * (dot / magSquare), this->getY() * (dot / magSquare), this->getZ() * (dot / magSquare));
tempVec = a - tempVec;
return tempVec;
}

// parallel projects one vector onto another.
Vector3D Vector3D::operator^(Vector3D a) // ^
{
float dot, magSquare;
Vector3D tempVec; // The Temporary Vector
dot = (a.getX() * this->getX()) + (a.getY() * this->getY()) + (a.getZ() * this->getZ());
magSquare = this->getMagnitude() * this->getMagnitude();
if (magSquare == 0)
{
tempVec.setRectGivenRect3D(0, 0, 0);
return tempVec;
}
tempVec.setRectGivenRect3D(this->getX() * (dot / magSquare), this->getY() * (dot / magSquare), this->getZ() * (dot / magSquare));
return tempVec;
}

// This will obtain the cross product of 2 vectors by multiplying out the X, Y and Z values
Vector3D crossProduct(float p1x, float p1y, float p1z, float p2x, float p2y, float p2z)
{
Vector3D tempVec;
float i;
float j;
float k;

/* i j k
p1x p1y p1z
p2x p2y p2z
*/
i = (p1y * p2z) - (p2y * p1z);
j = -1 * ((p1x * p2z) - (p2x * p1z));
k = (p1x * p2y) - (p2x * p1y);
tempVec.setRectGivenRect3D(i, j, k);
return tempVec;
}

//a dot product operation using all four components
float dotProduct(Vector3D a, Vector3D b) // *
{
return ((a.getX() * b.getX()) + (a.getY() * b.getY()) + (a.getZ() * b.getZ()) + (a.getW() * b.getW()));
}

Vector3D* translation(Vector3D a[], float theX, float theY, float theZ, float theW, int vert)
{
int verticies = vert;
Vector3D t[3];
t[0].set4DVector(1, 0, 0, theX);
t[1].set4DVector(0, 1, 0, theY);
t[2].set4DVector(0, 0, 1, theZ);
t[3].set4DVector(0, 0, 0, 1);
float newX= 1 , newY = 1, newZ = 1;

Vector3D* result; // resulting vector returns to stack

result = new Vector3D[verticies];

for (int i = 0; i < verticies; i++)
{
newX = newX + (dotProduct(t[0], a));
newY = newY + (dotProduct(t[1], a));
newZ = newZ + (dotProduct(t[2], a));
}
for (int i = 0; i < verticies; i++)
{
result.set4DVector(a.getX() + newX,a.getY() + newY, a.getZ() + newZ, 1 );
}
return result;
}

Vector3D* rawScalingMultiply(Vector3D a[], float theX, float theY, float theZ, float theW, int vert)
{
int verticies = vert;
Vector3D* result; // resulting vector returns to stack

result = new Vector3D[verticies];
for (int i = 0; i < verticies; i++)
{
result.set4DVector((a.getX() * theX), (a.getY() * theY), (a.getZ() * theZ), theW);
}
return result;
}

//multiplication by a scaling about a center matrix
Vector3D* centerScaleMutliply(Vector3D a[], float theX, float theY, float theZ, float theW, int vert)
{
int verticies = vert;
Vector3D* result; // resulting vector returns to stack

result = new Vector3D[verticies];
float highX = 0;
float lowX = 0;
float highY = 0;
float lowY = 0;
float highZ = 0;
float lowZ = 0;
float highW = 0;
float lowW = 0;
float centerX = 0;
float centerY = 0;
float centerZ = 0;
float centerW = 0;

// can't figure out how to loop this or nest this better.
for (int i = 0; i < verticies; i++)
{
if (a.getX() > highX)
{
highX = a.getX();
}
if (a.getY() > highY)
{
highY = a.getY();
}
if (a.getZ() > highZ)
{
highZ = a.getZ();
}
if (a.getW() > highW)
{
highW = a.getW();
}
if (a.getX() < lowX)
{
lowX = a.getX();
}
if (a.getY() < lowY)
{
lowY = a.getY();
}
if (a.getZ() < lowZ)
{
lowZ = a.getZ();
}
if (a.getW() < lowW)
{
lowW = a.getW();
}
}

/*
newX=(Xscale)(oldX)+(XCenter)(1-ScaleX)
newY=(Yscale)(oldY)+(YCenter)(1-ScaleY)
newZ=(Zscale)(oldZ)+(ZCenter)(1-ScaleZ)
*/

centerX = (highX - lowX)/2;
centerY = (highY - lowY)/2;
centerZ = (highZ - lowZ)/2;
centerW = (highW - lowW)/2;

for (int i = 0; i < verticies; i++)
{
result.set4DVector((theX * a.getX()) + (centerX * (1-theX)),(theY * a.getY()) + (centerY * (1-theY)), (theZ * a.getZ()) + (centerZ * (1-theZ)), theW);
}

return result;
}
private:
float vecX, vecY, vecZ, vecW; // the different X, Y, and Z coordinates of the Vector.
float tempDivideTop; // top value in any division
float tempDivideBottom; // bottom value used to catch divide by 0 error

};
#endif



main.cpp

#include "Vector3D.h"

void main()
{
Vector3D* matrix;
Vector3D* resultMatrix;
int verticies;
int vec;
int vert = 1;
float tmpX;
float tmpY;
float tmpZ;
float tmpW;
int choice = 0;
bool goOn = true;
bool validChoice = false;
char userInput;

cout << "Enter the amount of verticies for the object: ";
cin >> verticies;
matrix = new Vector3D[verticies];
resultMatrix = new Vector3D[verticies];

for (int i = 0; i < verticies; i++)
{
cout << "Please enter the X, Y, Z and W coordinates for the " << verticies << "X4 matrix:" << endl;
cout << vert <<": X: ";
vert++;
cin >> tmpX;
cout << " Y: ";
cin >> tmpY;
cout << " Z: ";
cin >> tmpZ;
cout << " W: ";
cin >> tmpW;
matrix.set4DVector(tmpX, tmpY, tmpZ, tmpW);
}

while (goOn == true)
{
cout << "Enter the type of transformation (1: translation, 2: raw scaling, 3: scaling about a center): ";
cin >> choice;
while (validChoice == false)
{
if (choice < 1)
{
validChoice = false;
cout << "Enter the type of transformation (1: translation, 2: raw scaling, 3: scaling about a center): ";
cin >> choice;
}
if (choice > 3)
{
validChoice = false;
cout << "Enter the type of transformation (1: translation, 2: raw scaling, 3: scaling about a center): ";
cin >> choice;
}
else
{
validChoice = true;
}
}
switch (choice)
{
case 1:
{
cout << "Enter the Translation you want to do ";
cout << "X translation: ";
cin >> tmpX;
cout << "Y translation: ";
cin >> tmpY;
cout << "Z translation: ";
cin >> tmpZ;
cout << "W translation: ";
cin >> tmpW;
for (int i=0 ; i < verticies; i++)
{
resultMatrix.translation(&matrix, tmpX, tmpY, tmpZ, tmpW, verticies);
}
vec = 1;
break;
}
case 2:
{
cout << "Enter the Raw Scaling you want to do ";
cout << "X Scale: ";
cin >> tmpX;
cout << "Y Scale: ";
cin >> tmpY;
cout << "Z Scale: ";
cin >> tmpZ;
cout << "W Scale: ";
cin >> tmpW;
for (int i = 0; i< verticies; i++)
{
resultMatrix = resultMatrix.rawScalingMultiply(&matrix, tmpX, tmpY, tmpZ, tmpW, verticies);
}
vec = 1;
break;
}
case 3:
{
cout << "Enter the Scaling about the center you want to do ";
cout << "X Scale: ";
cin >> tmpX;
cout << "Y Scale: ";
cin >> tmpY;
cout << "Z Scale: ";
cin >> tmpZ;
cout << "W Scale: ";
cin >> tmpW;
for (int i = 0; i < verticies; i++)
{
resultMatrix = resultMatrix.centerScaleMutliply(&matrix, tmpX, tmpY, tmpZ, tmpW, verticies);
}
vec = 1;
break;
}
}
validChoice = false;

cout << "The new vertices are: " << endl;
for (int i = 0; i < verticies; i++)
{
cout << vec << ": ";
vec++;
cout << "X: " << matrix.getX();
cout << " Y: " << matrix.getY();
cout << " Z: " << matrix.getZ();
cout << " W: " << matrix.getW();
cout << endl;
}
cout << "Would you like to continue? (y/n): ";
cin >> userInput;

while (validChoice == false)
{
if (userInput == 'y' || userInput == 'Y')
{
validChoice = true;
goOn = true;
}
else if (userInput == 'n' || userInput == 'N')
{
validChoice = true;
goOn = false;
}
else
{
validChoice = false;
cout << "Would you like to continue? (y/n): ";
cin >> userInput;
}
}
}
}



To answer your questiosn really quickly.

  • Your code has lots of memory leaks. Remember to delete what you new and delete[] what you new[], or use automatic storage ("the stack") and standard containers more. IMHO, this is a very important point so make sure to read up a little about these things.
    I know i'm still working with things, dynamically allocating arrays will produce memory leaks if i don't delete them properly, For this class lab having memory leaks is okay right now, we are focusing on getting the math to work.
  • main() should return an int, really
    Yeah i know, void
  • "using namespace std;" in a header is considered bad practice because it has adverse effects on clients of the header
    yeah i know, teacher wants it in there so it is....
  • if you're learned about constructors, consider using one instead of having a set4DVector method
    again teacher says we need to create a set method and not a constructor
  • A #include "Vector3D.h" is missing from the top of main.cpp
    snippet error
  • Calling a 4D vector "Vector3D" is going to confuse a lot of people -- including you! At the start of translation(), there's a '3' which should be a '4'.
    yep.. confuses the hell outta me.. appearently this was a 4D vector to begin with and we just called it 3D, the W value was just set to 1. Now we are setting it to a user input value hence the 4d set method.
  • Why does translation() return a void* (as opposed to a Vector3D*) ?
    just me messing around with a few things, thought void might work.. i was wrong.
    Hopefully you can get the entire picture on what we are doing. A specific error i'm running into is that my t array i create and hard set is corrupted and i don't know why.

Share this post


Link to post
Share on other sites

[quote name='edd²' timestamp='1303984500' post='4803911']

As for your specific question: think very carefully about the inputs and outputs of Vector3D's translation() method. What does it return and what are you doing with that returned data? What should you be doing with it?

[/quote]
Did you try this suggestion?


[...]

To answer your questiosn really quickly.


  • [...]
  • Calling a 4D vector "Vector3D" is going to confuse a lot of people -- including you! At the start of translation(), there's a '3' which should be a '4'. yep.. confuses the hell outta me.. appearently this was a 4D vector to begin with and we just called it 3D, the W value was just set to 1. Now we are setting it to a user input value hence the 4d set method.

    [/quote]
    Ok, but you haven't fixed the problem I pointed out yet. To give you more of a hint, there's a buffer overflow caused by a '3' that should be a '4'.


    A specific error i'm running into is that my t array i create and hard set is corrupted and i don't know why.
    [/quote]
    How is it corrupted and how can you tell? (The error I've been hinting at in that function would be a likely candidate for some data corruption).

Share this post


Link to post
Share on other sites

[quote name='turlisk' timestamp='1304009815' post='4804034']
[quote name='edd²' timestamp='1303984500' post='4803911']
As for your specific question: think very carefully about the inputs and outputs of Vector3D's translation() method. What does it return and what are you doing with that returned data? What should you be doing with it?

[/quote]
Did you try this suggestion?
[/quote]

I figured out the 3 needs to be a 4 after looking into the code already (thanks for the extra hint on that too)

so that causes the error...

you asked " What should you be doing with it?" i should be setting those values in to result matrix and it's not happening.. doing an = gives me an error thanks to pointers and setting it equal to the pointer.method does nothing with the returned values..

I guess i could break that into 3 functions, translate X Y and Z and then do a set4d with translate x y and z in the calling variables but that would be a lot of work and think i can do it in the one function.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!