Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
    
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

3 Answers 3

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.

share|improve this answer
    
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).

share|improve this answer

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()
};

See http://www.willamette.edu/~gorr/classes/GeneralGraphics/Transforms/transforms2d.htm

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.