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
17package android.graphics;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21import android.util.FloatMath;
22
23
24/**
25 * PointF holds two float coordinates
26 */
27public 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    /**
77     * Return the euclidian distance from (0,0) to the point
78     */
79    public final float length() {
80        return length(x, y);
81    }
82
83    /**
84     * Returns the euclidian distance from (0,0) to (x,y)
85     */
86    public static float length(float x, float y) {
87        return FloatMath.sqrt(x * x + y * y);
88    }
89
90    /**
91     * Parcelable interface methods
92     */
93    @Override
94    public int describeContents() {
95        return 0;
96    }
97
98    /**
99     * Write this point to the specified parcel. To restore a point from
100     * a parcel, use readFromParcel()
101     * @param out The parcel to write the point's coordinates into
102     */
103    @Override
104    public void writeToParcel(Parcel out, int flags) {
105        out.writeFloat(x);
106        out.writeFloat(y);
107    }
108
109    public static final Parcelable.Creator<PointF> CREATOR = new Parcelable.Creator<PointF>() {
110        /**
111         * Return a new point from the data in the specified parcel.
112         */
113        public PointF createFromParcel(Parcel in) {
114            PointF r = new PointF();
115            r.readFromParcel(in);
116            return r;
117        }
118
119        /**
120         * Return an array of rectangles of the specified size.
121         */
122        public PointF[] newArray(int size) {
123            return new PointF[size];
124        }
125    };
126
127    /**
128     * Set the point's coordinates from the data stored in the specified
129     * parcel. To write a point to a parcel, call writeToParcel().
130     *
131     * @param in The parcel to read the point's coordinates from
132     */
133    public void readFromParcel(Parcel in) {
134        x = in.readFloat();
135        y = in.readFloat();
136    }
137}
138