1 /*
2  * Copyright (C) 2018 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 
17 package android.view.math;
18 
19 public class Math3DHelper {
20 
Math3DHelper()21     private Math3DHelper() { }
22 
min(int x1, int x2, int x3)23     public final static int min(int x1, int x2, int x3) {
24         return (x1 > x2) ? ((x2 > x3) ? x3 : x2) : ((x1 > x3) ? x3 : x1);
25     }
26 
max(int x1, int x2, int x3)27     public final static int max(int x1, int x2, int x3) {
28         return (x1 < x2) ? ((x2 < x3) ? x3 : x2) : ((x1 < x3) ? x3 : x1);
29     }
30 
31     /**
32      * @return Rect bound of flattened (ignoring z). LTRB
33      * @param dimension - 2D or 3D
34      */
flatBound(float[] poly, int dimension)35     public static float[] flatBound(float[] poly, int dimension) {
36         int polySize = poly.length/dimension;
37         float left = poly[0];
38         float right = poly[0];
39         float top = poly[1];
40         float bottom = poly[1];
41 
42         for (int i = 0; i < polySize; i++) {
43             float x = poly[i * dimension + 0];
44             float y = poly[i * dimension + 1];
45 
46             if (left > x) {
47                 left = x;
48             } else if (right < x) {
49                 right = x;
50             }
51 
52             if (top > y) {
53                 top = y;
54             } else if (bottom < y) {
55                 bottom = y;
56             }
57         }
58         return new float[]{left, top, right, bottom};
59     }
60 
61     /**
62      * Translate the polygon to x and y
63      * @param dimension in what dimension is polygon represented (supports 2 or 3D).
64      */
translate(float[] poly, float translateX, float translateY, int dimension)65     public static void translate(float[] poly, float translateX, float translateY, int dimension) {
66         int polySize = poly.length/dimension;
67 
68         for (int i = 0; i < polySize; i++) {
69             poly[i * dimension + 0] += translateX;
70             poly[i * dimension + 1] += translateY;
71         }
72     }
73 
74 }
75 
76