1 /* 2 * Copyright (C) 2011 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 18 package android.filterfw.geometry; 19 20 import java.lang.Math; 21 22 /** 23 * @hide 24 */ 25 public class Point { 26 27 public float x; 28 public float y; 29 30 public Point() { 31 } 32 33 public Point(float x, float y) { 34 this.x = x; 35 this.y = y; 36 } 37 38 public void set(float x, float y) { 39 this.x = x; 40 this.y = y; 41 } 42 43 public boolean IsInUnitRange() { 44 return x >= 0.0f && x <= 1.0f && 45 y >= 0.0f && y <= 1.0f; 46 } 47 48 public Point plus(float x, float y) { 49 return new Point(this.x + x, this.y + y); 50 } 51 52 public Point plus(Point point) { 53 return this.plus(point.x, point.y); 54 } 55 56 public Point minus(float x, float y) { 57 return new Point(this.x - x, this.y - y); 58 } 59 60 public Point minus(Point point) { 61 return this.minus(point.x, point.y); 62 } 63 64 public Point times(float s) { 65 return new Point(this.x * s, this.y * s); 66 } 67 68 public Point mult(float x, float y) { 69 return new Point(this.x * x, this.y * y); 70 } 71 72 public float length() { 73 return (float)Math.sqrt(x*x + y*y); 74 } 75 76 public float distanceTo(Point p) { 77 return p.minus(this).length(); 78 } 79 80 public Point scaledTo(float length) { 81 return this.times(length / this.length()); 82 } 83 84 public Point normalize() { 85 return this.scaledTo(1.0f); 86 } 87 88 public Point rotated90(int count) { 89 float nx = this.x; 90 float ny = this.y; 91 for (int i = 0; i < count; ++i) { 92 float ox = nx; 93 nx = ny; 94 ny = -ox; 95 } 96 return new Point(nx, ny); 97 } 98 99 public Point rotated(float radians) { 100 // TODO(renn): Optimize: Keep cache of cos/sin values 101 return new Point((float)(Math.cos(radians) * x - Math.sin(radians) * y), 102 (float)(Math.sin(radians) * x + Math.cos(radians) * y)); 103 } 104 105 public Point rotatedAround(Point center, float radians) { 106 return this.minus(center).rotated(radians).plus(center); 107 } 108 109 @Override 110 public String toString() { 111 return "(" + x + ", " + y + ")"; 112 } 113 } 114