Skip to content
Related Articles

Related Articles

Program to check congruency of two triangles

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 16 Jun, 2022

Given four arrays of 3 numbers each which represents sides and angles of two triangles. The task is to check if the two triangles are Congruent or not. Also print the theorem by which they are congruent. Note: All sides and angles given as input are for valid triangles. Examples:

Input : side1 = [3, 4, 5]  angle1 = [90, 60, 30]
        side2 = [4, 3, 5]  angle2 = [60, 30, 90]
Output: Triangles are congruent by SSS SAS ASA AAS HL.  

Input : side1 = [3, 5, 6]  angle1 = [80, 50, 50]
        side2 = [1, 1, 1]  angle2 = [60, 60, 60]
Output: Triangles are not congruent

Congruent triangles are two or more triangles that have all corresponding sides that are equal or a pair of sides and between angle are equal or a pair of angle and side between are equal or a pair of angle and other side are equal or hypotenuse and one side are equal. The congruency of triangles can be proved by the following theorems:

  1. Side-Side-Side (SSS) Congruency criteria: If all the sides of a triangle are equal to the sides of another triangle then the triangles are said to be congruent by the property of Side-Side-Side (SSS). In above triangle ABC and A’B’C’ if, AB=A’B’ and BC=B’C’ and CA=C’A’ then, triangles are congruent.
  2. Side-Angle-Side (SAS) Congruent criteria: If two sides of the two triangles are equal and the angle between them is same in both triangle then the triangles are said to be congruent by the property of Side-Angle-Side (SAS). In above triangle ABC and A’B’C’ if, AB=A’B’ and BC=B’C’ and \measuredangle ABC    \measuredangle A'B'C'    triangles are congruent.
  3. Angle-Side-Angle (ASA) Congruent criteria :If two angles of the two triangles are equal and the length of side between them is same in both triangle then the triangles are said to be congruent by the property of Angle-Side-Angle (ASA).In above triangle ABC and A’B’C’ if, \measuredangle ABC    \measuredangle A'B'C'    and \measuredangle BCA    \measuredangle B'C'A'    and BC=B’C’ then, triangles are congruent.
  4. Angle-Angle-Side (AAS) Congruent criteria :If two angles of the two triangles are equal and the length of other side is same in both triangle then the triangles are said to be congruent by the property of Angle-Angle-Side (AAS). In above triangle ABC and A’B’C’ if, \measuredangle ABC    \measuredangle A'B'C'    and \measuredangle BCA    \measuredangle B'C'A'    and CA=C’A’ then, triangles are congruent.
  5. Hypotenuse-Leg (HL) Congruent criteria : If the hypotenuse of the two triangles are equal and the length of any other one side is same in both triangle then the triangles are said to be congruent by the property of Hypotenuse-Leg (HL).

Below is the implementation of the above theorems. 

Python




# Python program to check
# similarity between two triangles.
  
# Function for SAS congruency
def cong_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] and s1[1] == s2[1]:
          
        # since we take angle b / w the sides.
        if a1[2] == a2[2]:        
            return 1
              
    if s1[1] == s2[1] and s1[2] == s2[2]:
        if a1[0] == a2[0]:
            return 1
              
    if s1[2] == s2[2] and s1[0] == s2[0]:
        if a1[1] == a2[1]:
            return 1
      
    return 0
     
# Function for ASA congruency
def cong_asa(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 ASA
      
    # side b / w two smallest angle is largest.
    if a1[0] == a2[0] and a1[1] == a2[1]:
          
        # since we take side b / w the angle.
        if s1[2] == s2[2]:        
            return 1
              
    if a1[1] == a2[1] and a1[2] == a2[2]:
        if s1[0] == s2[0]:
            return 1
              
    if a1[2] == a2[2] and a1[0] == a2[0]:
        if s1[1] == s2[1]:
            return 1
      
    return 0
     
# Function for AAS congruency
def cong_aas(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 AAS
      
    # side other two smallest angle is smallest or 2nd smallest.
    if a1[0] == a2[0] and a1[1] == a2[1]:
          
        # since we take side other than angles.
        if s1[0] == s2[0] or s1[1] == s2[1]:        
            return 1
              
    if a1[1] == a2[1] and a1[2] == a2[2]:
        if s1[1] == s2[1] or s1[2] == s2[2]:
            return 1
              
    if a1[2] == a2[2] and a1[0] == a2[0]:
        if s1[0] == s2[0] or s1[2] == s2[2]:
            return 1
      
    return 0
  
# Function for HL congruency
def cong_hl(s1, s2):
      
    s1 = [float(i) for i in s1]
    s2 = [float(i) for i in s2]
    s1.sort()
    s2.sort()
      
    # Check for HL
    if s1[2] == s2[2]:
        if s1[1] == s2[1] or s1[0] == s2[0]:
            return 1
      
    return 0
     
# Function for SSS congruency
def cong_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] and s1[1] == s2[1] and s1[2] == s2[2]):
        return 1
      
    return 0
      
  
