Home >Backend Development >C++ >Program to check the similarity of two given triangles

Program to check the similarity of two given triangles

WBOY
WBOYforward
2023-08-25 17:33:03857browse

In this question, we will learn to check the similarity of two given triangles, which has many real-world use cases from a programmer's perspective.

In order to build and manage 2D and 3D models of things, you need to use CAD systems, one of the key features is the ability to compare two triangles.

For example, engineers working on design and construction may need to match a building's foundation measurements to blueprints. Engineers can quickly assess whether the angles and sides of a foundation fit the layout using CAD tools, which have built-in functionality to check the similarity of two triangles. This helps ensure the structural stability and safety of the building.

In addition, the 3D model of the object is produced using CAD software through 3D printing technology. To ensure that the model is printed accurately and to the desired proportions in this case, a similarity check can be helpful. This is critical for complex models, as manually validating similarities can be tedious and error-prone.

Programmers in the field of robotics can ensure the accuracy of robot motion by using similarity checking tools. Checking the similarity of two triangles helps ensure that complex movements made by robotic arms (often with multiple joints) are precise and constant.

illustrate

Now let's understand some of the math involved in calculating triangle similarity.

Two triangles are similar if they have the following characteristics -

  • The interior angles of two triangles are equal.

  • Corresponding sides of a triangle have the same proportions.

There are three methods to determine whether two triangles are similar: SSS, SAS, and AA. Let us briefly discuss each theorem.

SSS (Side-Side-Side) Standard

In two given triangles, two triangles are similar if the proportions of the three pairs of sides are the same.

Program to check the similarity of two given triangles

Let us consider the two triangles given above. If the proportions of the three opposite sides are equal, the above two triangles can be similar according to the SSS standard, that is, AC/PR = AB/PQ = CB/RQ

SAS (Side-Angle-Side) Standard

In two given triangles, two triangles are similar if the proportions of the two pairs of sides are the same and the angle between the two sides in the two triangles is the same.

Program to check the similarity of two given triangles

Taking the above triangle as an example, if AB/PQ = BC/QR and

AA (angle-angle) standard

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

Program to check the similarity of two given triangles

If we take the above triangle as an example, then these two triangles are similar if

Normally, we will get the coordinates of the three points of the triangle, and then we need to check the similarity. In this case, we will use this formula to calculate the distance.

Program to check the similarity of two given triangles

A program that checks the similarity of two triangles given when coordinates are provided.

method

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

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

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

  • After calculating the lengths of all sides of two triangles, calculate the ratio of all pairs.

  • Next, check whether the three proportions are the same. If they are the same, print the triangles to be similar. Otherwise, print the triangles to be dissimilar.

Now, we will write the code to implement the above algorithm

Example

C Program to check the similarity of given two triangles when 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.

Complexity

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

Space Complexity: O(1) because the code uses a fixed number of variables to store input values ​​and results regardless of the size of the input.

A program that checks the similarity of two triangles given when coordinates are provided.

method

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

  • Take the angle of the triangle as input.

  • Compare angles and check whether any two angles of a triangle are the same. Here we use the AA criterion.

  • If any two angles are the same, the printed triangles are similar, otherwise the printed triangles are not similar.

Now, we will write the code that implements the above algorithm.

Example

C Program to check the similarity of given two triangles when 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;
}

输出

The two triangles are similar.

复杂性

时间复杂度:O(1),因为无论输入大小如何,此代码都会执行固定数量的计算。

空间复杂度:O(1),因为代码使用固定数量的变量来存储输入值和结果,而不管输入的大小。

结论

在本文中,我们尝试基于两种情况解释检查两个三角形相似性的方法,一种是提供边作为输入,另一种是提供角度作为输入。我希望这篇文章可以帮助您更好地学习这个概念。

The above is the detailed content of Program to check the similarity of two given triangles. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete