in reply to Translating python math to Perl
You might take another crack at linear algebra. It shows up fairly often in graphics, and isn't too hard to reason about once you create a mental model around what is happening.
In case it helps, here is my mental model in a nutshell. Suppose you have an (x,y,z) point in a room. Suppose you are standing in that room at point (x2,y2,z2). The direction you are facing and angle of your head is represented as a 3x3 matrix. (ignore its contents for the moment) Suppose your task is to figure out what the coordinates of that point would be to the coordinate system of your eye. (in other words, imagine a coordinate system where your eye is (0,0,0) and forward 1 meter along your line of sight is (0,0,1) and so on) So you could describe any point in the room relative to the room, or describe it relative to your line of sight.
You solve that problem by subtracting your eye (in room coordinates) from the point (in room coordinates) then multiply that by the matrix that represents the direction you are looking. Now you have a point relative to your eye's coordinate system.
Now, what is the 3x3 matrix describing the direction you are looking? Well, the top row is the (x,y,z) vector (in room coordinates) sideways (leftward) from your eye. The second row is the (x,y,z) vector (room coordinates) if what your eye considers to be "up". And the third row of the matrix is the (x,y,z) vector (room coordinates) of what your eye considers to be "forward".
So remember that initial subtraction we had to do before multiplying by the matrix? It turns out you can hide that step if you upgrade the matrix to 4x4 and write your point as (x,y,z,1). The same happens in 2D, by upgrading to a 3x3 matrix with points as (x,y,1). I think this is what you're seeing in that code above with the funny stuff it does before multiplying the point by the matrix. This wastes a few multiplications, but lets you describe it in one operation.
If you want to map a point back out of a coordinate space, you just multiply by the "transpose" of the matrix. (swapping the columns and rows)
You can map a coordinate space into or out of a coordinate space! Remember how the matrix is really just three vectors described in the parent coordinate space? Well if you map those 3 vectors into some other matrix, now you've mapped the whole coordinate space into the other coordinate space. This turns out to happen automatically by normal matrix multiplication. Just multiply a matrix by a matrix and you've got a new matrix that represents performing both translations. Now (if you have a lot of points to remap) you can actually save multiplications by only needing to multiply each point by one matrix instead of a chain of matrix multiplications.
If that all made relative sense, then the rest is just implementation details which you can safely forget until you need them.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Translating python math to Perl
by cavac (Prior) on Aug 28, 2023 at 07:55 UTC | |
by NERDVANA (Priest) on Sep 02, 2023 at 07:58 UTC | |
by etj (Priest) on Feb 06, 2024 at 18:39 UTC | |
by NERDVANA (Priest) on Feb 07, 2024 at 02:13 UTC | |
by etj (Priest) on Jun 18, 2024 at 02:34 UTC | |
by NERDVANA (Priest) on Aug 29, 2023 at 20:16 UTC | |
by etj (Priest) on Feb 06, 2024 at 19:57 UTC |