# Driver Code
s1 = [3, 4, 5]
s2 = [4, 3, 5]
          
a1 = [90, 60, 30]
a2 = [60, 30, 90]
  
# function call for SSS congruency
sss = cong_sss(s1, s2)
  
# function call for SAS congruency
sas = cong_sas(s1, s2, a1, a2)
 
# function call for ASA congruency
asa = cong_asa(s1, s2, a1, a2)
  
# function call for AAS congruency
aas = cong_aas(s1, s2, a1, a2)
 
# function call for HL congruency
hl = cong_hl(s1, s2, )
  
# Check if triangles are congruent or not
if sss or sas or asa or aas or hl :
    print "Triangles are congruent by",
    if sss: print "SSS",
    if sas: print "SAS",
    if asa: print "ASA",
    if aas: print "AAS",
    if hl: print "HL",
else: print "Triangles are not congruent"

Javascript




<script>
// JavaScript program to check
// similarity between two triangles.
 
// Function for SAS congruency
function cong_sas(s1, s2, a1, 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 ASA congruency
function cong_asa(s1, s2, a1, a2){
 
    s1.sort();
    s2.sort();
    a1.sort();
    a2.sort();
     
    // Check for ASA
     
    // side b / w two smallest angle is largest.
    if (a1[0] == a2[0] && a1[1] == a2[1]){
        // since we take side b / w the angle.
        if (s1[2] == s2[2]){
            return 1;
        }           
    }
                     
    if( a1[1] == a2[1] && a1[2] == a2[2]){
         if (s1[0] == s2[0]){
            return 1;
        }      
    }       
             
    if( a1[2] == a2[2] && a1[0] == a2[0]){
          if (s1[1] == s2[1]){
            return 1;
        }     
    }
     
    return 0;
}
 
     
// Function for AAS congruency
function cong_aas(s1, s2, a1, a2){
      
    s1.sort();
    s2.sort();
    a1.sort();
    a2.sort();
     
    // Check for AAS
     
    // side other two smallest angle is smallest or 2nd smallest.
    if (a1[0] == a2[0] && a1[1] == a2[1]){
        // # since we take side other than angles.
        if (s1[0] == s2[0] || s1[1] == s2[1]){
            return 1;
        }           
    }
                 
    if (a1[1] == a2[1] && a1[2] == a2[2]){
        if (s1[1] == s2[1] || s1[2] == s2[2]){
            return 1;
        }       
    }
 
             
             
    if (a1[2] == a2[2] && a1[0] == a2[0]){
        if( s1[0] == s2[0] || s1[2] == s2[2]){
            return 1;
        }       
    }
     
    return 0;
}
 
 
// Function for HL congruency
function cong_hl(s1, s2){
    s1.sort();
    s2.sort();
     
    // Check for HL
    if (s1[2] == s2[2]){
         if( s1[1] == s2[1] || s1[0] == s2[0]){
            return 1;
        }      
    }
     
    return 0; 
}
 
     
// Function for SSS congruency
function cong_sss(s1, s2){
    s1.sort();
    s2.sort();
     
    // # Check for SSS
    if(s1[0] == s2[0] && s1[1] == s2[1] && s1[2] == s2[2]){
        return 1;
    }
         
     
    return 0;  
}
 
     
 
// Driver Code
s1 = [3, 4, 5];
s2 = [4, 3, 5];
         
a1 = [90, 60, 30];
a2 = [60, 30, 90];
 
// function call for SSS congruency
sss = cong_sss(s1, s2);
 
// function call for SAS congruency
sas = cong_sas(s1, s2, a1, a2);
 
// function call for ASA congruency
asa = cong_asa(s1, s2, a1, a2);
 
// function call for AAS congruency
aas = cong_aas(s1, s2, a1, a2);
 
// function call for HL congruency
hl = cong_hl(s1, s2, );
 
// Check if triangles are congruent or not
if (sss || sas || asa || aas || hl){
    document.write("Triangles are congruent by ");
    if(sss) document.write("SSS ");
    if(sas) document.write("SAS ");
    if (asa) document.write("ASA ");
    if (aas) document.write("AAS ");
    if (hl) document.write("HL ");
}
 
else document.write("Triangles are not congruent");
 
// The code is contributed by Nidhi goel
</script>

Output:

Triangles are congruent by SSS SAS ASA AAS HL

Time Complexity: O(1) As the arrays have only 3 elements, so the total time taken can be treated as constant.
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!