Program to check the similarity of given two triangles


In this problem, we will learn to check the similarity of two given triangles, which have many real-world use cases from the viewpoint of a programmer.

To construct and manage 2D and 3D models of things, CAD systems are utilized, and one key function is the capability to compare two triangles.

For instance, engineers working in design and construction may need to make that the foundational measurements of a building match the blueprint. Engineers can rapidly evaluate whether the angles and sides of the foundation game the layout by utilizing a CAD tool that has a built-in feature to check the resemblance of two triangles. This can aid in ensuring the building's structural stability and safety.

In addition, 3D models of objects are produced using CAD software by 3D printing technology. To make sure that the model is printed precisely and to the desired scale in this situation, similarity checking can be helpful. This is crucial for complicated models because manually verifying similarity can be tedious and error-prone.

The precision of robot motions can be ensured by programmers in the field of robotics by using similarity-checking tools. Checking the similarity of two triangles can be useful in ensuring that complex movements made by robot arms, which frequently have numerous joints, are precise and constant.

Explanation

Now let us understand some mathematics involved in calculating the similarity of triangles.

Two triangles are similar if they share the following characteristics −

  • The angles of both triangles are equal.

  • Corresponding sides of the triangle share the same ratio.

There are three ways to determine whether two triangles are similar: SSS, SAS, and AA. Let’s discuss each of the theorems in brief.

SSS (Side-Side-Side) Criteria

In two given triangles, if the three pairs of sides are in the same ratio then the two triangles are similar.

Let us consider the two triangles given above. The above two triangles can be similar by SSS criteria if the ratio of the three pairs of sides are equal, i.e. AC/PR = AB/PQ = CB/RQ

SAS (Side-Angle-Side) Criteria

In two given triangles, if the two pairs of sides are in the same ratio, and the angle between the two sides are same in both triangles, then the two triangles are similar.

If we take the above triangle as an example, then if AB/PQ = BC/QR and <B = <Q then both the triangles are similar by SAS criteria.

AA (Angle-Angle) Criteria

In two given triangles, if any two angles of the two triangles are equal, then the two triangles are similar.

If we take the above triangle as an example, then if <A =<P and <B=<Q or <A=<P and <C=<R or any such combinations exist then the two triangles are similar by AA criteria.

Normally, we are given the coordinates of the three points of the triangle and then we need to check similarity. In such cases, we will use this formula to calculate the distance.

Program to check the similarity of given two triangles when the coordinates are provided.

Approach

Let’s decode the entire program into step by step algorithm

  • Take the coordinates of the three points of both triangles as input.

  • Calculate the length in between the coordinates using the formula discussed above i.e. distance= Math. sqrt(Math. pow(y2-y1,2)+Math.pow(x2-x1,2))

  • After calculating the length of all the sides of both triangles, calculate the ratio of all the pairs.

  • Next, check if all three ratios are the same, if they are the same, then print the triangles are similar otherwise print the triangles are not similar.

Now, we will write the code implementing the above algorithm

Example

C++ Program to check the similarity of given two triangles when the coordinates are provided.

#include <iostream>
#include <cmath>
using namespace std;

int main() {
   double x1 = 0, y1 = 0, x2 = 3, y2 = 0, x3 = 0, y3 = 4; //coordinates of first triangle (x1, y1), (x2, y2), (x3, y3)
   double p1 = 0, q1 = 0, p2 = 6, q2 = 0, p3 = 0, q3 = 8; //coordinates of second triangle (p1, q1), (p2, q2), (p3, q3)
   
   // calculate the distance between the coordinates of the first triangle
   double dist1 = sqrt(pow((x2 - x1), 2) + pow((y2 - y1), 2));
   double dist2 = sqrt(pow((x3 - x2), 2) + pow((y3 - y2), 2));
   double dist3 = sqrt(pow((x1 - x3), 2) + pow((y1 - y3), 2));
   
   // calculate the distance between the coordinates of the second triangle
   double dist4 = sqrt(pow((p2 - p1), 2) + pow((q2 - q1), 2));
   double dist5 = sqrt(pow((p3 - p2), 2) + pow((q3 - q2), 2));
   double dist6 = sqrt(pow((p1 - p3), 2) + pow((q1 - q3), 2));
   
   // calculate the ratio of the length of the triangle
   double ratio1 = dist1/dist4;
   double ratio2 = dist2/dist5;
   double ratio3 = dist3/dist6;
   
   // check if the ratio of all three pairs of sides of the triangle are same, we are using SSS criteria
   if ((ratio1 == ratio2) && (ratio2 == ratio3)) {
      cout << "The two triangles are similar." << endl;
   } else {
      cout << "The two triangles are not similar." << endl;
   }
   
   return 0;
}

Output

The two triangles are similar.

Complexities

Time complexity: O(1), As this code performs a fixed number of calculations, regardless of the size of the input.

Space complexity: O(1), As the code uses a fixed number of variables to store input values and results, regardless of the size of the input.

Program to check the similarity of given two triangles when the coordinates are provided.

Approach

Let’s decode the entire program into step by step algorithm

  • Take the angles of the triangles as input.

  • Compare the angles to check if any two angles of the triangle are the same, here we are using AA criteria.

  • If any two angles are the same then print the triangles are similar else print the triangles are not similar.

Now, we will write the code implementing the above algorithm.

Example

C++ Program to check the similarity of given two triangles when the angles are provided.

#include <iostream>
using namespace std;

bool check_aa(int a1,int a2,int a3,int a4,int a5,int a6){
   if((a1==a4 || a1==a5 || a1==a6) && (a2==a4 || a2==a5 || a2==a6))
   return true;
   else
   return false;

}
int main(){
   
   // Input:  the angles of the triangles
   double a1 = 30, a2 = 60, a3 = 90; //angles of triangle A
   double a4 = 60, a5 = 90, a6 = 30; //angles of triangle B
   
   
   bool similar= check_aa(a1,a2,a3,a4,a5,a6);
   
   if (similar)
      cout << "The two triangles are similar." << endl;
   else
      cout << "The two triangles are not similar." << endl;
}

Output

The two triangles are similar.

Complexities

Time complexity: O(1), As this code performs a fixed number of calculations, regardless of the size of the input.

Space complexity: O(1), As the code uses a fixed number of variables to store input values and results, regardless of the size of the input.

Conclusion

In this article, we have tried to explain the approach to check the similarity of two triangles based on two situations, one where the sides are provided as input and another where the angles are provided as input. I hope this article helps you to learn the concept in a better way.

Updated on: 23-Mar-2023

327 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements