Vector.h revision b79a3e301a8d89b9e1b1f6f3d7fd6aa56610a6f0
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 20namespace android { 21namespace uirenderer { 22 23/////////////////////////////////////////////////////////////////////////////// 24// Classes 25/////////////////////////////////////////////////////////////////////////////// 26 27struct 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 lengthSquared() const { 40 return x * x + y * y; 41 } 42 43 float length() const { 44 return sqrt(x * x + y * y); 45 } 46 47 void operator+=(const Vector2& v) { 48 x += v.x; 49 y += v.y; 50 } 51 52 void operator-=(const Vector2& v) { 53 x -= v.x; 54 y -= v.y; 55 } 56 57 void operator+=(const float v) { 58 x += v; 59 y += v; 60 } 61 62 void operator-=(const float v) { 63 x -= v; 64 y -= v; 65 } 66 67 void operator/=(float s) { 68 x /= s; 69 y /= s; 70 } 71 72 void operator*=(float s) { 73 x *= s; 74 y *= s; 75 } 76 77 Vector2 operator+(const Vector2& v) const { 78 return Vector2(x + v.x, y + v.y); 79 } 80 81 Vector2 operator-(const Vector2& v) const { 82 return Vector2(x - v.x, y - v.y); 83 } 84 85 Vector2 operator/(float s) const { 86 return Vector2(x / s, y / s); 87 } 88 89 Vector2 operator*(float s) const { 90 return Vector2(x * s, y * s); 91 } 92 93 void normalize() { 94 float s = 1.0f / length(); 95 x *= s; 96 y *= s; 97 } 98 99 Vector2 copyNormalized() const { 100 Vector2 v(x, y); 101 v.normalize(); 102 return v; 103 } 104 105 float dot(const Vector2& v) const { 106 return x * v.x + y * v.y; 107 } 108 109 void dump() { 110 ALOGD("Vector2[%.2f, %.2f]", x, y); 111 } 112}; // class Vector2 113 114class Vector3 { 115public: 116 float x; 117 float y; 118 float z; 119 120 Vector3() : 121 x(0.0f), y(0.0f), z(0.0f) { 122 } 123 124 Vector3(float px, float py, float pz) : 125 x(px), y(py), z(pz) { 126 } 127 128 void dump() { 129 ALOGD("Vector3[%.2f, %.2f, %.2f]", x, y, z); 130 } 131}; 132 133/////////////////////////////////////////////////////////////////////////////// 134// Types 135/////////////////////////////////////////////////////////////////////////////// 136 137typedef Vector2 vec2; 138 139}; // namespace uirenderer 140}; // namespace android 141 142#endif // ANDROID_HWUI_VECTOR_H 143