소스 검색

vector math: add implementation of Length and Normalize

wwylele 9 년 전
부모
커밋
2e6d8e1321
1개의 변경된 파일19개의 추가작업 그리고 0개의 파일을 삭제
  1. 19 0
      src/common/vector_math.h

+ 19 - 0
src/common/vector_math.h

@@ -186,6 +186,18 @@ Vec2<T> operator*(const V& f, const Vec2<T>& vec) {
 
 
 typedef Vec2<float> Vec2f;
 typedef Vec2<float> Vec2f;
 
 
+template <>
+inline float Vec2<float>::Length() const {
+    return std::sqrt(x * x + y * y);
+}
+
+template <>
+inline float Vec2<float>::Normalize() {
+    float length = Length();
+    *this /= length;
+    return length;
+}
+
 template <typename T>
 template <typename T>
 class Vec3 {
 class Vec3 {
 public:
 public:
@@ -388,6 +400,13 @@ inline Vec3<float> Vec3<float>::Normalized() const {
     return *this / Length();
     return *this / Length();
 }
 }
 
 
+template <>
+inline float Vec3<float>::Normalize() {
+    float length = Length();
+    *this /= length;
+    return length;
+}
+
 typedef Vec3<float> Vec3f;
 typedef Vec3<float> Vec3f;
 
 
 template <typename T>
 template <typename T>