📜  程序检查给定两个三角形的相似性

📅  最后修改于: 2021-04-24 03:27:26             🧑  作者: Mango

给定四个由3个数字组成的数组,每个数组代表两个三角形的边和角。任务是检查两个三角形是否相似。如果相似,则打印它所依据的定理。

例子:

Input : side1 = [2, 3, 3] angle1 = [80, 60, 40]
        side2 = [4, 6, 6]  angle2 = [40, 60, 80]
Output: Triangles are similar by SSS AAA SAS 

Input : side1 = [2, 3, 4] angle1 = [85, 45, 50]
        side2 = [4, 6, 6]  angle2 = [40, 60, 80]
Output: Triangles are not similar

相似的三角形是两个或多个三角形,它们的所有对应角度均相等,并且所有对应边均成比例。三角形面向的方向无关紧要。只要双方成比例,它们的大小就无关紧要。三角形的相似性可以通过以下定理证明:

  1. 侧面(SSS)相似性标准:
    如果一个三角形的所有边都与另一个三角形的相应边成比例,则这些三角形的“侧面” ( Side-Side-Side) (SSS)属性被认为是相似的。
    在三角形ABC和PQR中,AB / PQ = BC / QR = CA / RP三角形相似。
  2. 侧面角度(SAS)相似性标准:
    如果两个三角形的两个边成比例,并且两个三角形之间的角度相同,则根据“边角-侧面” (SAS)的属性,这些三角形被称为相似。
    在三角形ABC和PQR中,如果AB / PQ = BC / QR且\measuredangle ABC = \measuredangle PQR三角形是相似的。
  3. 角-角-角(AAA)相似性标准:
    如果一个三角形的所有角度都等于另一个三角形的相应角度,则根据Angle-Angle-Angle (AAA)的属性,这些三角形被称为是相似的。
    在三角形ABC和PQR中,如果\measuredangle ABC = \measuredangle PQR\measuredangle BCA = \measuredangle QRP\measuredangle CAB = \measuredangle RPQ那么三角形是相似的。

下面是上述方法的实现:

C++
// C++ program to check 
// similarity between 
// two triangles.
#include
using namespace std;
  
//Function for AAA similarity
int simi_aaa(int a1[], int a2[])
{
    sort(a1, a1 + 3);
    sort(a2, a2 + 3);
      
    // Check for AAA
    if (a1[0] == a2[0] && 
        a1[1] == a2[1] && 
        a1[2] == a2[2])
    return 1;
    else
    return 0;
      
}
  
// Function for
// SAS similarity
int simi_sas(int s1[], int s2[], 
             int a1[], int a2[])
{
    sort(a1, a1 + 3);
    sort(a2, a2 + 3);
    sort(s1, s1 + 3);
    sort(s2, s2 + 3);
      
    // Check for SAS
      
    // angle b / w two smallest
    // sides is largest.
    if( s1[0] / s2[0] == s1[1] / 
                         s2[1])
    {
        // since we take angle
        // b / w the sides.
        if (a1[2] == a2[2])         
            return 1;
    }
    if (s1[1] / s2[1] == s1[2] /
                         s2[2])
    {
        if (a1[0] == a2[0])
            return 1;
        } 
    if (s1[2] / s2[2] == s1[0] /    
                         s2[0])
    {
        if(a1[1] == a2[1])
            return 1;
        }
    return 0;
}
  
// Function for SSS similarity
int simi_sss(int s1[], int s2[])
{
    sort(s1, s1 + 3);
    sort(s2, s2 + 3);
      
    // Check for SSS
    if(s1[0] / s2[0] == s1[1] / s2[1] && 
       s1[1] / s2[1] == s1[2] / s2[2] && 
       s1[2] / s2[2] == s1[0] / s2[0])
        return 1;
      
    return 0;
}
  
// Driver Code
int main()
{
    int s1[] = {2, 3, 3};
    int s2[] = {4, 6, 6};
          
    int a1[] = {80, 60, 40};
    int a2[] = {40, 60, 80};
  
    // function call for
    // AAA similarity
    int aaa = simi_aaa(a1, a2); 
  
    // function call for
    // SSS similarity 
    int sss = simi_sss(s1, s2) ;
  
    // function call for
    // SAS similarity 
    int sas = simi_sas(s1, s2, 
                       a1, a2) ;
  
    // Check if triangles
    // are similar or not 
    if(aaa == 1 || 
       sss == 1 || sas == 1)
    {
        cout << "Triangles are " 
             << "similar by ";
        if(aaa == 1) cout << "AAA ";
        if(sss == 1) cout << "SSS ";
        if(sas == 1) cout << "SAS.";
    }
      
    else
        cout << "Triangles are " 
             << "not similar";
    return 0;
}
  
