15,844,498 members
Articles / General Programming / Algorithms
Tip/Trick

# Algorithm for Triangles Classification

Rate me:
27 Oct 2018CPOL1 min read 18K   66   6   5
Simple algorithm to determine the type of a triangle, being informed its sides

## Introduction

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)

Obs:

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:

```--[[--
TriangleType
Determines the type of the triangle by the sides and by the angles

Language: Lua

2018, Jose Cintra
josecintra@josecintra.com
--]]--

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

-- 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
else
bySide = 3 -- Scalene
end

--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
else
byAngle = 3 -- Acute
end

end

return bySide,byAngle

end

-- Main routine

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

print("Triangle Type\n")
print("Enter the value of A side: ")
print("Enter the value of B side: ")
print("Enter the value of C side: ")

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

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

-- 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.

## History

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

## Last Words

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

Written By
Software Developer
Brazil
I am a software developer focused on Mathematics, IoT and Games.
Homepage: HTML Apps
Blog: www.josecintra.com/blog

 First Prev Next
 My vote of 4 Doom For Ever27-Oct-18 1:48 Doom For Ever 27-Oct-18 1:48
 Simplification ? Harrison Pratt23-Oct-18 4:02 Harrison Pratt 23-Oct-18 4:02
 Re: Simplification ? José Cintra23-Oct-18 6:54 José Cintra 23-Oct-18 6:54
 Nice and well... CodeWraith22-Oct-18 4:21 CodeWraith 22-Oct-18 4:21
 ... but here I have a real world addition: What about 'degenerated' triangles, those where point C lies on the line between points A and B? You often have to deal with these in computer graphics where you have to eliminate them early on because the effort to draw them is pointless (pun intended). I have lived with several Zen masters - all of them were cats. His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.
 Re: Nice and well... José Cintra22-Oct-18 16:00 José Cintra 22-Oct-18 16:00
 Last Visit: 31-Dec-99 19:00     Last Update: 22-Feb-24 10:09 Refresh 1