Open In App

Geometry using Complex Numbers in C++ | Set 2

Last Updated : 18 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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




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


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, ?) 
     

CPP




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


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)

CPP




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


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

 



Similar Reads

Geometry using Complex Numbers &lt;std::complex&gt; in C++ | Set 1
While solving geometric problems, it is time consuming to define the point class for specifying a point on the 2D Plane or the Euclidean Plane. So, this article specifies a faster and clever way to implement the same using complex class from STL in C++. Before implementation, it is essential to understand what are complex numbers and how they can h
5 min read
Computational Geometry - Algorithms for Geometry
Computational geometry is a field of study that focuses on developing algorithms and data structures for solving problems that involve geometric shapes and structures. The field has applications in a variety of areas, including computer graphics, robotics, geographic information systems, and more. In this article, we will explore some of the key co
22 min read
Complex numbers in C++ | Set 1
The complex library implements the complex class to contain complex numbers in cartesian form and several functions and overloads to operate with them. real() - It returns the real part of the complex number.imag() - It returns the imaginary part of the complex number. C/C++ Code // Program illustrating the use of real() and // imag() function #inc
5 min read
Complex numbers in C++ | Set 2
We introduced and discussed the concept in Complex numbers in C++ | Set 1The remaining functions with example are discussed here: log() - It is used to return the log of the complex number. C/C++ Code // CPP program to illustrate the use of log() #include &lt;iostream&gt; // for std::complex, std::log #include &lt;complex&gt; using namespace std; /
3 min read
Product of Complex Numbers using three Multiplication Operation
Given four integers a, b, c, and d which represents two complex numbers of the form (a + bi) and (c + di), the task is to find the product of the given complex numbers using only three multiplication operations.Examples: Input: a = 2, b = 3, c = 4 and d = 5 Output: -7 + 22i Explanation: Product is given by: (2 + 3i)*(4 + 5i) = 2*4 + 4*3i + 2*5i + 3
6 min read
proj() function for Complex Numbers in C++
The proj() function is a built-in function and is defined in the complex header file. This function is used to find the projection of complex number [Tex]z [/Tex]onto the Riemann sphere. Syntax: template &lt;class T&gt; complex&lt;T&gt; proj (const complex&lt;T&gt;&amp; z); Parameter: z: This method takes a mandatory parameter z which represents th
2 min read
Program to Add Two Complex Numbers
Given two complex numbers of the form and the task is to add these two complex numbers. [Tex]a_{1} + ib_{1} [/Tex]and [Tex]a_{2} + ib_{2} [/Tex] Here the values of real and imaginary numbers are passed while calling the parameterized constructor and, with the help of a default(empty) constructor, the function addComp is called to get the addition o
7 min read
Modular Exponentiation of Complex Numbers
Given four integers A, B, K, M. The task is to find (A + iB)K % M which is a complex number too. A + iB represents a complex number. Examples: Input : A = 2, B = 3, K = 4, M = 5 Output: 1 + i*0 Input : A = 7, B = 3, K = 10, M = 97 Output: 25 + i*29 Prerequisite: Modular Exponentiation Approach: An efficient approach is similar to the modular expone
7 min read
Convert given Complex Numbers into polar form and perform all arithmetic operations
Given two Complex Numbers Z1 and Z2 in the Cartesian form, the task is to convert the given complex number into polar form and perform all the arithmetic operations ( addition, subtraction, multiplication, and division ) on them. Examples: Input: Z1 = (2, 3), Z2 = (4, 6)Output:Polar form of the first Complex Number: (3.605551275463989, 0.9827937232
15 min read
Square root of two Complex Numbers
Given two positive integers A and B representing the complex number Z in the form of Z = A + i * B, the task is to find the square root of the given complex number. Examples: Input: A = 0, B =1Output:The Square roots are: 0.707107 + 0.707107*i-0.707107 - 0.707107*i Input: A = 4, B = 0Output:The Square roots are: 2-2 Approach: The given problem can
9 min read
Article Tags :
Practice Tags :