ok i have to make a matrix class for a math library and im now Very lost with what im ment to do or how to do it the code i have so far is:

#include <iostream>
#include <math.h>
#include "Vector2.h"

class Matrix
{
private:
float data[3][3];

public:
Matrix()
{
}

~Matrix()
{
}

float Matrix::GetData(int Col, int Row )
{ 
return data[ Col ][ Row ];
}

float Matrix:: SetData(int Col, int Row )
{ 
return data[ Col ][ Row ];
}

void SetTranslation( const vector2 &Translation );

void GetTranslation( const vector2 &Translation );

void TransformPoint( vector2 &Point );
};

inline void Matrix:: SetTranslation( const vector2 &Translation )
{
}

inline void Matrix::GetTranslation( const vector2 &Translation )
{
}

inline void Matrix::TransformPoint( vector2 &Point )
{
//Transform and translate the point
}

and i need to now create operator functions for


Matrix + Matrix


how would i do this or can someone point me in the right direction on how to do it

Dani AI

Generated

The class in the first post has a few common issues that make implementing operators confusing: some member declarations are written with the qualified name inside the class, setters don't accept a value, and the row/column ordering is ambiguous. is right to start by thinking about elementwise addition on paper, and is pointing at the correct approach (add corresponding elements). Build on that by making the API consistent, using const-correct getters, a proper setter, and an operator+= with operator+ implemented in terms of it.

A compact, idiomatic approach (keep indexing consistent as row-major: data[row][col]):

float Get(int row, int col) const;      // const getter
void  Set(int row, int col, float v);  // setter takes a value

Matrix& Matrix::operator+=(const Matrix& rhs) {
    for (int r = 0; r < 3; ++r)
        for (int c = 0; c < 3; ++c)
            data[r][c] += rhs.data[r][c];
    return *this;
}

Matrix operator+(Matrix lhs, const Matrix& rhs) {
    lhs += rhs;
    return lhs;
}

Notes and quick checks:

  • Make Get const and Set accept the value to store. That fixes the confusion seen in 's class.
  • Choose and document ordering: treat data[row][col] so data[0][2] is first row, third column.
  • Returning Matrix by value is fine for a 3x3 float matrix (RVO/elision keeps it cheap).
  • Implement TransformPoint by applying the 3x3 affine matrix to a homogeneous point; prefer vector2 TransformPoint(const vector2 &p) const that returns a transformed point instead of mutating the input.
  • Add unit tests: identity + X == X, zero matrix, and elementwise checks to catch transposed-index bugs.

After operator+ works, consider adding operator for matrix multiplication and operator(vector2) for transforms so the class becomes useful for typical 2D affine math.

Recommended Answers

All 3 Replies

Tell us how you would add two matrices on paper.

[a b] + [1 2 ] = [ a + 1 b + 2]
[c d] [3 4 ] [c + 3 d + 4]

but how would i code it?

like this???

float Matrix::Add(data )
	{	
		[1, 0, 0] + [1, 0, 0];
		[0, 1, 0] + [0, 1, 0];
		[0, 0, 1] + [0, 0, 1];
	}

[a b] + [1 2 ] = [ a + 1 b + 2]
[c d] [3 4 ] [c + 3 d + 4]

but how would i code it?

like this???

float Matrix::Add(data )
	{	
		[1, 0, 0] + [1, 0, 0];
		[0, 1, 0] + [0, 1, 0];
		[0, 0, 1] + [0, 0, 1];
	}

lol...
You need to add their respective elements like this..

int sum[3][3];
for(int i=0;i<3;i++)
 for(int j=0;j<3;j++)
  sum[i][j]=A[i][j]+B[i][j];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.