VOOZH about

URL: https://www.geeksforgeeks.org/cpp/geometry-using-complex-numbers-c-set-2/

⇱ Geometry using Complex Numbers in C++ | Set 2 - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Geometry using Complex Numbers in C++ | Set 2

Last Updated : 23 Jul, 2025

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.
 

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: 
 

  1. The X coordinate of P: P.x
  2. The Y coordinate of P: P.y
  3. The distance of P from origin (0, 0): abs(P)
  4. The angle made by OP from the X-Axis where O is the origin: arg(z)
  5. Rotation of P about origin: P * polar(r, ?) 
     

Output: 

The X-coordinate of point P is: 4
The Y-coordinate of point P is: 3
The distance of point P from origin is: 5
The squared distance of point P from origin 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)
  1. Vector Addition: P + Q
  2. Vector Subtraction: P – Q
  3. Euclidean Distance: abs(P – Q)
  4. Slope of line PQ: tan(arg(Q – P)) 
     
point A = conj(P) * Q
  1. Dot Product: A.x
  2. Magnitude of Cross Product: abs(A.y)

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


 

Comment
Article Tags: