1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "matrix_test.h"
17 
18 namespace OHOS {
19 namespace Rosen {
20 namespace Drawing {
TestMatrix(Canvas & canvas,uint32_t width,uint32_t height)21 void MatrixTest::TestMatrix(Canvas& canvas, uint32_t width, uint32_t height)
22 {
23     LOGI("+++++++ TestMatrix");
24     Brush brush;
25     brush.SetColor(Drawing::Color::COLOR_BLUE);
26     canvas.AttachBrush(brush);
27     canvas.DrawRect({ 1200, 100, 1500, 400 }); // rect is set to (fLeft, fTop, fRight, fBottom)
28 
29     brush.SetColor(Drawing::Color::COLOR_RED);
30     canvas.AttachBrush(brush);
31     Matrix m;
32     // Set matrix to scale by 0.5 and 1.5, about a pivot point at (0, 0).
33     m.Scale(0.5, 1.5, 0, 0);
34 
35     canvas.SetMatrix(m);
36     canvas.DrawRect({ 1000, 0, 1300, 300 }); // rect is set to (fLeft, fTop, fRight, fBottom)
37 
38     Matrix n;
39     /* Set matrix to:
40         | 1  0  100 |
41         | 0  1  300 |
42         | 0  0   1  |
43     */
44     n.SetMatrix(1, 0, 100, 0, 1, 300, 0, 0, 1);
45     brush.SetColor(Drawing::Color::COLOR_GREEN);
46     canvas.AttachBrush(brush);
47     canvas.SetMatrix(n);
48     canvas.DrawRect({ 1200, 100, 1500, 400 }); // rect is set to (fLeft, fTop, fRight, fBottom)
49 
50     Matrix m1;
51     Matrix m2;
52     m1.Translate(100, 300); // Set matrix to translate by (100, 300).
53     // Set matrix to rotate by degrees 45 about a pivot point at (0, 0).
54     m2.Rotate(45, 0, 0);
55     m = m1 * m2;
56 
57     brush.SetColor(Drawing::Color::COLOR_BLACK);
58     canvas.AttachBrush(brush);
59     canvas.SetMatrix(m);
60     canvas.DrawRect({ 1200, 100, 1500, 400 }); // rect is set to (fLeft, fTop, fRight, fBottom)
61 
62     Matrix matrix;
63     matrix.Translate(100, 100);              // 100 means offset size
64     std::vector<Point> point = { { 0, 0 } }; // {0, 0} means point position
65     matrix.MapPoints(point, point, point.size());
66     for (size_t i = 0; i < point.size(); i++) {
67         LOGI("mapped point fx = %{public}f, fy = %{public}f", point[i].GetX(), point[i].GetY());
68     }
69 
70     LOGI("------- TestMatrix");
71 }
72 
MatrixTestCase()73 std::vector<MatrixTest::TestFunc> MatrixTest::MatrixTestCase()
74 {
75     std::vector<TestFunc> testFuncVec;
76     testFuncVec.push_back(TestMatrix);
77     return testFuncVec;
78 }
79 } // namespace Drawing
80 } // namespace Rosen
81 } // namespace OHOS