How to multiple a 2D point with a 4D Matrix

岁酱吖の 提交于 2019-12-04 19:58:07

Expand your 2D vector to a 4D vector - (X, Y, 0, 1); this is a 3D vector specified in homogeneous coordinates. Multiply the 4D vector by the 4D matrix thus getting a new 4D vector, from which you take the first 2 components.

If the matrix specifies some kind of perspective projection, then you'll need to divide by the last component, i.e. if your resulting vector is (x, y, z, w), then the final coordinates are (x/w, y/w, z/w). If the matrix doesn't have a perspective projection, then w = 1 and the final vector is just (x, y, z)

I'm not sure if there's a shortcut for this but what you want is:

newX = oldx * mat.M11 + oldY * mat.M21 + mat.OffsetX;
newX = oldx * mat.M12 + oldY * mat.M22 + mat.OffsetY;

(assuming that your oldZ is zero and you're going to ignore the newZ value).

Edit: A better way to do it is:

Vector3D oldPos(oldx, oldy, 0.0f);
Vector3D newPos = oldPos * matrix;

Your new coordinates are: newPos.X and newPos.Y;

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!