1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
//#include <GL/glut.h>
#include <iostream>
#include <cmath>
int angle = 0;
float a, b, c;
float i, j, k;
/* Callback when no events occurs */
template<typename T>
class Vec3
{
public:
// 3 most basic ways of initializing a vector
Vec3() : x(T(0)), y(T(0)), z(T(0)) {}
Vec3(const T &xx) : x(xx), y(xx), z(xx) {}
Vec3(T xx, T yy, T zz) : x(xx), y(yy), z(zz) {}
T x, y, z;
T length()
{
return sqrt(x * x + y * y + z * z);
}
};
//
//template<typename T>
//class Vec3
//{
//public:
// ...
// // length can be a method from the class...
// T length()
//{
// return sqrt(x * x + y * y + z * z);
//}
// ...
//};
// ... or you can also compute the length in a function which is not part of the class
//
template<typename T>
T length(const Vec3<T> &v)
{
return sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
}
template<typename T>
T normalizeX(const Vec3<T> &x_)
{
float len2;
// avoid division by 0
len2 = x_.x * x_.x + x_.y * x_.y + x_.z * x_.z;
if (len2 > 0) {
float invLen = 1 / sqrt(len2);
i *= invLen, j *= invLen, k *= invLen;
}
return i;
}
template<typename T>
T normalizeY(const Vec3<T> &y_)
{
float len2;
// avoid division by 0
len2 = y_.x * y_.x + y_.y * y_.y + y_.z * y_.z;
if (len2 > 0) {
float invLen = 1 / sqrt(len2);
i *= invLen, j *= invLen, k *= invLen;
}
return j;
}
template<typename T>
T normalizeZ(const Vec3<T> &z_)
{
float len2;
// avoid division by 0
len2 = z_.x * z_.x + z_.y * z_.y + z_.z * z_.z;
if (len2 > 0) {
float invLen = 1 / sqrt(len2);
i *= invLen, j *= invLen, k *= invLen;
}
return k;
}
template<typename T>
T dot(const Vec3<T> &a, const Vec3<T> &b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
//template<typename T>
//void normalize(Vec3<T> &v)
//{
// T len2 = v.x * v.x + v.y * v.y + v.z * v.z;
// // avoid division by 0
//if (len2 > 0) {
// T invLen = 1 / sqrt(len2);
// x *= invLen, y *= invLen, z *= invLen;
//}
//}
int main() {
std::cout << "Enter a:";
std::cin>> a;
std::cout << "Enter b:";
std::cin >> b;
std::cout << "Enter c:";
std::cin >> c;
std::cout<<length(Vec3<float>{a, b, c})<<std::endl;
//std::cout<<length<float>({x, y, z});
Vec3<float> v{ a, b, c };
std::cout << v.length();
std::cout << "\n\nNormalizing Vectors \n";
std::cout << normalizeX<float>({ a, b, c }); \\should return 0.26726 but showed 0.
std::cout << " ";
std::cout << normalizeY<float>({ a, b, c }); \\should return 0.53452 but showed 0.
std::cout << " ";
std::cout << normalizeZ<float>({ a, b, c }); \\should return 0.80178 but showed 0.
std::cout << "\n";
std::cout << "Dot Products \n";
std::cout << dot<float>({ a, b, c }, { 1, 2, 3 });
std::cout << "\n";
system("pause");
return 0;
}
| |