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;
21
22
23/**
24 * Point holds two integer coordinates
25 */
26public class Point implements Parcelable {
27    public int x;
28    public int y;
29
30    public Point() {}
31
32    public Point(int x, int y) {
33        this.x = x;
34        this.y = y;
35    }
36
37    public Point(Point src) {
38        this.x = src.x;
39        this.y = src.y;
40    }
41
42    /**
43     * Set the point's x and y coordinates
44     */
45    public void set(int x, int y) {
46        this.x = x;
47        this.y = y;
48    }
49
50    /**
51     * Negate the point's coordinates
52     */
53    public final void negate() {
54        x = -x;
55        y = -y;
56    }
57
58    /**
59     * Offset the point's coordinates by dx, dy
60     */
61    public final void offset(int dx, int dy) {
62        x += dx;
63        y += dy;
64    }
65
66    /**
67     * Returns true if the point's coordinates equal (x,y)
68     */
69    public final boolean equals(int x, int y) {
70        return this.x == x && this.y == y;
71    }
72
73    @Override public boolean equals(Object o) {
74        if (o instanceof Point) {
75            Point p = (Point) o;
76            return this.x == p.x && this.y == p.y;
77        }
78        return false;
79    }
80
81    @Override public int hashCode() {
82        return x * 32713 + y;
83    }
84
85    @Override public String toString() {
86        return "Point(" + x + ", " + y+ ")";
87    }
88
89    /**
90     * Parcelable interface methods
91     */
92    @Override
93    public int describeContents() {
94        return 0;
95    }
96
97    /**
98     * Write this point to the specified parcel. To restore a point from
99     * a parcel, use readFromParcel()
100     * @param out The parcel to write the point's coordinates into
101     */
102    @Override
103    public void writeToParcel(Parcel out, int flags) {
104        out.writeInt(x);
105        out.writeInt(y);
106    }
107
108    public static final Parcelable.Creator<Point> CREATOR = new Parcelable.Creator<Point>() {
109        /**
110         * Return a new point from the data in the specified parcel.
111         */
112        public Point createFromParcel(Parcel in) {
113            Point r = new Point();
114            r.readFromParcel(in);
115            return r;
116        }
117
118        /**
119         * Return an array of rectangles of the specified size.
120         */
121        public Point[] newArray(int size) {
122            return new Point[size];
123        }
124    };
125
126    /**
127     * Set the point's coordinates from the data stored in the specified
128     * parcel. To write a point to a parcel, call writeToParcel().
129     *
130     * @param in The parcel to read the point's coordinates from
131     */
132    public void readFromParcel(Parcel in) {
133        x = in.readInt();
134        y = in.readInt();
135    }
136}
137