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.systemui.classifier; 18 19 public class Point { 20 public float x; 21 public float y; 22 public long timeOffsetNano; 23 Point(float x, float y)24 public Point(float x, float y) { 25 this.x = x; 26 this.y = y; 27 this.timeOffsetNano = 0; 28 } 29 Point(float x, float y, long timeOffsetNano)30 public Point(float x, float y, long timeOffsetNano) { 31 this.x = x; 32 this.y = y; 33 this.timeOffsetNano = timeOffsetNano; 34 } 35 equals(Point p)36 public boolean equals(Point p) { 37 return x == p.x && y == p.y; 38 } 39 dist(Point a)40 public float dist(Point a) { 41 return (float) Math.hypot(a.x - x, a.y - y); 42 } 43 44 /** 45 * Calculates the cross product of vec(this, a) and vec(this, b) where vec(x,y) is the 46 * vector from point x to point y 47 */ crossProduct(Point a, Point b)48 public float crossProduct(Point a, Point b) { 49 return (a.x - x) * (b.y - y) - (a.y - y) * (b.x - x); 50 } 51 52 /** 53 * Calculates the dot product of vec(this, a) and vec(this, b) where vec(x,y) is the 54 * vector from point x to point y 55 */ dotProduct(Point a, Point b)56 public float dotProduct(Point a, Point b) { 57 return (a.x - x) * (b.x - x) + (a.y - y) * (b.y - y); 58 } 59 60 /** 61 * Calculates the angle in radians created by points (a, this, b). If any two of these points 62 * are the same, the method will return 0.0f 63 * 64 * @return the angle in radians 65 */ getAngle(Point a, Point b)66 public float getAngle(Point a, Point b) { 67 float dist1 = dist(a); 68 float dist2 = dist(b); 69 70 if (dist1 == 0.0f || dist2 == 0.0f) { 71 return 0.0f; 72 } 73 74 float crossProduct = crossProduct(a, b); 75 float dotProduct = dotProduct(a, b); 76 float cos = Math.min(1.0f, Math.max(-1.0f, dotProduct / dist1 / dist2)); 77 float angle = (float) Math.acos(cos); 78 if (crossProduct < 0.0) { 79 angle = 2.0f * (float) Math.PI - angle; 80 } 81 return angle; 82 } 83 } 84