Click here to Skip to main content
15,844,477 members
Articles / General Programming / Algorithms

Algorithm for Triangles Classification

Rate me:
Please Sign up or sign in to vote.
4.80/5 (2 votes)
27 Oct 2018CPOL1 min read 18K   66   6   5
Simple algorithm to determine the type of a triangle, being informed its sides


Triangles are classified depending on relative sizes of their elements.

As regards their sides, triangles may be:

  • Scalene (all sides are different)
  • Isosceles (two sides are equal)
  • Equilateral (all three sides are equal)

And as regards their angles, triangles may be:

  • Acute (all angles are acute)
  • Right (one angle is right)
  • Obtuse (one angle is obtuse)


  1. One of the properties of the triangles is that the sum of the lengths of any two sides  is greater than the length of the third side.
  2. There is a special type of triangle, called a degenerate triangle. formed by three collinear points. It doesn’t look like a triangle, it looks like a line segment. In this case, one of its sides is equal to the sum of the other

In this tip, we will develop an algorithm that determines the type of a triangle, given its sides.

Using the Code

The algorithm below was implemented in the Lua language using the ZeroBrane Studio IDE:

Determines the type of the triangle by the sides and by the angles 

Language: Lua

2018, Jose Cintra

-- Compare two floats
local function almostEquals(a, b)  
  threshold = 0.00001
  diff = math.abs(a - b) -- Absolute value of difference
  return (diff < threshold) 

-- Determines the type of the triangle by the sides and by the angles 
local function triangleType(a,b,c) 
  local bySide,byAngle = nil

  if (a <= (b + c) and b <= (a + c) and c <= (a + b)) then

    --Type of the triangle by sides
    if ( a == b and b == c ) then
      bySide = 1 -- Equilateral
    elseif (a == b or b == c or a == c) then
      bySide = 2 -- Isosceles
      bySide = 3 -- Scalene

    --Type of the triangle by Angle
    if almostEquals(a,(b + c)) or almostEquals(b,(a + c)) or almostEquals(c,(a + b))  then
      byAngle = 4 -- Degenerate
    elseif almostEquals(a^2,(b^2 + c^2)) or almostEquals(b^2,(a^2 + c^2)) or almostEquals(c^2,(a^2 + b^2))  then
      byAngle = 1 -- Right
    elseif (a^2 > b^2 + c^2) or (b^2 > a^2 + c^2) or (c^2 > a^2 + b^2)  then
      byAngle = 2 -- Obtuse
      byAngle = 3 -- Acute


  return bySide,byAngle


-- Main routine

local bySideTypes = {"Equilateral","Isosceles","Scalene"}
local byAngleTypes = {"Right","Obtuse","Acute","Degenerate"}

print("Triangle Type\n")
print("Enter the value of A side: ")
local a = tonumber(
print("Enter the value of B side: ")
local b = tonumber(
print("Enter the value of C side: ")
local c = tonumber(

local bySide,byAngle = triangleType(a,b,c)

if (bySide ~= nil) then
  print ("The type of the triangle is " .. bySideTypes[bySide] .. "/" .. byAngleTypes[byAngle])
  print ("These sides do not form a triangle")
-- end  

Points of Interest

The following points should be taken into account for the understanding of the algorithm:

  1. To obtain the type of the triangle according to its angles, we use the Pythagorean theorem. Like this.
  2. In the Lua language, a function can return more than one value.
  3. For comparison of float values, we use the almostEquals function, which can be adjusted according to your need, in the threshold variable.
  4. For the sake of "good practices", we have resolved to encode the types of triangles in an array.


21 Oct 2018 - Initial version
27 Oct 2018 - Added "degenerate triangles" and almostEquals function



Last Words

Thanks for reading!
The source code for this algorithm is also available on GitHub at: MathAlgorithms

This article was originally posted at


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer
Brazil Brazil
I am a software developer focused on Mathematics, IoT and Games.
Homepage: HTML Apps

Comments and Discussions

GeneralMy vote of 4 Pin
Doom For Ever27-Oct-18 1:48
professionalDoom For Ever27-Oct-18 1:48 
QuestionSimplification ? Pin
Harrison Pratt23-Oct-18 4:02
professionalHarrison Pratt23-Oct-18 4:02 
AnswerRe: Simplification ? Pin
José Cintra23-Oct-18 6:54
José Cintra23-Oct-18 6:54 
QuestionNice and well... Pin
CodeWraith22-Oct-18 4:21
CodeWraith22-Oct-18 4:21 
AnswerRe: Nice and well... Pin
José Cintra22-Oct-18 16:00
José Cintra22-Oct-18 16:00 
@CodeWraith, Thanks for your comments.

After thinking a lot about your observation, I came up with two possible solutions:

1) As for the sides, a degenerated triangle may be isosceles and scalene, but never equilateral.
As for the angles, there is a lot of controversy, so it's best to just classify it as degenerated. In this case, the triangle could be: Right, Acute, obtuse or degenerate

2) Ignore the classifications by side and by angle and classify the triangle only as a degenerated triangle

What do you think?

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.