# Working with vectors and transformations

I am going to write an graphical 2D application that allows user to create polygons and transform them through transformation such as rotation an so on. I was hoping someone can give pro and cons arguments for the different choices I got in my mind. (Its all in Java btw!)

a). Represent vectors by filling matrices with 'real' numbers. This means making a matrix datas tructure that supports multiplication, transposing etc

b). Make a own vector class, such that I can make a matrix class that support those vectors.

-
I don't see how a) and b) are different. At the end of the day you are still going to manipulate arrays of numbers? – davidk01 Sep 21 '12 at 20:56

My personal preference (for my own personal project) is to have separate classes for matrices and vectors. This allows the language to do a bit more typechecking than if everything was a matrix.

-
Yes, having the compiler to verify that you multiply a 'horizontal' vector to a square matrix and a square matrix by 'vertical' vector would be very welcome to me. – 9000 Sep 21 '12 at 18:24

For 2D transforms you only need 2-tuple vectors and a 2x2 matrix. Both are trivial to implement. Things like matrix to vector multiplication would take a one-line formula rather than a proper loop.

Since matrices are so small I think it would be easier to store 4 numbers in a matrix and not a list of vectors. At least I'd try this approach first. If you need you can directly accept vectors in a constructor or a factory method (e.g. to create an offset matrix).

-

It seems like you just need

``````struct xy {float x; float y}
``````

and

``````class affine_transformation
{
float m[6];

xy transform(xy input) {...}
...
methods rotate(), shear(), scale(), translate()
};
``````
-