Home | History | Annotate | Download | only in imageshow
      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 
     17 package com.android.gallery3d.filtershow.imageshow;
     18 
     19 import android.graphics.Rect;
     20 import android.graphics.RectF;
     21 
     22 public 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     public static float[] lineIntersect(float[] line1, float[] line2) {
     30         float a0 = line1[0];
     31         float a1 = line1[1];
     32         float b0 = line1[2];
     33         float b1 = line1[3];
     34         float c0 = line2[0];
     35         float c1 = line2[1];
     36         float d0 = line2[2];
     37         float d1 = line2[3];
     38         float t0 = a0 - b0;
     39         float t1 = a1 - b1;
     40         float t2 = b0 - d0;
     41         float t3 = d1 - b1;
     42         float t4 = c0 - d0;
     43         float t5 = c1 - d1;
     44 
     45         float denom = t1 * t4 - t0 * t5;
     46         if (denom == 0)
     47             return null;
     48         float u = (t3 * t4 + t5 * t2) / denom;
     49         float[] intersect = {
     50                 b0 + u * t0, b1 + u * t1
     51         };
     52         return intersect;
     53     }
     54 
     55     public static float[] shortestVectorFromPointToLine(float[] point, float[] line) {
     56         float x1 = line[0];
     57         float x2 = line[2];
     58         float y1 = line[1];
     59         float y2 = line[3];
     60         float xdelt = x2 - x1;
     61         float ydelt = y2 - y1;
     62         if (xdelt == 0 && ydelt == 0)
     63             return null;
     64         float u = ((point[0] - x1) * xdelt + (point[1] - y1) * ydelt)
     65                 / (xdelt * xdelt + ydelt * ydelt);
     66         float[] ret = {
     67                 (x1 + u * (x2 - x1)), (y1 + u * (y2 - y1))
     68         };
     69         float[] vec = {
     70                 ret[0] - point[0], ret[1] - point[1]
     71         };
     72         return vec;
     73     }
     74 
     75     // A . B
     76     public static float dotProduct(float[] a, float[] b) {
     77         return a[0] * b[0] + a[1] * b[1];
     78     }
     79 
     80     public static float[] normalize(float[] a) {
     81         float length = (float) Math.sqrt(a[0] * a[0] + a[1] * a[1]);
     82         float[] b = {
     83                 a[0] / length, a[1] / length
     84         };
     85         return b;
     86     }
     87 
     88     // A onto B
     89     public static float scalarProjection(float[] a, float[] b) {
     90         float length = (float) Math.sqrt(b[0] * b[0] + b[1] * b[1]);
     91         return dotProduct(a, b) / length;
     92     }
     93 
     94     public static float[] getVectorFromPoints(float[] point1, float[] point2) {
     95         float[] p = {
     96                 point2[0] - point1[0], point2[1] - point1[1]
     97         };
     98         return p;
     99     }
    100 
    101     public static float[] getUnitVectorFromPoints(float[] point1, float[] point2) {
    102         float[] p = {
    103                 point2[0] - point1[0], point2[1] - point1[1]
    104         };
    105         float length = (float) Math.sqrt(p[0] * p[0] + p[1] * p[1]);
    106         p[0] = p[0] / length;
    107         p[1] = p[1] / length;
    108         return p;
    109     }
    110 
    111     public static RectF scaleRect(RectF r, float scale) {
    112         return new RectF(r.left * scale, r.top * scale, r.right * scale, r.bottom * scale);
    113     }
    114 
    115     // A - B
    116     public static float[] vectorSubtract(float[] a, float[] b) {
    117         int len = a.length;
    118         if (len != b.length)
    119             return null;
    120         float[] ret = new float[len];
    121         for (int i = 0; i < len; i++) {
    122             ret[i] = a[i] - b[i];
    123         }
    124         return ret;
    125     }
    126 
    127     public static float vectorLength(float[] a) {
    128         return (float) Math.sqrt(a[0] * a[0] + a[1] * a[1]);
    129     }
    130 
    131     public static float scale(float oldWidth, float oldHeight, float newWidth, float newHeight) {
    132         if (oldHeight == 0 || oldWidth == 0)
    133             return 1;
    134         return Math.min(newWidth / oldWidth, newHeight / oldHeight);
    135     }
    136 
    137     public static Rect roundNearest(RectF r) {
    138         Rect q = new Rect(Math.round(r.left), Math.round(r.top), Math.round(r.right),
    139                 Math.round(r.bottom));
    140         return q;
    141     }
    142 
    143 }
    144