You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
6.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. #pragma once
  2. #include <cmath>
  3. #include <stdio.h>
  4. #include <cassert>
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <stdarg.h>
  8. #define PRECISION 0.000001
  9. #define SQRT(X) sqrt((X)) //��ƽ����
  10. #define SQR(X) ((X)*(X)) //��ƽ��
  11. #define ABS(X) abs((X))
  12. #define EQ(X,Y,EPS) (ABS((X)-(Y))<EPS)//ͨ������ȥ�жϣ��������Ƿ�����
  13. #define EQ_ZERO(X,EPS) (ABS((X))<EPS)
  14. #define ARR_ZERO(A, N) memset((A), 0, sizeof(A[0])*(N))
  15. #define MIN(x,y) (x)<(y)?(x):(y);
  16. #define SWAP(type, x, y) {type tmp=(x); (x)=(y); (y) = tmp;}
  17. using namespace std;
  18. class GVector3;
  19. class GVector;
  20. class GMatrix;
  21. typedef GVector3 GPoint3;
  22. class GVector3 {
  23. private:
  24. float v[3];
  25. public:
  26. GVector3(float x = 0.0f, float y = 0.0f, float z = 0.0f);
  27. GVector3(const GVector3& copy);
  28. GVector3& operator =(const GVector3& rhs);
  29. //�����Ӽ�
  30. GVector3 operator +(const GVector3& rhs) const; // u + v
  31. GVector3 operator -(const GVector3& rhs) const; // u - v
  32. //�����ͳ����˳�
  33. friend GVector3 operator *(const GVector3& lhs, const float& k); // u * k
  34. friend GVector3 operator *(const float& k, const GVector3& rhs); // k * u
  35. friend GVector3 operator /(const GVector3& lhs, const float& k); // u / k
  36. //��������ģ�ͷ���
  37. friend float norm(const GVector3& v); //��������С��ģ������
  38. GVector3& normalize(); //���������� �����
  39. //���������ڻ�
  40. float operator *(const GVector3& rhs) const;
  41. //����ͶӰ
  42. friend GVector3 proj(const GVector3& p, const GVector3& q);
  43. friend GVector3 perp(const GVector3& p, const GVector3& q);
  44. //�������ڻ� ����
  45. GVector3 operator^(const GVector3& rhs) const;
  46. //����������1
  47. GVector3& Set(const float& x, const float& y, const float& z);
  48. friend float distance(const GVector3& v, const GVector3 u);
  49. //����������2
  50. GVector3& operator +=(const GVector3& rhs);
  51. GVector3& operator -=(const GVector3& rhs);
  52. friend ostream& operator<<(ostream& os, const GVector3& v);
  53. //����������3
  54. GVector3& operator *=(const float& k);
  55. GVector3& operator /=(const float& k);
  56. GVector3& operator ^=(const GVector3& rhs);
  57. //����������4
  58. bool operator ==(const GVector3 rhs) const;
  59. bool operator !=(const GVector3 rhs) const;
  60. //����������5
  61. GVector3 operator+() const;
  62. GVector3 operator-() const;
  63. float& operator [](const int& idx); //�±긳ֵ
  64. const float operator [](const int& idx) const;//�±���ȡ����
  65. };
  66. class GVector {
  67. public:
  68. //GVector 1~2
  69. GVector(int dim = 3);
  70. GVector(int dim, double x, ...);
  71. GVector(const GVector3& copy);
  72. GVector(const GVector& copy);
  73. ~GVector();
  74. //GVector 3
  75. GVector& Set(double x, ...);
  76. GVector& Set(float *p);
  77. //GVector 4
  78. GVector& operator =(const GVector& rhs);
  79. GVector& operator +=(const GVector& rhs);
  80. GVector& operator -=(const GVector& rhs);
  81. GVector& operator +=(const float& k);
  82. GVector& operator -=(const float& k);
  83. GVector &operator *=(const float &k);
  84. GVector &operator /=(const float &k);
  85. bool operator ==(const GVector& rhs) const;
  86. bool operator !=(const GVector& rhs) const;
  87. GVector operator +() const;
  88. GVector operator -() const;
  89. GVector operator +(const GVector& rhs) const;
  90. GVector operator -(const GVector& rhs) const;
  91. float operator *(const GVector& rhs) const;
  92. GVector operator /(const float& k) const;
  93. float& operator [](const int& idx);
  94. const float& operator [](const int& idx) const;
  95. GVector& Nornalize();
  96. int GetDim() const;
  97. friend GVector operator *(const float& k, const GVector& rhs);
  98. friend GVector operator *(const GVector& lhs, const float& k);
  99. friend GVector operator *(const GMatrix& m, const GVector& v);
  100. friend GMatrix operator *(const GVector& v, const GMatrix& m);
  101. friend float norm(const GVector& v);
  102. friend float distance(const GVector& v, const GVector& u);
  103. friend ostream& operator <<(ostream& os, const GVector& v);
  104. friend class GMatrix;
  105. private:
  106. int n;
  107. float* v;
  108. };
  109. class GMatrix {
  110. public:
  111. GMatrix(int row = 4, int col = 4, float *elem = NULL);
  112. GMatrix(const GMatrix& copy);
  113. ~GMatrix();
  114. GMatrix& operator =(const GMatrix& rhs);
  115. GMatrix& operator +=(const GMatrix& rhs);
  116. GMatrix& operator -=(const GMatrix& rhs);
  117. GMatrix& operator *=(const float& k);
  118. GMatrix& operator *=(const GMatrix& rhs);
  119. GMatrix& operator /=(const float& k);
  120. bool operator ==(const GMatrix& rhs) const;
  121. bool operator !=(const GMatrix& rhs) const;
  122. float* operator [](const int idx);
  123. const float* operator [](const int idx) const;
  124. GMatrix operator +() const;
  125. GMatrix operator -() const;
  126. GMatrix operator +(const GMatrix& rhs) const;
  127. GMatrix operator -(const GMatrix& rhs) const;
  128. GMatrix operator *(const GMatrix& rhs) const;
  129. GMatrix operator /(const float& k) const;
  130. GMatrix& SetTranspose();
  131. GMatrix& SetIdentity();
  132. GMatrix& SetZeros();
  133. GMatrix& SetRowVec(const int idx, const GVector& v);
  134. GMatrix& SetColVec(const int idx, const GVector& v);
  135. GVector GetRowVec(const int idx) const;
  136. GVector GetColVec(const int idx) const;
  137. GMatrix& ExchangeRows(const int idx0, const int idx1);
  138. GMatrix& ExchangeCols(const int idx0, const int idx1);
  139. int GetRowNum() const;
  140. int GetColNum() const;
  141. bool IsSquare() const;
  142. friend GMatrix operator *(const GMatrix& lhs, const float& k);
  143. friend GMatrix operator *(const float& k , const GMatrix& rhs);
  144. friend GVector operator *(const GMatrix& m, const GVector& v);
  145. friend GMatrix operator *(const GVector& v, const GMatrix& m);
  146. friend ostream& operator <<(ostream& os, const GMatrix& m);
  147. friend GMatrix RowEchelonForm(const GMatrix& m);
  148. friend GMatrix ReduceRowEchelonForm(const GMatrix& m);
  149. friend float* from_arr(const GMatrix& m);
  150. friend int Rank(const GMatrix& m);//��ȡ��������
  151. friend int Nullity(const GMatrix& m);//��ȡ�������㻯��
  152. friend GMatrix Mij(const GMatrix& m, int r, int c); //��������ʽ����
  153. friend float Det(const GMatrix& m);
  154. private:
  155. int r;
  156. int c;
  157. float *m;
  158. };
  159. class GLine {
  160. private:
  161. // l(t) = p + t*v;
  162. GPoint3 p;//�˵�
  163. GVector3 v;//����
  164. public:
  165. GLine(const GPoint3& _p = GPoint3(0.0f, 0.0f, 0.0f), const GVector3& _v = GVector3(0.0f, 0.0f, 0.0f));
  166. GLine(const GLine& copy);
  167. GLine& operator =(const GLine& rhs);
  168. GPoint3 operator ()(const float t) const;
  169. GLine& SetPt(const GPoint3& _p);
  170. GLine& SetDir(const GVector3& _v);
  171. GVector3 GetPt() const;
  172. GVector3 GetDir() const;
  173. bool IsOnLine(const GPoint3& q);
  174. friend float dist(const GPoint3& q, const GLine& l);
  175. };