Vector.h revision 1aa5d2d7068147ff781cfe911a93f01593a68c79
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
27// MUST BE A POD - this means no ctor or dtor!
28struct Vector2 {
29    float x;
30    float y;
31
32    float lengthSquared() const {
33        return x * x + y * y;
34    }
35
36    float length() const {
37        return sqrt(x * x + y * y);
38    }
39
40    void operator+=(const Vector2& v) {
41        x += v.x;
42        y += v.y;
43    }
44
45    void operator-=(const Vector2& v) {
46        x -= v.x;
47        y -= v.y;
48    }
49
50    void operator+=(const float v) {
51        x += v;
52        y += v;
53    }
54
55    void operator-=(const float v) {
56        x -= v;
57        y -= v;
58    }
59
60    void operator/=(float s) {
61        x /= s;
62        y /= s;
63    }
64
65    void operator*=(float s) {
66        x *= s;
67        y *= s;
68    }
69
70    Vector2 operator+(const Vector2& v) const {
71        return (Vector2){x + v.x, y + v.y};
72    }
73
74    Vector2 operator-(const Vector2& v) const {
75        return (Vector2){x - v.x, y - v.y};
76    }
77
78    Vector2 operator/(float s) const {
79        return (Vector2){x / s, y / s};
80    }
81
82    Vector2 operator*(float s) const {
83        return (Vector2){x * s, y * s};
84    }
85
86    void normalize() {
87        float s = 1.0f / length();
88        x *= s;
89        y *= s;
90    }
91
92    Vector2 copyNormalized() const {
93        Vector2 v = {x, y};
94        v.normalize();
95        return v;
96    }
97
98    float dot(const Vector2& v) const {
99        return x * v.x + y * v.y;
100    }
101
102    void dump() {
103        ALOGD("Vector2[%.2f, %.2f]", x, y);
104    }
105}; // class Vector2
106
107// MUST BE A POD - this means no ctor or dtor!
108class Vector3 {
109public:
110    float x;
111    float y;
112    float z;
113
114    void dump() {
115        ALOGD("Vector3[%.2f, %.2f, %.2f]", x, y, z);
116    }
117};
118
119}; // namespace uirenderer
120}; // namespace android
121
122#endif // ANDROID_HWUI_VECTOR_H
123