Newer
Older
cg / sources / vec.cpp
@glproj03 glproj03 on 29 Jan 2006 1 KB Did a lot off stuff :-)
  1. #include "vec.h"
  2.  
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <string>
  6.  
  7. Vec::Vec() {
  8. Vec(0.0f, 0.0f, 0.0f);
  9. }
  10.  
  11. Vec::Vec(float vx, float vy, float vz) {
  12. c[0] = vx;
  13. c[1] = vy;
  14. c[2] = vz;
  15. }
  16.  
  17. Vec::Vec(Vec* v) {
  18. Vec(v->x(), v->y(), v->z());
  19. /* TODO: copy registered faces ?? */
  20. }
  21.  
  22. float Vec::x() {
  23. return c[0];
  24. }
  25.  
  26. float Vec::y() {
  27. return c[1];
  28. }
  29.  
  30. float Vec::z() {
  31. return c[2];
  32. }
  33.  
  34. float* Vec::getCoords() {
  35. return c;
  36. }
  37.  
  38. Vec Vec::operator+(Vec* v) {
  39. return Vec(x() + v->x(), y() + v->y(), z() + v->z());
  40. }
  41.  
  42. Vec Vec::operator+(Vec v) {
  43. return Vec(x() + v.x(), y() + v.y(), z() + v.z());
  44. }
  45.  
  46. void Vec::add(Vec a) {
  47. c[0] += a.x();
  48. c[1] += a.y();
  49. c[2] += a.z();
  50. }
  51.  
  52. void Vec::sub(Vec a) {
  53. c[0] -= a.x();
  54. c[1] -= a.y();
  55. c[2] -= a.z();
  56. }
  57.  
  58. void Vec::add(Vec& a) {
  59. c[0] += a.x();
  60. c[1] += a.y();
  61. c[2] += a.z();
  62. }
  63.  
  64. void Vec::sub(Vec& a) {
  65. c[0] -= a.x();
  66. c[1] -= a.y();
  67. c[2] -= a.z();
  68. }
  69.  
  70. Vec Vec::operator-(Vec& v) {
  71. return Vec(x() - v.x(), y() - v.y(), z() - v.z());
  72. }
  73.  
  74. Vec Vec::operator*(float s) {
  75. Vec r = Vec(x()*s, y()*s, z()*s);
  76. return r;
  77. }
  78.  
  79. Vec Vec::operator*(Vec& s) {
  80. return Vec(x()*s.x(), y()*s.y(), z()*s.z());
  81. }
  82.  
  83. Vec Vec::operator/(float s) {
  84. return Vec(x()/s, y()/s, z()/s);
  85. }
  86.  
  87. float Vec::angle(Vec& v) {
  88. return acos((*this).dotP(v)/((*this).length()*v.length()));
  89. }
  90.  
  91. float Vec::dotP(Vec &v) {
  92. return x()*v.x() + y()*v.y() + z()*v.z();
  93. }
  94.  
  95. Vec Vec::cross(Vec& v) {
  96. return Vec(y() * v.z() - z() * v.y(), z() * v.x() - x() * v.z(), x() * v.y() - y() * v.x());
  97. }
  98.  
  99. Vec Vec::normalize() {
  100. return *this * (1 / length());
  101. }
  102.  
  103. float Vec::length() {
  104. return sqrt(x()*x()+y()*y()+z()*z());
  105. }
  106.  
  107. void Vec::print() {
  108. printf (" vektor: %f, %f, %f\n", x(), y(), z());
  109. }
  110.  
  111. Vec* Vec::clone() {
  112. return new Vec(x(), y(), z());
  113. }
  114.  
  115. void Vec::vardump(std::string whitespace) {
  116. printf("%sVec(%f, %f, %f)\n", whitespace.c_str(), c[0], c[1], c[2]);
  117. printf("%s linked to %d faces\n", whitespace.c_str(), faces.size());
  118. }
  119.  
  120. void Vec::registerFace(SPoly* p) {
  121. faces.push_back(p);
  122. }