How would you go about setting the x, y, pixel_x, pixel_y of a matrix, manually?
|
If you don't want to deal with remembering which letters refer to what, you can use Translate() on matrices for the same effect.
var/matrix/M=new Other procs apply such as Scale() and Turn(). Skewing is the only function that does not have a proc. I've yet to need to go any deeper than these. |
a d 0
b e 0
c f 1
The first thing you should know about matrices, is that they solve like so that they solve:
x_output = a*x_input + b*y_input + c
y_output = d*x_input + e*y_input + f
C and F respectively equate to translation.
They are simply added to the product of the scalar components.
So let's look at some common matrix operations:
We need to know the default matrix first. This is called the identity matrix.
1 0 0
0 1 0
0 0 1
The identity matrix is what you can consider an "unmodified" matrix. This means it is not translated, rotated, scaled, skewed, etc.
Scale:
The scale matrix looks something like this:
X 0 0
0 Y 0
0 0 1
X refers to the X-axis scaling. 1 means normal scale, 0.5 means half scale, 2 means double, etc. -1 would mean inverting the scale (mirroring the image across the specified axis) -2 would also double the size as well as mirroring it.
Y refers to the Y-axis scaling.
Skew:
Skewing will add additional X/Y values to your image, making each successive row of pixels change in slope.
1 Y 0
X 1 0
0 0 1
A Y value of 1 means for every 1 pixel we move to the right, we move that pixel upward 1.
A Y value of -1 means for every pixel we move to the right, the move that pixel downward 1.
An X value of 1 means that for every Y pixel we move up, the X value will increase by 1, and so on.
Rotation
Sin Cos 0
Cos Sin 0
0 0 1
Rotation is achieved by applying the Sin/Cos values of a particular angle to the a, b, d, and e values.
Translation
Translation is achieved through c and f.
A translation matrix would look something like this:
1 0 0
0 1 0
X Y 1
X is the number of pixels you want to move along the X axis,
Y is the number of pixels you want to move along the Y axis.
Mixing and matching:
You can combine matrices by multiplying them.
It's a lot of grunt algebra work, but:
In other words, stacking your rotations is best done through matrix multiply.
You will also notice that the process for multiplying matrices necessitates that the order that you multiply them in matters, because they matrix_x * matrix_y != matrix_y * matrix_x.
So yeah... That's the basics. Admittedly, algebra isn't really my best topic, and I never got taught matrices properly, so I could be partially wrong on a lot of this. Don't take it as gospel. Use it as a guideline moreso than a rule. And the other math junkies here, please correct any mistakes you find in this. I'm sure there are some.