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