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