1/*
2 * Copyright 2019, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17function multiplyVec2(matrix, x, y) {
18  if (!matrix) return {x, y};
19  // |dsdx dsdy  tx|     | x |
20  // |dtdx dtdy  ty|  x  | y |
21  // |0    0     1 |     | 1 |
22  return {
23    x: matrix.dsdx * x + matrix.dsdy * y + matrix.tx,
24    y: matrix.dtdx * x + matrix.dtdy * y + matrix.ty,
25  };
26}
27
28function multiplyRect(transform, rect) {
29  let matrix = transform;
30  if (transform && transform.matrix) {
31    matrix = transform.matrix;
32  }
33  //          |dsdx dsdy  tx|         | left, top         |
34  // matrix = |dtdx dtdy  ty|  rect = |                   |
35  //          |0    0     1 |         |     right, bottom |
36
37  const leftTop = multiplyVec2(matrix, rect.left, rect.top);
38  const rightTop = multiplyVec2(matrix, rect.right, rect.top);
39  const leftBottom = multiplyVec2(matrix, rect.left, rect.bottom);
40  const rightBottom = multiplyVec2(matrix, rect.right, rect.bottom);
41
42  const outrect = {};
43  outrect.left = Math.min(leftTop.x, rightTop.x, leftBottom.x, rightBottom.x);
44  outrect.top = Math.min(leftTop.y, rightTop.y, leftBottom.y, rightBottom.y);
45  outrect.right = Math.max(leftTop.x, rightTop.x, leftBottom.x, rightBottom.x);
46  outrect.bottom = Math.max(leftTop.y, rightTop.y, leftBottom.y,
47      rightBottom.y);
48  return outrect;
49}
50
51export {multiplyRect};
52