📜  C++中使用复数的几何套装2

📅  最后修改于: 2021-05-04 14:56:09             🧑  作者: Mango

在上一篇文章中,我们知道什么是复数,以及如何使用它们来模拟笛卡尔平面中的点。现在,我们将了解如何在C++中使用STL中的复杂类。

要使用STL中的复杂类,请使用#include

定义点类别

我们可以通过typedef complex point定义我们的点类。在程序开始时。该点的X和Y坐标分别是复数的实部和虚部。要访问我们的X和Y坐标,我们可以使用#define来对real()和imag()函数进行宏处理,如下所示:

# include 
typedef complex point;
# define x real()
# define y imag()

缺点:由于x和y已用作宏,因此它们不能用作变量。但是,此缺点并不代表其所具有的许多优点。

// CPP program to illustrate 
// the definition of point class
#include 
#include 
  
using namespace std;
  
typedef complex 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;
}

输出:

The X-coordinate of point P is: 2
The Y-coordinate of point P is: 3

关于平面中P个单点P的属性的实现:

  1. P的X坐标: Px
  2. P的Y坐标: Py
  3. P距原点(0,0)的距离: abs(P)
  4. OP与X轴的夹角,其中O为原点: arg(z)
  5. P绕原点旋转: P *极坐标(r,θ)
    // CPP program to illustrate
    // the implementation of single point attributes
    #include 
    #include 
      
    using namespace std;
      
    typedef complex 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 coordiantes 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 orgin is: " << abs(P) <

    输出:

    The X-coordinate of point P is: 4
    The Y-coordinate of point P is: 3
    The distance of point P from orgin is: 5
    The squared distance of point P from orgin is: 25
    The angle made by OP with the X-Axis is: 0.643501 radians
    The angle made by OP with the X-Axis is: 36.8699 degrees
    The point P on rotating 90 degrees anti-clockwise becomes: P_rotated(-3, 4)
    

    让我们考虑欧几里得平面上的点P(a,b)和Q(c,d)。
    关于P和Q的属性的实现。

  6. 向量加法: P + Q
  7. 矢量减法: P – Q
  8. 欧氏距离: abs(P – Q)
  9. PQ线的斜率: tan(arg(Q – P))
    point A = conj(P) * Q
  10. 点积:斧头
  11. 叉乘幅度: abs(Ay)
// CPP program to illustrate 
// the implementation of two point attributes
#include 
#include 
  
using namespace std;
  
typedef complex 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 coordiantes 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) <

输出:

Addition of P and Q is: P+Q(5, 7)
Subtraction of P and Q is: P-Q(-1, -1)
The distance between point P ans Q is: 1.41421
The squared distance between point P ans Q is: 2
The angle of elevation for line PQ is: 45 degrees
The slope of line PQ is: 1
The dot product P.Q is: 18
The magnitude of cross product PxQ is: 1