home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 27 / IOPROG_27.ISO / SOFT / CDX.ZIP / Src / C3d / Vector.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-09-10  |  5.0 KB  |  153 lines

  1. //////////////////////////////////////////////////////////////////////////////////
  2. // Project Name: [ C3D Class Library - C3D.lib ]
  3. // Author:       [ Dan Farley - 97308096@brookes.ac.uk ]
  4. // Source File:  [ C3D_Vector Implementation ]
  5. // Revision:     [ 1.6 ]
  6. //////////////////////////////////////////////////////////////////////////////////
  7. #include "C3D.h"
  8. #include <math.h>
  9.  
  10. //////////////////////////////////////////////////////////////////////////////////
  11. // C3D_Vector constructor
  12. //////////////////////////////////////////////////////////////////////////////////
  13. C3D_Vector::C3D_Vector(void)
  14. {
  15.     x = y = z = 0.0f;
  16. }
  17.  
  18. //////////////////////////////////////////////////////////////////////////////////
  19. // C3D_Vector constructor
  20. //////////////////////////////////////////////////////////////////////////////////
  21. C3D_Vector::C3D_Vector(float f)
  22. {
  23.     x = y = z = f;
  24. }
  25.  
  26. //////////////////////////////////////////////////////////////////////////////////
  27. // C3D_Vector constructor
  28. //////////////////////////////////////////////////////////////////////////////////
  29. C3D_Vector::C3D_Vector(float _x, float _y, float _z)
  30. {
  31.     x = _x;
  32.     y = _y;
  33.     z = _z;
  34. }
  35.  
  36. //////////////////////////////////////////////////////////////////////////////////
  37. // C3D_Vector copy constructor
  38. //////////////////////////////////////////////////////////////////////////////////
  39. C3D_Vector::C3D_Vector(C3D_Vector* v)
  40. {
  41.     x = v->x;
  42.     y = v->y;
  43.     z = v->z;
  44. }
  45.  
  46. //////////////////////////////////////////////////////////////////////////////////
  47. // C3D_Vector = operator
  48. //////////////////////////////////////////////////////////////////////////////////
  49. C3D_Vector &C3D_Vector::operator = (const C3D_Vector &vector)
  50. {
  51.     x = vector.x;
  52.     y = vector.y;
  53.     z = vector.z;
  54.  
  55.     return *this;
  56. }
  57.  
  58. //////////////////////////////////////////////////////////////////////////////////
  59. // C3D_Vector = operator
  60. //////////////////////////////////////////////////////////////////////////////////
  61. C3D_Vector &C3D_Vector::operator = (const D3DVECTOR &vector)
  62. {
  63.     x = vector.x;
  64.     y = vector.y;
  65.     z = vector.z;
  66.  
  67.     return *this;
  68. }
  69.  
  70. //////////////////////////////////////////////////////////////////////////////////
  71. // C3D_Vector + operator
  72. //////////////////////////////////////////////////////////////////////////////////
  73. C3D_Vector &C3D_Vector::operator + (const C3D_Vector &vector)
  74. {
  75.     return C3D_Vector(x + vector.x, y + vector.y, z + vector.z);
  76. }
  77.  
  78. //////////////////////////////////////////////////////////////////////////////////
  79. // C3D_Vector + operator
  80. //////////////////////////////////////////////////////////////////////////////////
  81. C3D_Vector &C3D_Vector::operator + (const C3D_Vertex &vertex)
  82. {
  83.     return C3D_Vector(x + vertex.x, y + vertex.y, z + vertex.z);
  84. }
  85.  
  86. //////////////////////////////////////////////////////////////////////////////////
  87. // C3D_Vector - operator
  88. //////////////////////////////////////////////////////////////////////////////////
  89. C3D_Vector &C3D_Vector::operator - (const C3D_Vector &vector)
  90. {
  91.     return C3D_Vector(x - vector.x, y - vector.y, z - vector.z);
  92. }
  93.  
  94. //////////////////////////////////////////////////////////////////////////////////
  95. // C3D_Vector Normalize
  96. //////////////////////////////////////////////////////////////////////////////////
  97. void C3D_Vector::Normalize(void)
  98. {
  99.     float d = sqrt(DotProduct(this, this));
  100.     x = x/d;
  101.     y = y/d;
  102.     z = z/d;
  103. }
  104.  
  105. //////////////////////////////////////////////////////////////////////////////////
  106. // C3D_Vector Normal
  107. //////////////////////////////////////////////////////////////////////////////////
  108. void C3D_Vector::Normal(C3D_Vertex* a, C3D_Vertex* b, C3D_Vertex* c)
  109. {
  110.     C3D_Vector v1, v2;
  111.  
  112.     v1.x = b->x - a->x; v1.y = b->y - a->y; v1.z = b->z - a->z;
  113.     v2.x = c->x - a->x; v2.y = c->y - a->y; v2.z = c->z - a->z;
  114.     CrossProduct(&v1, &v2);
  115.     Normalize();
  116. }
  117.  
  118. //////////////////////////////////////////////////////////////////////////////////
  119. // C3D_Vector CrossProduct
  120. //////////////////////////////////////////////////////////////////////////////////
  121. void C3D_Vector::CrossProduct(C3D_Vector* a, C3D_Vector* b)
  122. {
  123.     x = a->y * b->z - a->z * b->y;
  124.     y = a->z * b->x - a->x * b->z;
  125.     z = a->x * b->y - a->y * b->x;
  126. }
  127.  
  128. //////////////////////////////////////////////////////////////////////////////////
  129. // C3D_Vector DotProduct
  130. //////////////////////////////////////////////////////////////////////////////////
  131. float C3D_Vector::DotProduct(C3D_Vector* a, C3D_Vector* b)
  132. {
  133.     return(a->x * b->x + a->y * b->y + a->z * b->z);
  134. }
  135.  
  136. //////////////////////////////////////////////////////////////////////////////////
  137. // C3D_Vector Locate
  138. //////////////////////////////////////////////////////////////////////////////////
  139. void C3D_Vector::Locate(float _x, float _y, float _z)
  140. {
  141.     x = _x;
  142.     y = _y;
  143.     z = _z;
  144. }
  145.  
  146. //////////////////////////////////////////////////////////////////////////////////
  147. // C3D_Vector Print
  148. //////////////////////////////////////////////////////////////////////////////////
  149. void C3D_Vector::Print(void)
  150. {
  151.     printf("x = %.5f, y = %.5f, z = %.5f\n", x, y, z);
  152. }
  153.