在我做的项目中,很多图形本身只有一组变换参数和一个指向图元的引用,那么绘制这些图形时,即需要用变换参数把图元坐标变换到真正的显示系统坐标,才能绘制。当捕捉这些对象时,又需要把鼠标点逆变换到图元坐标系统中。
这是一些注释:很基本,变换和逆变换的代码省略。
/*
*********************************************************************
* 【变换矩阵Matrix】
* (1)坐标变换的矩阵方程为:
*
* [ m11 m12 0 ]
* [ ]
* [x' y' 1]=[x y 1] [ m21 m22 0 ]
* [ ]
* [ dx dy 0 ]
*
* (2)变换可以累加,变换矩阵为每次变换的相乘结果。
*
* (3)变换矩阵的逆矩阵为:
*
* [ m22 -m12 0 ]
* [ ]
* [ -m21 m11 0 ] /(m11*m22-m12*m22)
* [ ]
* [ -m22*dx+m21*dy m12*dx-m11*dy 1 ]
*
* (4)后偏移法的变换矩阵:
*
* x' = x * scale + dx
* y' = y * scale + dy
*
* [ scale 0 0 ]
* [ 0 scale 0 ]
* [ dx dy 1 ]
*
*
* (5)围绕原点(0,0)(向y轴正向)旋转a角度的变换矩阵:
*
* x' = x * cos(a) - y * sin(a)
* y' = x * sin(a) + y * cos(a)
*
* [ cos(a) sin(a) 0 ]
* [ -sin(a) cos(a) 0 ]
* [ 0 0 0 ]
*
* (6)先围绕原点正向旋转a度,再缩放z倍,再平移dx,dy的变换矩阵:
* x' = x *cos(a)*z -y *sin(a)*z +dx
* y' = x *sin(a)*z +y *cos(a)*z +dy
*
*
* [ cos(a)*z sin(a)*z 0 ]
* [ -sin(a)*z cos(a)*z 0 ]
* [ dx dy 0 ]
*
* (7)上式的逆变换是:
* x = x' * cos(a)/z +y *sin(a)/z -(dx*cos(a)+dy*sin(a))/z
* y = x' * -sin(a)/z +y *cos(a)/z +(dx*sin(a)-dy*cos(a))/z
*
*
* [ cos(a) -sin(a) 0 ]
* [ sin(a) cos(a) 0 ] / z
* [ -dx*cos(a)-dy*sin(a) dx*sin(a)-dy*cos(a) 0 ]
*
*
********************************************************************** */
* 【变换矩阵Matrix】
* (1)坐标变换的矩阵方程为:
*
* [ m11 m12 0 ]
* [ ]
* [x' y' 1]=[x y 1] [ m21 m22 0 ]
* [ ]
* [ dx dy 0 ]
*
* (2)变换可以累加,变换矩阵为每次变换的相乘结果。
*
* (3)变换矩阵的逆矩阵为:
*
* [ m22 -m12 0 ]
* [ ]
* [ -m21 m11 0 ] /(m11*m22-m12*m22)
* [ ]
* [ -m22*dx+m21*dy m12*dx-m11*dy 1 ]
*
* (4)后偏移法的变换矩阵:
*
* x' = x * scale + dx
* y' = y * scale + dy
*
* [ scale 0 0 ]
* [ 0 scale 0 ]
* [ dx dy 1 ]
*
*
* (5)围绕原点(0,0)(向y轴正向)旋转a角度的变换矩阵:
*
* x' = x * cos(a) - y * sin(a)
* y' = x * sin(a) + y * cos(a)
*
* [ cos(a) sin(a) 0 ]
* [ -sin(a) cos(a) 0 ]
* [ 0 0 0 ]
*
* (6)先围绕原点正向旋转a度,再缩放z倍,再平移dx,dy的变换矩阵:
* x' = x *cos(a)*z -y *sin(a)*z +dx
* y' = x *sin(a)*z +y *cos(a)*z +dy
*
*
* [ cos(a)*z sin(a)*z 0 ]
* [ -sin(a)*z cos(a)*z 0 ]
* [ dx dy 0 ]
*
* (7)上式的逆变换是:
* x = x' * cos(a)/z +y *sin(a)/z -(dx*cos(a)+dy*sin(a))/z
* y = x' * -sin(a)/z +y *cos(a)/z +(dx*sin(a)-dy*cos(a))/z
*
*
* [ cos(a) -sin(a) 0 ]
* [ sin(a) cos(a) 0 ] / z
* [ -dx*cos(a)-dy*sin(a) dx*sin(a)-dy*cos(a) 0 ]
*
*
********************************************************************** */