# Matrix Scale and Translation

This is a discussion on Matrix Scale and Translation within the C++ Programming forums, part of the General Programming Boards category; Just a warning that this is for a class and is a lab for homework. I'm not expecting an answer ...

1. ## Matrix Scale and Translation

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
Code:
```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[i].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[i].translation(&matrix[i], tmpX, tmpY, tmpZ, tmpW, verticies);
}

cout << "The new vertices are: " << endl;
for (int i = 0; i < verticies; i++)
{
cout << vec << ":     ";
vec++;
cout << "X: " << matrix[i].getX();
cout << " Y: " << matrix[i].getY();
cout << " Z: " << matrix[i].getZ();
cout << " W: " << matrix[i].getW();
cout << endl;
}```
Vector3D.h
Code:
```#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[i]));
newY = newY + (t[1].dotProduct(t[1], a[i]));
newZ = newZ + (t[2].dotProduct(t[2], a[i]));
}
for (int i = 0; i < verticies; i++)
{
result[i].set4DVector(a[i].getX() + newX,a[i].getY() + newY, a[i].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 awesome.

2. in your translation method,
Code:
`t = new Vector3D [4];`
Since you learned "new", how about flipping some pages on "delete" ? You're program is leaking memory.

Popular pages Recent additions