示例图
在阅读了上一篇文章之后,我们知道了复数到底是什么,以及如何使用它们来模拟笛卡尔平面中的点。现在,我们将了解如何在 C++ 中使用 STL 中的复杂类。
要使用 STL 中的复杂类,我们使用 #include <complex>
定义点类
我们可以在程序开始时通过 typedef complex<double> point; 来定义点类。点的 X 和 Y 坐标分别是复数的实部和虚部。要访问 X 和 Y 坐标,我们可以使用 #define 宏 real() 和 imag() 函数,如下所示:
# include <complex> typedef complex<double> point; # define x real() # define y imag()
缺点:由于 x 和 y 已被用作宏,因此它们不能用作变量。然而,这个缺点并不能掩盖它的诸多优点。
// CPP program to illustrate
// the definition of point class
#include <iostream>
#include <complex>
using namespace std;
typedef complex<double> point;
// X-coordinate is equivalent to the real part
// Y-coordinate is equivalent to the imaginary part
#define x real()
#define y imag()
int main()
{
point P(2.0, 3.0);
cout << "The X-coordinate of point P is: " << P.x << endl;
cout << "The Y-coordinate of point P is: " << P.y << endl;
return 0;
}
输出:
P点的X坐标为:2
P点的Y坐标为:3
关于平面上P点P的属性的实现:
1、P的X坐标: P.x
2、P的Y坐标: P.y
3、P 距离原点 (0, 0) 的距离: abs(P)
4、OP 与 X 轴形成的角度,其中 O 为原点: arg(z)
5、P 绕原点旋转: P * polar(r, ?)
// CPP program to illustrate
// the implementation of single point attributes
#include <iostream>
#include <complex>
using namespace std;
typedef complex<double> point;
#define x real()
#define y imag()
// The constant PI for providing angles in radians
#define PI 3.1415926535897932384626
// Function used to display X and Y coordinates of a point
void displayPoint(point P)
{
cout << "(" << P.x << ", " << P.y << ")" << endl;
}
int main()
{
point P(4.0, 3.0);
// X-Coordinate and Y-coordinate
cout << "The X-coordinate of point P is: " << P.x << endl;
cout << "The Y-coordinate of point P is: " << P.y << endl;
// Distances of P from origin
cout << "The distance of point P from origin is: " << abs(P) <<endl;
cout << "The squared distance of point P from origin is: " << norm(P) <<endl;
// Tangent Angle made by OP with the X-Axis
cout << "The angle made by OP with the X-Axis is: "
<< arg(P) << " radians" << endl;
cout << "The angle made by OP with the X-Axis is: "
<< arg(P)*(180/PI) << " degrees" << endl;
// Rotation of P about origin
// The angle of rotation = 90 degrees
point P_rotated = P * polar(1.0, PI/2);
cout<<"The point P on rotating 90 degrees anti-clockwise becomes: P_rotated";
displayPoint(P_rotated);
return 0;
}
输出:
P 点的 X 坐标为:4
P 点的 Y 坐标为:3
P 点与原点的距离为:5
P 点与原点的距离的平方为:25
OP 与 X 轴的夹角为:0.643501 弧度
OP 与 X 轴的夹角为:36.8699 度
点 P 逆时针旋转 90 度后变为:P_rotated(-3, 4)
矢量加法: P + Q
矢量减法: P – Q
欧几里得距离: abs(P – Q)
直线 PQ 的斜率: tan(arg(Q – P))
point A = conj(P) * Q
点积: A.x
叉积的数值: abs(A.y)
// CPP program to illustrate
// the implementation of two point attributes
#include <iostream>
#include <complex>
using namespace std;
typedef complex<double> point;
#define x real()
#define y imag()
// Constant PI for providing angles in radians
#define PI 3.1415926535897932384626
// Function used to display X and Y coordinates of a point
void displayPoint(point P)
{
cout << "(" << P.x << ", " << P.y << ")" << endl;
}
int main()
{
point P(2.0, 3.0);
point Q(3.0, 4.0);
// Addition and Subtraction
cout << "Addition of P and Q is: P+Q"; displayPoint(P+Q);
cout << "Subtraction of P and Q is: P-Q"; displayPoint(P-Q);
// Distances between points P and Q
cout << "The distance between point P ans Q is: " << abs(P-Q) <<endl;
cout << "The squared distance between point P ans Q is: " << norm(P-Q) <<endl;
// Slope of line PQ
cout << "The angle of elevation for line PQ is: "
<< arg(Q-P)*(180/PI) << " degrees" << endl;
cout << "The slope of line PQ is: " << tan(arg(Q-P)) <<endl;
// Construction of point A
point A = conj(P)*Q;
// Dot Product and Cross Product
cout << "The dot product P.Q is: " << A.x << endl;
cout << "The magnitude of cross product PxQ is: " << abs(A.y) << endl;
return 0;
}
输出:
P 和 Q 相加为:P+Q(5, 7)
P 和 Q 相减为:PQ(-1, -1)
点 P 和 Q 之间的距离为:1.41421
点 P 和 Q 之间的平方距离为:2
直线 PQ 的仰角为:45 度
直线 PQ 的斜率为:1
点积 PQ 为:18
叉积 PxQ 的幅度为:1