# Geometry using Complex Numbers in C++ | Set 2

After going through previous post, we know what exactly are complex numbers and how we can use them to simulate points in a cartesian plane. Now, we will have an insight as to how to use the complex class from STL in C++.

**To use the complex class from STL we use #include <complex>**

**Defining Point Class**

We can define our point class by typedef complex<double> point; at the start of the program. The X and Y coordinates of the point are the real and imaginary part of the complex number respectively. To access our X- and Y-coordinates, we can macro the real() and imag() functions by using #define as follows:

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

**Drawback:** Since x and y have been used as macros, these can’t be used as variables. However, this drawback doesn’t stand in front of the many advantages this serves.

`// 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; ` `} ` |

*chevron_right*

*filter_none*

Output:

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

**Implementation of attributes with respect to P single point P in plane:**

**The X coordinate of P:**P.x**The Y coordinate of P:**P.y**The distance of P from origin (0, 0):**abs(P)**The angle made by OP from the X-Axis where O is the origin:**arg(z)**Rotation of P about origin:**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 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) <<endl;`

`cout <<`

`"The squared distance of point P from orgin 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;`

`}`

*chevron_right**filter_none*Output:

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)

**Vector Addition:**P + Q**Vector Subtraction:**P – Q**Euclidean Distance:**abs(P – Q)**Slope of line PQ:**tan(arg(Q – P))point A = conj(P) * Q

**Dot Product:**A.x**Magnitude of Cross Product:**abs(A.y)

Let us consider points P (a, b) and Q (c, d) on the Euclidean Plane.

**Implementation of attributes with respect to P and Q.**

`// 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 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) <<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; ` `} ` |

*chevron_right*

*filter_none*

Output:

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

This article is contributed by **Aanya Jindal**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Geometry using Complex Numbers <std::complex> in C++ | Set 1
- Complex numbers in C++ | Set 1
- Complex numbers in C++ | Set 2
- proj() function for Complex Numbers in C++
- Multiplication of two complex numbers given as strings
- pow() function for complex number in C++
- arg() function for Complex Number in C++
- cos() function for complex number in C++
- exp() function for complex number in C++
- tan() function for complex number in C++
- acos() function for complex number in C++
- atanh() function for complex number in C++
- asinh() function for complex number in C++
- asin() function for complex number in C++
- cosh() function for complex number in C++