1/*
2 * Copyright (C) 2012 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
17package com.android.gallery3d.filtershow.imageshow;
18
19import android.graphics.Rect;
20import android.graphics.RectF;
21
22public class GeometryMath {
23
24    // Math operations for 2d vectors
25    public static float clamp(float i, float low, float high) {
26        return Math.max(Math.min(i, high), low);
27    }
28
29    protected static float[] shortestVectorFromPointToLine(float[] point, float[] l1, float[] l2) {
30        float x1 = l1[0];
31        float x2 = l2[0];
32        float y1 = l1[1];
33        float y2 = l2[1];
34        float xdelt = x2 - x1;
35        float ydelt = y2 - y1;
36        if (xdelt == 0 && ydelt == 0)
37            return null;
38        float u = ((point[0] - x1) * xdelt + (point[1] - y1) * ydelt)
39                / (xdelt * xdelt + ydelt * ydelt);
40        float[] ret = {
41                (x1 + u * (x2 - x1)), (y1 + u * (y2 - y1))
42        };
43        float [] vec = {ret[0] - point[0], ret[1] - point[1] };
44        return vec;
45    }
46
47    // A . B
48    public static float dotProduct(float[] a, float[] b){
49        return a[0] * b[0] + a[1] * b[1];
50    }
51
52    public static float[] normalize(float[] a){
53        float length = (float) Math.sqrt(a[0] * a[0] + a[1] * a[1]);
54        float[] b = { a[0] / length, a[1] / length };
55        return b;
56    }
57
58    // A onto B
59    public static float scalarProjection(float[] a, float[] b){
60        float length = (float) Math.sqrt(b[0] * b[0] + b[1] * b[1]);
61        return dotProduct(a, b) / length;
62    }
63
64    public static float[] getVectorFromPoints(float [] point1, float [] point2){
65        float [] p = { point2[0] - point1[0], point2[1] - point1[1] };
66        return p;
67    }
68
69    public static float[] getUnitVectorFromPoints(float [] point1, float [] point2){
70        float [] p = { point2[0] - point1[0], point2[1] - point1[1] };
71        float length = (float) Math.sqrt(p[0] * p[0] + p[1] * p[1]);
72        p[0] = p[0] / length;
73        p[1] = p[1] / length;
74        return p;
75    }
76
77    public static RectF scaleRect(RectF r, float scale){
78        return new RectF(r.left * scale, r.top * scale, r.right * scale, r.bottom * scale);
79    }
80
81    // A - B
82    public static float[] vectorSubtract(float [] a, float [] b){
83        int len = a.length;
84        if (len != b.length)
85            return null;
86        float [] ret = new float[len];
87        for (int i = 0; i < len; i++){
88            ret[i] = a[i] - b[i];
89        }
90        return ret;
91    }
92
93    public static float vectorLength(float [] a){
94        return (float) Math.sqrt(a[0] * a[0] + a[1] * a[1]);
95    }
96
97    public static float scale(float oldWidth, float oldHeight, float newWidth, float newHeight) {
98        if (oldHeight == 0 || oldWidth == 0)
99            return 1;
100        return Math.min(newWidth / oldWidth , newHeight / oldHeight);
101    }
102
103    public static Rect roundNearest(RectF r){
104        Rect q = new Rect(Math.round(r.left), Math.round(r.top), Math.round(r.right),
105                Math.round(r.bottom));
106        return q;
107    }
108
109}
110