• 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         Vec2<float> temp();
45         assert(param != 0);
46         temp.set(static_cast<float>(mX) / static_cast<float>(param),
47                  static_cast<float>(mY) / static_cast<float>(param));
48 
49         return temp;
50     }
51 
52     template <class U>
squareDistance(const Vec2<U> & param)53     float squareDistance(const Vec2<U> &param) const {
54         int difference = 0.f;
55         difference = (static_cast<float>(mX) - static_cast<float>(param.x())) *
56               (static_cast<float>(mX) - static_cast<float>(param.x())) +
57               (static_cast<float>(mY) - static_cast<float>(param.y())) *
58               (static_cast<float>(mY) - static_cast<float>(param.y()));
59         return difference;
60     }
61 
x()62     inline T x() const { return mX; }
y()63     inline T y() const { return mY; }
64 
set(const T inputX,const T inputY)65     inline void set(const T inputX, const T inputY) {
66         mX = inputX;
67         mY = inputY;
68     }
69 
70   private:
71     T mX;
72     T mY;
73 };
74 
75 typedef Vec2<int> Vec2i;
76 typedef Vec2<float> Vec2f;
77 #endif
78