Saturday, May 23, 2015

Basic Math classes

Let us beginn with fundamental elements for our algorithms: Vectors and Matrices.

I used 2 classes for both 2D and 3D Vectors:

Vector2D: 2-dimensional vector and point
Vector2D.hVector2D.cpp

Vector3D: 3-dimensional vector and point
Vector3D.hVector3D.cpp

  • length() returns the length of a Vector
  • normalize() reduces x, y, z so that Vector's length = 1


I admit at this part that I'm sort of lazy and I don't really see a programming difference between points and vectors. That's why I use them in one and the same class. You will also see that I overloaded operators which returns a Vector:
  • Vector + Vector
  • Vector - Vector
  • Vector * scalar
  • Vector / scalar
  • scalar * Vector
Actually not really needed, but I also overloaded operator for Dot and Cross products:
  • Dot Product: Vector * Vector (2D/3D), important for angles
  • Cross Product: Vector % Vector (3D only), important for surfaces
That way I'm able to use Vector class like any other number data type (int, long, float, double etc.) and - unlike Java - don't need to write confusing calculation methods (add, sub, mul, div etc.)

I also used 2 classes for both 2D and 3D Matrices:

TransformMatrix2D: 3x3 Matrix for 2D coordinate transformations
TransformMatrix2D.hTransformMatrix2D.cpp

TransformMatrix3D: 4x4 Matrix for 3D coordinate transformations
TransformMatrix3D.hTransformMatrix3D.cpp

These two classes' only job is to setup transformation matrices. We have:

  • Translation (2D and 3D)
  • Scale (2D and 3D)
  • Rotation (2D and 3D around x-, y- and z-axis
  • Mirroring (2D only)
There is no single method where you can combine several transformations. The only way to do so is by multiplying them altogether. Therefore I overloaded * operator.
  • Matrix * Matrix (2D and 3D)
  • Matrix * Vector (2D and 3D)
We have no use for "+" and "-" operators, so I skipped that part.

Last but not least I provided some Mathtools to the project I might think very useful:
  • Direction from one Point/Vector to another (destination - start)
  • Distance between two Points/Vectors
  • Dot- and Cross products
  • Methods which return correct transformation matrices (even in combination)
  • sin(x) and cos(x) where x is in degree (for human useage)

There might be a chance that we will include additional methods to this class, but I think I mentioned most of it. In the next issue we will setup a render class and our first graphical output in Portable Pixmap (PPM) format.

In addition I will describe some methods for coloring pixels, from simple ones to lightly complex ones. We will keep it with lines and circles (a rectangle has 4 lines ^^ ). It features drawing:

  • Lines
  • Circles
  • Ellipses
  • Rectangles
  • Polygons
  • Polylines


See ya in my next post.

Feel free to fork my repository and try the code yourself, but don't change it in master branch. Source code was compiled with Visual Studio 2013 (Windows 8.1) and GNU 4.9.2 (Ubuntu 12.04). If someone has a clue about improving my Makefile I'm thankful

Links:
Repository for that post

No comments:

Post a Comment