• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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  ** Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef VEC2_H
17 #define VEC2_H
18 
19 #include <assert.h>
20 #include <cmath>
21 
22 // Implements a class to represent the location of a pixel.
23 template <class T>
24 class Vec2{
25   public:
Vec2(T inputX,T inputY)26     Vec2(T inputX, T inputY) {
27         mX = inputX;
28         mY = inputY;
29     }
30 
Vec2()31     Vec2() {}
32 
33     inline Vec2<T> operator+ (const Vec2<T> &param) const {
34         Vec2<T> temp(mX + param.x(), mY + param.y());
35         return temp;
36     }
37 
38     inline Vec2<T> operator- (const Vec2<T> &param) const {
39         Vec2<T> temp(mX - param.x(), mY - param.y());
40         return temp;
41     }
42 
43     inline Vec2<float> operator/ (const int param) const {
44         assert(param != 0);
45         return Vec2<float>(static_cast<float>(mX) / static_cast<float>(param),
46                            static_cast<float>(mY) / static_cast<float>(param));
47     }
48 
49     template <class U>
squareDistance(const Vec2<U> & param)50     float squareDistance(const Vec2<U> &param) const {
51         int difference = 0.f;
52         difference = (static_cast<float>(mX) - static_cast<float>(param.x())) *
53               (static_cast<float>(mX) - static_cast<float>(param.x())) +
54               (static_cast<float>(mY) - static_cast<float>(param.y())) *
55               (static_cast<float>(mY) - static_cast<float>(param.y()));
56         return difference;
57     }
58 
x()59     inline T x() const { return mX; }
y()60     inline T y() const { return mY; }
61 
set(const T inputX,const T inputY)62     inline void set(const T inputX, const T inputY) {
63         mX = inputX;
64         mY = inputY;
65     }
66 
67   private:
68     T mX;
69     T mY;
70 };
71 
72 typedef Vec2<int> Vec2i;
73 typedef Vec2<float> Vec2f;
74 #endif
75