// This code is contributed
// by Arnab Kundu


Java
// Java program to check 
// similarity between 
// two triangles.
import java.util.*;
class GFG1
{
      
// Function for
// AAA similarity
static int simi_aaa(int a1[], 
                    int a2[])
{
    Arrays.sort(a1);
    Arrays.sort(a2);
      
    // Check for AAA
    if (a1[0] == a2[0] && 
        a1[1] == a2[1] &&
        a1[2] == a2[2])
    return 1;
    else
    return 0;
      
}
  
// Function for
// SAS similarity
static int simi_sas(int s1[], int s2[], 
                    int a1[], int a2[])
{
    Arrays.sort(a1);
    Arrays.sort(a2);
    Arrays.sort(s1);
    Arrays.sort(s2);
      
    // Check for SAS
      
    // angle b / w two smallest
    // sides is largest.
    if(s1[0] / s2[0] == s1[1] / s2[1])
    {
        // since we take angle
        // b / w the sides.
        if (a1[2] == a2[2])         
            return 1;
    }
    if (s1[1] / s2[1] == s1[2] / s2[2])
    {
        if (a1[0] == a2[0])
            return 1;
    } 
    if (s1[2] / s2[2] == s1[0] / s2[0])
    {
        if(a1[1] == a2[1])
            return 1;
    }
    return 0;
}
  
// Function for 
// SSS similarity
static int simi_sss(int s1[],
                    int s2[])
{
    Arrays.sort(s1);
    Arrays.sort(s2);
      
    // Check for SSS
    if(s1[0] / s2[0] == s1[1] / s2[1] && 
       s1[1] / s2[1] == s1[2] / s2[2] && 
       s1[2] / s2[2] == s1[0] / s2[0])
        return 1;
      
    return 0;
}
  
// Driver Code
public static void main(String args[])
{
    int s1[] = {2, 3, 3};
    int s2[] = {4, 6, 6};
          
    int a1[] = {80, 60, 40};
    int a2[] = {40, 60, 80};
  
    // function call for
    // AAA similarity
    int aaa = simi_aaa(a1, a2); 
  
    // function call for
    // SSS similarity 
    int sss = simi_sss(s1, s2) ;
  
    // function call for 
    // SAS similarity 
    int sas = simi_sas(s1, s2, 
                       a1, a2) ;
  
    // Check if triangles 
    // are similar or not 
    if(aaa == 1 || 
       sss == 1 || sas == 1)
    {
        System.out.print("Triangles are " + 
                         "similar by ");
        if(aaa == 1) System.out.print("AAA ");
        if(sss == 1) System.out.print("SSS ");
        if(sas == 1) System.out.print("SAS.");
    }
    else
        System.out.println("Triangles are " + 
                           "not similar");
}
}
  
// This code is contributed 
// by Arnab Kundu


Python
# Python program to check 
# similarity between two triangles.
  
# Function for AAA similarity
def simi_aaa(a1, a2):             
    a1 = [float(i) for i in a1]
    a2 = [float(i) for i in a2]
    a1.sort()
    a2.sort()
      
    # Check for AAA
    if a1[0] == a2[0] and a1[1] == a2[1] and a1[2] == a2[2]:
        return 1
    return 0
  
# Function for SAS similarity
def simi_sas(s1, s2, a1, a2): 
      
    s1 = [float(i) for i in s1]
    s2 = [float(i) for i in s2]
    a1 = [float(i) for i in a1]
    a2 = [float(i) for i in a2]
      
    s1.sort()
    s2.sort()
    a1.sort()
    a2.sort()
      
    # Check for SAS
      
    # angle b / w two smallest sides is largest.
    if s1[0] / s2[0] == s1[1] / s2[1]:
          
        # since we take angle b / w the sides.
        if a1[2] == a2[2]:         
            return 1
              
    if s1[1] / s2[1] == s1[2] / s2[2]:
        if a1[0] == a2[0]:
            return 1
              
    if s1[2] / s2[2] == s1[0] / s2[0]:
        if a1[1] == a2[1]:
            return 1
      
    return 0
  
