Vector2D 예제

Programming/C,C++ 2017. 2. 25. 15:32

Vector2D_2.zip



#pragma once


template<class T>

class Vector2D

{

public:

T x_, y_;


Vector2D()

:x_(0.0f), y_(0.0f)

{}


Vector2D(const T &_x, const T& _y)

:x_(_x), y_(_y)

{}


void print()

{

printf("%f %f", x_, y_);

}


T getMagnitue()

{

return sqrt(x_*x_ + y_*y_);

}


void normalize()

{

const T magnitude = getMagnitue();


x_ /= magnitude;

y_ /= magnitude;

}


friend std::ostream& operator << (std::ostream& stream, const Vector2D& vec)

{

std::cout << vec.x_ << " " << vec.y_ << std::endl;


return stream;

}

};


template<class T>

T dotProduct(const Vector2D<T>& vec0, const Vector2D<T>& vec1)

{

return vec0.x_ * vec1.x_ + vec0.y_ * vec1.y_;

}



'Programming > C,C++' 카테고리의 다른 글

#pragma pack, pop[펌]  (0) 2017.02.27
Piano 예제  (0) 2017.02.26
function pointer 예제  (0) 2017.02.25
STL list 예제 코드[펌]  (0) 2017.02.14
Template 예제  (0) 2017.01.24
Posted by 루나s
,