Romain Guy | a957eea | 2010-12-08 18:34:42 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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_HWUI_VECTOR_H |
| 18 | #define ANDROID_HWUI_VECTOR_H |
| 19 | |
| 20 | namespace android { |
| 21 | namespace uirenderer { |
| 22 | |
| 23 | /////////////////////////////////////////////////////////////////////////////// |
| 24 | // Classes |
| 25 | /////////////////////////////////////////////////////////////////////////////// |
| 26 | |
| 27 | struct Vector2 { |
| 28 | float x; |
| 29 | float y; |
| 30 | |
| 31 | Vector2() : |
| 32 | x(0.0f), y(0.0f) { |
| 33 | } |
| 34 | |
| 35 | Vector2(float px, float py) : |
| 36 | x(px), y(py) { |
| 37 | } |
| 38 | |
| 39 | float length() const { |
| 40 | return sqrt(x * x + y * y); |
| 41 | } |
| 42 | |
| 43 | void operator+=(const Vector2& v) { |
| 44 | x += v.x; |
| 45 | y += v.y; |
| 46 | } |
| 47 | |
| 48 | void operator-=(const Vector2& v) { |
| 49 | x -= v.x; |
| 50 | y -= v.y; |
| 51 | } |
| 52 | |
| 53 | void operator+=(const float v) { |
| 54 | x += v; |
| 55 | y += v; |
| 56 | } |
| 57 | |
| 58 | void operator-=(const float v) { |
| 59 | x -= v; |
| 60 | y -= v; |
| 61 | } |
| 62 | |
| 63 | void operator/=(float s) { |
| 64 | x /= s; |
| 65 | y /= s; |
| 66 | } |
| 67 | |
| 68 | void operator*=(float s) { |
| 69 | x *= s; |
| 70 | y *= s; |
| 71 | } |
| 72 | |
| 73 | Vector2 operator+(const Vector2& v) const { |
| 74 | return Vector2(x + v.x, y + v.y); |
| 75 | } |
| 76 | |
| 77 | Vector2 operator-(const Vector2& v) const { |
| 78 | return Vector2(x - v.x, y - v.y); |
| 79 | } |
| 80 | |
| 81 | Vector2 operator/(float s) const { |
| 82 | return Vector2(x / s, y / s); |
| 83 | } |
| 84 | |
| 85 | Vector2 operator*(float s) const { |
| 86 | return Vector2(x * s, y * s); |
| 87 | } |
| 88 | |
| 89 | void normalize() { |
| 90 | float s = 1.0f / length(); |
| 91 | x *= s; |
| 92 | y *= s; |
| 93 | } |
| 94 | |
| 95 | Vector2 copyNormalized() const { |
| 96 | Vector2 v(x, y); |
| 97 | v.normalize(); |
| 98 | return v; |
| 99 | } |
| 100 | |
| 101 | float dot(const Vector2& v) const { |
| 102 | return x * v.x + y * v.y; |
| 103 | } |
| 104 | |
| 105 | void dump() { |
| 106 | LOGD("Vector2[%.2f, %.2f]", x, y); |
| 107 | } |
| 108 | }; // class Vector2 |
| 109 | |
| 110 | /////////////////////////////////////////////////////////////////////////////// |
| 111 | // Types |
| 112 | /////////////////////////////////////////////////////////////////////////////// |
| 113 | |
| 114 | typedef Vector2 vec2; |
| 115 | |
| 116 | }; // namespace uirenderer |
| 117 | }; // namespace android |
| 118 | |
| 119 | #endif // ANDROID_HWUI_VECTOR_H |