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