The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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 | |
| 22 | namespace android { |
| 23 | |
| 24 | class Point |
| 25 | { |
| 26 | public: |
| 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 |
Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 34 | inline Point() { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | } |
Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 36 | inline Point(int x, int y) : x(x), y(y) { |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | inline bool operator == (const Point& rhs) const { |
| 40 | return (x == rhs.x) && (y == rhs.y); |
| 41 | } |
| 42 | inline bool operator != (const Point& rhs) const { |
| 43 | return !operator == (rhs); |
| 44 | } |
| 45 | |
| 46 | inline bool isOrigin() const { |
| 47 | return !(x|y); |
| 48 | } |
| 49 | |
| 50 | // operator < defines an order which allows to use points in sorted |
| 51 | // vectors. |
| 52 | bool operator < (const Point& rhs) const { |
| 53 | return y<rhs.y || (y==rhs.y && x<rhs.x); |
| 54 | } |
| 55 | |
| 56 | inline Point& operator - () { |
Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 57 | x = -x; |
| 58 | y = -y; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | return *this; |
| 60 | } |
| 61 | |
| 62 | inline Point& operator += (const Point& rhs) { |
| 63 | x += rhs.x; |
| 64 | y += rhs.y; |
| 65 | return *this; |
| 66 | } |
| 67 | inline Point& operator -= (const Point& rhs) { |
| 68 | x -= rhs.x; |
| 69 | y -= rhs.y; |
| 70 | return *this; |
| 71 | } |
| 72 | |
Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 73 | const Point operator + (const Point& rhs) const { |
| 74 | const Point result(x+rhs.x, y+rhs.y); |
| 75 | return result; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | } |
Mathias Agopian | 35801ce | 2009-05-26 17:44:57 -0700 | [diff] [blame] | 77 | const Point operator - (const Point& rhs) const { |
| 78 | const Point result(x-rhs.x, y-rhs.y); |
| 79 | return result; |
The Android Open Source Project | edbf3b6 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | } |
| 81 | }; |
| 82 | |
| 83 | ANDROID_BASIC_TYPES_TRAITS(Point) |
| 84 | |
| 85 | }; // namespace android |
| 86 | |
| 87 | #endif // ANDROID_UI_POINT |