Home | History | Annotate | Download | only in classifier
      1 /*
      2  * Copyright (C) 2015 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.incallui.answer.impl.classifier;
     18 
     19 class Point {
     20   public float x;
     21   public float y;
     22   public long timeOffsetNano;
     23 
     24   public Point(float x, float y) {
     25     this.x = x;
     26     this.y = y;
     27     this.timeOffsetNano = 0;
     28   }
     29 
     30   public Point(float x, float y, long timeOffsetNano) {
     31     this.x = x;
     32     this.y = y;
     33     this.timeOffsetNano = timeOffsetNano;
     34   }
     35 
     36   @Override
     37   public boolean equals(Object other) {
     38     if (!(other instanceof Point)) {
     39       return false;
     40     }
     41     Point otherPoint = ((Point) other);
     42     return x == otherPoint.x && y == otherPoint.y;
     43   }
     44 
     45   @Override
     46   public int hashCode() {
     47     int result = (x != +0.0f ? Float.floatToIntBits(x) : 0);
     48     result = 31 * result + (y != +0.0f ? Float.floatToIntBits(y) : 0);
     49     return result;
     50   }
     51 
     52   public float dist(Point a) {
     53     return (float) Math.hypot(a.x - x, a.y - y);
     54   }
     55 
     56   /**
     57    * Calculates the cross product of vec(this, a) and vec(this, b) where vec(x,y) is the vector from
     58    * point x to point y
     59    */
     60   public float crossProduct(Point a, Point b) {
     61     return (a.x - x) * (b.y - y) - (a.y - y) * (b.x - x);
     62   }
     63 
     64   /**
     65    * Calculates the dot product of vec(this, a) and vec(this, b) where vec(x,y) is the vector from
     66    * point x to point y
     67    */
     68   public float dotProduct(Point a, Point b) {
     69     return (a.x - x) * (b.x - x) + (a.y - y) * (b.y - y);
     70   }
     71 
     72   /**
     73    * Calculates the angle in radians created by points (a, this, b). If any two of these points are
     74    * the same, the method will return 0.0f
     75    *
     76    * @return the angle in radians
     77    */
     78   public float getAngle(Point a, Point b) {
     79     float dist1 = dist(a);
     80     float dist2 = dist(b);
     81 
     82     if (dist1 == 0.0f || dist2 == 0.0f) {
     83       return 0.0f;
     84     }
     85 
     86     float crossProduct = crossProduct(a, b);
     87     float dotProduct = dotProduct(a, b);
     88     float cos = Math.min(1.0f, Math.max(-1.0f, dotProduct / dist1 / dist2));
     89     float angle = (float) Math.acos(cos);
     90     if (crossProduct < 0.0) {
     91       angle = 2.0f * (float) Math.PI - angle;
     92     }
     93     return angle;
     94   }
     95 }
     96