1 /* 2 * Copyright (C) 2007 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 android.graphics; 18 19 import android.os.Parcel; 20 import android.os.Parcelable; 21 import android.util.FloatMath; 22 23 24 /** 25 * PointF holds two float coordinates 26 */ 27 public class PointF implements Parcelable { 28 public float x; 29 public float y; 30 31 public PointF() {} 32 33 public PointF(float x, float y) { 34 this.x = x; 35 this.y = y; 36 } 37 38 public PointF(Point p) { 39 this.x = p.x; 40 this.y = p.y; 41 } 42 43 /** 44 * Set the point's x and y coordinates 45 */ 46 public final void set(float x, float y) { 47 this.x = x; 48 this.y = y; 49 } 50 51 /** 52 * Set the point's x and y coordinates to the coordinates of p 53 */ 54 public final void set(PointF p) { 55 this.x = p.x; 56 this.y = p.y; 57 } 58 59 public final void negate() { 60 x = -x; 61 y = -y; 62 } 63 64 public final void offset(float dx, float dy) { 65 x += dx; 66 y += dy; 67 } 68 69 /** 70 * Returns true if the point's coordinates equal (x,y) 71 */ 72 public final boolean equals(float x, float y) { 73 return this.x == x && this.y == y; 74 } 75 76 @Override 77 public boolean equals(Object o) { 78 if (this == o) return true; 79 if (o == null || getClass() != o.getClass()) return false; 80 81 PointF pointF = (PointF) o; 82 83 if (Float.compare(pointF.x, x) != 0) return false; 84 if (Float.compare(pointF.y, y) != 0) return false; 85 86 return true; 87 } 88 89 @Override 90 public int hashCode() { 91 int result = (x != +0.0f ? Float.floatToIntBits(x) : 0); 92 result = 31 * result + (y != +0.0f ? Float.floatToIntBits(y) : 0); 93 return result; 94 } 95 96 @Override 97 public String toString() { 98 return "PointF(" + x + ", " + y + ")"; 99 } 100 101 /** 102 * Return the euclidian distance from (0,0) to the point 103 */ 104 public final float length() { 105 return length(x, y); 106 } 107 108 /** 109 * Returns the euclidian distance from (0,0) to (x,y) 110 */ 111 public static float length(float x, float y) { 112 return FloatMath.sqrt(x * x + y * y); 113 } 114 115 /** 116 * Parcelable interface methods 117 */ 118 @Override 119 public int describeContents() { 120 return 0; 121 } 122 123 /** 124 * Write this point to the specified parcel. To restore a point from 125 * a parcel, use readFromParcel() 126 * @param out The parcel to write the point's coordinates into 127 */ 128 @Override 129 public void writeToParcel(Parcel out, int flags) { 130 out.writeFloat(x); 131 out.writeFloat(y); 132 } 133 134 public static final Parcelable.Creator<PointF> CREATOR = new Parcelable.Creator<PointF>() { 135 /** 136 * Return a new point from the data in the specified parcel. 137 */ 138 public PointF createFromParcel(Parcel in) { 139 PointF r = new PointF(); 140 r.readFromParcel(in); 141 return r; 142 } 143 144 /** 145 * Return an array of rectangles of the specified size. 146 */ 147 public PointF[] newArray(int size) { 148 return new PointF[size]; 149 } 150 }; 151 152 /** 153 * Set the point's coordinates from the data stored in the specified 154 * parcel. To write a point to a parcel, call writeToParcel(). 155 * 156 * @param in The parcel to read the point's coordinates from 157 */ 158 public void readFromParcel(Parcel in) { 159 x = in.readFloat(); 160 y = in.readFloat(); 161 } 162 } 163