# Function for SSS similarity
def simi_sss(s1, s2): 
      
    s1 = [float(i) for i in s1]
    s2 = [float(i) for i in s2] 
    s1.sort()
    s2.sort() 
      
    # Check for SSS
    if(s1[0] / s2[0] == s1[1] / s2[1] 
        and s1[1] / s2[1] == s1[2] / s2[2] 
        and s1[2] / s2[2] == s1[0] / s2[0]):
        return 1
      
    return 0
      
  
# Driver Code 
s1 = [2, 3, 3]
s2 = [4, 6, 6]
          
a1 = [80, 60, 40]
a2 = [40, 60, 80]
  
# function call for AAA similarity
aaa = simi_aaa(a1, a2) 
  
# function call for SSS similarity 
sss = simi_sss(s1, s2) 
  
# function call for SAS similarity 
sas = simi_sas(s1, s2, a1, a2) 
  
# Check if triangles are similar or not 
if aaa or sss or sas: 
    print "Triangles are similar by",
    if aaa: print "AAA", 
    if sss: print "SSS", 
    if sas: print "SAS"
else: print "Triangles are not similar"


C#
// C# program to check 
// similarity between 
// two triangles. 
  
using System;
  
class GFG1 
{ 
      
// Function for 
// AAA similarity 
static int simi_aaa(int [] a1, 
                    int [] a2) 
{ 
    Array.Sort(a1); 
    Array.Sort(a2); 
      
    // Check for AAA 
    if (a1[0] == a2[0] && 
        a1[1] == a2[1] && 
        a1[2] == a2[2]) 
    return 1; 
    else
    return 0; 
      
} 
  
// Function for 
// SAS similarity 
static int simi_sas(int [] s1, int [] s2, 
                    int [] a1, int [] a2) 
{ 
    Array.Sort(a1); 
    Array.Sort(a2); 
    Array.Sort(s1); 
    Array.Sort(s2); 
      
    // Check for SAS 
      
    // angle b / w two smallest 
    // sides is largest. 
    if(s1[0] / s2[0] == s1[1] / s2[1]) 
    { 
        // since we take angle 
        // b / w the sides. 
        if (a1[2] == a2[2])         
            return 1; 
    } 
    if (s1[1] / s2[1] == s1[2] / s2[2]) 
    { 
        if (a1[0] == a2[0]) 
            return 1; 
    } 
    if (s1[2] / s2[2] == s1[0] / s2[0]) 
    { 
        if(a1[1] == a2[1]) 
            return 1; 
    } 
    return 0; 
} 
  
// Function for 
// SSS similarity 
static int simi_sss(int [] s1, 
                    int [] s2) 
{ 
    Array.Sort(s1); 
    Array.Sort(s2); 
      
    // Check for SSS 
    if(s1[0] / s2[0] == s1[1] / s2[1] && 
    s1[1] / s2[1] == s1[2] / s2[2] && 
    s1[2] / s2[2] == s1[0] / s2[0]) 
        return 1; 
      
    return 0; 
} 
  
// Driver Code 
public static void Main() 
{ 
    int [] s1 = {2, 3, 3}; 
    int [] s2 = {4, 6, 6}; 
          
    int [] a1 = {80, 60, 40}; 
    int [] a2 = {40, 60, 80}; 
  
    // function call for 
    // AAA similarity 
    int aaa = simi_aaa(a1, a2); 
  
    // function call for 
    // SSS similarity 
    int sss = simi_sss(s1, s2) ; 
  
    // function call for 
    // SAS similarity 
    int sas = simi_sas(s1, s2, 
                    a1, a2) ; 
  
    // Check if triangles 
    // are similar or not 
    if(aaa == 1 || 
    sss == 1 || sas == 1) 
    { 
        Console.Write("Triangles are " + 
                        "similar by "); 
        if(aaa == 1) Console.Write("AAA "); 
        if(sss == 1) Console.Write("SSS "); 
        if(sas == 1) Console.Write("SAS."); 
    } 
    else
        Console.WriteLine("Triangles are " + 
                        "not similar"); 
} 
} 
  
// This code is contributed 
// by Ryuga


PHP


输出:
Triangles are similar by AAA SSS SAS