This is the fourth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021
1 the introduction
Graphic transformation and observation is one of the basic contents of computer graphics, and also an indispensable link in the process of graphic display
In fact, many complex graphics can be combined by simple graphics through translation, rotation, scaling, mirroring, projection and other operations
This paper will discuss the various transformations of two-dimensional graphics and the combination of transformation results into complex graphics
2 ideas
All transformation functions, such as translation, rotation, scaling, etc., can be integrated into one function, and only need to change the parameters of the function when called, without repeated implementation
The main idea of two-dimensional graph transformation is matrix operation, and the right multiplication is the main method
-
The coordinates of the points that make up the original graph are reduced to homogeneous form
-
Translational transformation matrix for txty1 [100010] \ begin 1 & & 0 0 {bmatrix} \ \ 0 & 1 & 0 \ \ T_x & T_y & 1 \ end {bmatrix} ⎣ ⎢ ⎡ 10 tx01ty001 ⎦ ⎥ ⎤
-
The scaling transformation matrix is [Sx000Sy0001]\begin{bmatrix}S_x&0&0\\0&S_y&0\\0&0&1\end{bmatrix}⎣⎢, repeatable sy0001 x ⎥⎤
-
The rotation transformation matrix is zero [cosine theta sine theta 0-0001] sine theta cosine theta \ begin {bmatrix} cos \ theta&sin \ theta & 0 \ \ – sin \ theta&cos \ \ \ theta & 0 0 & 0 and 1 \ end {bmatrix} ⎣ ⎢ ⎡ cosine theta – sine theta 0 sin Theta cosine theta 0001 ⎦ ⎥ ⎤
Tips:
If the reference point of the original graph transformation is not at the origin, it should be shifted to the origin first, and then inversely shifted back to the original position after corresponding transformation
To accomplish the desired function, multiple matrix transformations are required to multiply matrices, that is, cascade transformations
3 process
3.1 the function
Function call format xuanzhuan(x, Y, A, K,tx, TY, Sx, SY)
- Where x and y are graphic coordinates
- A is rotation Angle (Angle system)
- K is a pattern: 1 is a straight line and 2 is a circle or ellipse
- Tx,ty is the translation parameter
- Sx,sy is the scaling parameter
First initialize
[~,n]=size(x);
e=ones(1,n);
A=[x;y;e]'; % homogeneous matrix
B=A; % initialization
a1=pi/ (180/a); % in radians
Copy the code
And then you do the transformation in terms of the line, or the circle and the ellipse
% of a straight line
if k==1
B=A*[1 0 0;0 1 0; -min(x) -min(y) 1]... * [cos(a1) sin(a1) 0; -sin(a1) cos(a1) 0;0 0 1]... * [1 0 0;0 1 0;min(x) min(y) 1];
C=B*[1 0 0;0 1 0; -min(x) -min(y) 1]...
*[sx 0 0;0 sy 0; tx ty1]... * [1 0 0;0 1 0;min(x) min(y) 1];
end
% Circle or ellipse
if k==2
xpingyi=(max(x)+min(x))/2; ypingyi=(max(y)+min(y))/2;
B=A*[1 0 0;0 1 0; -xpingyi -ypingyi1]... * [cos(a1) sin(a1) 0; -sin(a1) cos(a1) 0;0 0 1]... * [1 0 0;0 1 0; xpingyi ypingyi1];
C=B*[1 0 0;0 1 0; -xpingyi -ypingyi1]...
*[sx 0 0;0 sy 0; tx ty1]... * [1 0 0;0 1 0; xpingyi ypingyi1];
end
Copy the code
Finally, the new and old graphics are drawn together to facilitate comparison
% Draw new and old graphics
plot(A(:,1),A(:,2),'r-',C(:,1),C(:,2),'r-');
Copy the code
3.2 Transformation Process
- First, on the basis of drawing line, circle and ellipse, the x and Y coordinates of the basic graph are obtained
- Rotate a straight line, and then translate the old and new lines
- Rotate and symmetry the drawn ellipse to give it a graceful symmetry
- By scaling the circle, the compressed circle becomes an ellipse, which is superimposed on the circle to create a “planetary” effect
See GraphTransform(gitee.com) for the full code
4 the result
The graphic combination effect is as follows: