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.

201 lines
5.8 KiB

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. private:
  20. float v[3];
  21. public:
  22. GVector3(float x = 0.0f, float y = 0.0f, float z = 0.0f);
  23. GVector3(const GVector3& copy);
  24. GVector3& operator =(const GVector3& rhs);
  25. //�����Ӽ�
  26. GVector3 operator +(const GVector3& rhs) const; // u + v
  27. GVector3 operator -(const GVector3& rhs) const; // u - v
  28. //�����ͳ����˳�
  29. friend GVector3 operator *(const GVector3& lhs, const float& k); // u * k
  30. friend GVector3 operator *(const float& k, const GVector3& rhs); // k * u
  31. friend GVector3 operator /(const GVector3& lhs, const float& k); // u / k
  32. //��������ģ�ͷ���
  33. friend float norm(const GVector3& v); //��������С��ģ������
  34. GVector3& normalize(); //���������� �����
  35. //���������ڻ�
  36. float operator *(const GVector3& rhs) const;
  37. //����ͶӰ
  38. friend GVector3 proj(const GVector3& p, const GVector3& q);
  39. friend GVector3 perp(const GVector3& p, const GVector3& q);
  40. //�������ڻ� ����
  41. GVector3 operator^(const GVector3& rhs) const;
  42. //����������1
  43. GVector3& Set(const float& x, const float& y, const float& z);
  44. friend float distance(const GVector3& v, const GVector3 u);
  45. //����������2
  46. GVector3& operator +=(const GVector3& rhs);
  47. GVector3& operator -=(const GVector3& rhs);
  48. friend ostream& operator<<(ostream& os, const GVector3& v);
  49. //����������3
  50. GVector3& operator *=(const float& k);
  51. GVector3& operator /=(const float& k);
  52. GVector3& operator ^=(const GVector3& rhs);
  53. //����������4
  54. bool operator ==(const GVector3 rhs) const;
  55. bool operator !=(const GVector3 rhs) const;
  56. //����������5
  57. GVector3 operator+() const;
  58. GVector3 operator-() const;
  59. float& operator [](const int& idx); //�±긳ֵ
  60. const float operator [](const int& idx) const;//�±���ȡ����
  61. };
  62. class GMatrix;
  63. class GVector {
  64. public:
  65. //GVector 1~2
  66. GVector(int dim = 3);
  67. GVector(int dim, double x, ...);
  68. GVector(const GVector3& copy);
  69. GVector(const GVector& copy);
  70. ~GVector();
  71. //GVector 3
  72. GVector& Set(double x, ...);
  73. GVector& Set(float *p);
  74. //GVector 4
  75. GVector& operator =(const GVector& rhs);
  76. GVector& operator +=(const GVector& rhs);
  77. GVector& operator -=(const GVector& rhs);
  78. GVector& operator +=(const float& k);
  79. GVector& operator -=(const float& k);
  80. GVector &operator *=(const float &k);
  81. GVector &operator /=(const float &k);
  82. bool operator ==(const GVector& rhs) const;
  83. bool operator !=(const GVector& rhs) const;
  84. GVector operator +() const;
  85. GVector operator -() const;
  86. GVector operator +(const GVector& rhs) const;
  87. GVector operator -(const GVector& rhs) const;
  88. float operator *(const GVector& rhs) const;
  89. GVector operator /(const float& k) const;
  90. float& operator [](const int& idx);
  91. const float& operator [](const int& idx) const;
  92. GVector& Nornalize();
  93. int GetDim() const;
  94. friend GVector operator *(const float& k, const GVector& rhs);
  95. friend GVector operator *(const GVector& lhs, const float& k);
  96. friend GVector operator *(const GMatrix& m, const GVector& v);
  97. friend GMatrix operator *(const GVector& v, const GMatrix& m);
  98. friend float norm(const GVector& v);
  99. friend float distance(const GVector& v, const GVector& u);
  100. friend ostream& operator <<(ostream& os, const GVector& v);
  101. friend class GMatrix;
  102. private:
  103. int n;
  104. float* v;
  105. };
  106. class GMatrix {
  107. public:
  108. GMatrix(int row = 4, int col = 4, float *elem = NULL);
  109. GMatrix(const GMatrix& copy);
  110. ~GMatrix();
  111. GMatrix& operator =(const GMatrix& rhs);
  112. GMatrix& operator +=(const GMatrix& rhs);
  113. GMatrix& operator -=(const GMatrix& rhs);
  114. GMatrix& operator *=(const float& k);
  115. GMatrix& operator *=(const GMatrix& rhs);
  116. GMatrix& operator /=(const float& k);
  117. bool operator ==(const GMatrix& rhs) const;
  118. bool operator !=(const GMatrix& rhs) const;
  119. float* operator [](const int idx);
  120. const float* operator [](const int idx) const;
  121. GMatrix operator +() const;
  122. GMatrix operator -() const;
  123. GMatrix operator +(const GMatrix& rhs) const;
  124. GMatrix operator -(const GMatrix& rhs) const;
  125. GMatrix operator *(const GMatrix& rhs) const;
  126. GMatrix operator /(const float& k) const;
  127. GMatrix& SetTranspose();
  128. GMatrix& SetIdentity();
  129. GMatrix& SetZeros();
  130. GMatrix& SetRowVec(const int idx, const GVector& v);
  131. GMatrix& SetColVec(const int idx, const GVector& v);
  132. GVector GetRowVec(const int idx) const;
  133. GVector GetColVec(const int idx) const;
  134. GMatrix& ExchangeRows(const int idx0, const int idx1);
  135. GMatrix& ExchangeCols(const int idx0, const int idx1);
  136. int GetRowNum() const;
  137. int GetColNum() const;
  138. bool IsSquare() const;
  139. friend GMatrix operator *(const GMatrix& lhs, const float& k);
  140. friend GMatrix operator *(const float& k , const GMatrix& rhs);
  141. friend GVector operator *(const GMatrix& m, const GVector& v);
  142. friend GMatrix operator *(const GVector& v, const GMatrix& m);
  143. friend ostream& operator <<(ostream& os, const GMatrix& m);
  144. friend GMatrix RowEchelonForm(const GMatrix& m);
  145. friend GMatrix ReduceRowEchelonForm(const GMatrix& m);
  146. friend float* from_arr(const GMatrix& m);
  147. friend int Rank(const GMatrix& m);//��ȡ��������
  148. friend int Nullity(const GMatrix& m);//��ȡ�������㻯��
  149. friend GMatrix Mij(const GMatrix& m, int r, int c); //��������ʽ
  150. friend float Det(const GMatrix& m);
  151. private:
  152. int r;
  153. int c;
  154. float *m;
  155. };