blob: 8e4288e35c42af68ef3231de951b5d644ae50c15 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.graphics;
18
Dianne Hackborn44bc17c2011-04-20 18:18:51 -070019import android.os.Parcel;
20import android.os.Parcelable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021
22
23/**
24 * PointF holds two float coordinates
25 */
Dianne Hackborn44bc17c2011-04-20 18:18:51 -070026public class PointF implements Parcelable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027 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 Guy9f8af652012-11-20 18:35:20 -080075 @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 Project9066cfe2009-03-03 19:31:44 -0800100 /**
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 Fuller33253a42014-10-01 11:55:10 +0100111 return (float) Math.hypot(x, y);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113
Dianne Hackborn44bc17c2011-04-20 18:18:51 -0700114 /**
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}