Matrix multiplication demonstration!
# Matrix multiplication demonstration! This program demonstrates a simple matrix multiplication library. ### Why is it significant? Matrix multiplication is used quite commonly for graphical effects like panning and zooming. Even for developers, alot of this work is done for them, but someone has to implement it. ### Do note! The future! This matrix multiplication library is currently unfinished and is only posted this way to demonstrate. I will be working on a 3D version of this, as well as a more advanced graphics stack to make use of it. I am willing to implement this in another programming language or even on an FPGA, given demand for this is proven. ### Is there an API? There is a basic API so far for robotman2412_matrix: | class | name | syntax | description | | :------------ | :-------------- | :---------------------------- | :--------------------------------------------------------------------------------------------------------- | | MatrixStack2D | multiply matrix | multiplyMatrix(value: Matrix) | Multiplies the current matrix by value, effectively applies the transformation value represents. | | MatrixStack2D | clear stack | clear() | Clears the matrix stack, sets the current matrix to identity (no transformation applied). | | MatrixStack2D | push matrix | push() | Pushes the current matrix onto the matrix stack so it can be used later. (see: stack) | | MatrixStack2D | pop matrix | pop() | Pops the last pushed matrix off the matrix stack and sets the current matrix to it. (see: stack) | | MatrixStack2D | transform point | x, y = transformPoint(x, y) | Transforms the XY coordinates based on the current transformation, used to easily draw transformed shapes. | | MatrixStack2D | translate | translate(x, y) | Applies a transformation equivalent to moving relatively by X and Y. | | MatrixStack2D | rotate | rotate2D(angle) | Applies a transformation equivalent to rotating the canvas around the origin by an angle. | | MatrixStack2D | scale | scale(x, y) | Applies a transformation equivalent to scaling the canvas by X and Y. | | - | make vector | vector(\*content) | Advanced: Makes a vector with content. | | - | make vector | vector_empty(size) | Advanced: Makes a vector of size, but filled with zeros. | | - | make matrix | matrix_empty(width, height) | Advanced: Makes a matrix of size, but filled with zeros. | | - | copy matrix | matrix_copy(other: Matrix) | Advanced: Makes a copy of another matrix. | | - | make matrix | matrix_implicit(x, y, \*cont) | Advanced: Makes a matrix with content. | | - | make rotation | matrix_rotate_2D(angle) | Makes a matrix representing a 2D rotation, also used in MatrixStack2D.rotate2D() | | - | make move | matrix_translate(x, y) | Makes a matrix representing a 2D movement, also used in MatrixStack2D.translate() | | - | make scale | matrix_scale_uniform_2D(scale)| Makes a matrix representing a 2D scale, but scaling X and Y by the same amount | | - | make rotation | matrix_scale_2D(x, y) | Makes a matrix representing a 2D rotation, also used in MatrixStack2D.scale2D() | | Matrix | multiply | Matrix * other | Matrix multiplication: other can be number, vector or matrix. | | Matrix | add | Matrix + Matrix | Matrix addition: adds two matricies of the same size entry-by-entry. | For the rest there is no real API yet, tough i am working on it. If you would like to be notified, follow me on [twitter](https://twitter.com/RobotMan2412) as i will make a tweet when this is more developed. Or, if you are already in the badge.team, i will also post it in telegram there. #### stack - how the matrix stack works It behaves like a stack of objects and is named as such. The first thing you push is the last thing you pop. Basically: If you want to use the current transformation later on but need to change it first, you do a push() Then do your changes... then do a pop() Which results in the stack being the same as when you did the push. Note: By the nature of transformations, it matters in which order you apply them. Code example: stack = MatrixStack2D() stack.translate(30, 30) # Offset by 30 pixels down to the right. stack.push() # Save the currect stack: offset 30, 30 pixels but no rotation. stack.rotate2D(some angle) # Rotate around the offset. stack.push() # Save the current stack: offset 30, 30 pixels followed by rotation. stack.translate(-30, -30) # Translate again, maybe to draw a model... ... stack.pop() \# The stack will at this point again represent the 30, 30 pixel offset followed by the rotation. ... stack.pop() \# The stack will at this point again represent the 30, 30 pixel offset with no rotation. ### How does it work? I cannot explain this fully, but for anyone familiar with matrices: Take a 3D identity matrix: \[ 1 0 0 ] \[ 0 1 0 ] \[ 0 0 1 ] This matrix represents no transformations at all. Now, take a translation matrix, where X is movement along the X-axis and Y is movement along th Y axis: \[ 1 0 X ] \[ 0 1 Y ] \[ 0 0 1 ] This is a matrix representing a movement. As you may notice, the bottom row will never change from the identity. For this reason, when one manually inputs a matrix, this row may be omitted without consequence. This means that any matrix is two rows by three columns, even if internally 3x3.