Point.h revision edbf3b6af777b721cd2a1ef461947e51e88241e1
1/*
2 * Copyright (C) 2006 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#ifndef ANDROID_UI_POINT
18#define ANDROID_UI_POINT
19
20#include <utils/TypeHelpers.h>
21
22namespace android {
23
24class Point
25{
26public:
27    int x;
28    int y;
29
30    // we don't provide copy-ctor and operator= on purpose
31    // because we want the compiler generated versions
32
33    // Default constructor doesn't initialize the Point
34    inline Point()
35    {
36    }
37
38    inline Point(int _x, int _y) : x(_x), y(_y)
39    {
40    }
41
42    inline bool operator == (const Point& rhs) const {
43        return (x == rhs.x) && (y == rhs.y);
44    }
45    inline bool operator != (const Point& rhs) const {
46        return !operator == (rhs);
47    }
48
49    inline bool isOrigin() const {
50        return !(x|y);
51    }
52
53    // operator < defines an order which allows to use points in sorted
54    // vectors.
55    bool operator < (const Point& rhs) const {
56        return y<rhs.y || (y==rhs.y && x<rhs.x);
57    }
58
59    inline Point& operator - () {
60        x=-x;
61        y=-y;
62        return *this;
63    }
64
65    inline Point& operator += (const Point& rhs) {
66        x += rhs.x;
67        y += rhs.y;
68        return *this;
69    }
70    inline Point& operator -= (const Point& rhs) {
71        x -= rhs.x;
72        y -= rhs.y;
73        return *this;
74    }
75
76    Point operator + (const Point& rhs) const {
77        return Point(x+rhs.x, y+rhs.y);
78    }
79    Point operator - (const Point& rhs) const {
80        return Point(x-rhs.x, y-rhs.y);
81    }
82};
83
84ANDROID_BASIC_TYPES_TRAITS(Point)
85
86}; // namespace android
87
88#endif // ANDROID_UI_POINT
89