Classify a triangle - GeeksforGeeks
We are given co-ordinates of a triangle. The task is to classify this triangle on basis of sides and angle.
We can solve this problem by first calculating the side length and then classifying on comparing of side lengths. Classification by sides is simple, if all sides are equal, triangle will be equilateral, if any two sides are equal triangle will be Isosceles otherwise it will be Scalene.
Now angle can be classified by Pythagoras theorem, if sum of square of two sides is equal to square of third side, triangle will be right angle, if less triangle will be acute angle else it will be obtuse angle triangle.
Read full article from Classify a triangle - GeeksforGeeks
We are given co-ordinates of a triangle. The task is to classify this triangle on basis of sides and angle.
We can solve this problem by first calculating the side length and then classifying on comparing of side lengths. Classification by sides is simple, if all sides are equal, triangle will be equilateral, if any two sides are equal triangle will be Isosceles otherwise it will be Scalene.
Now angle can be classified by Pythagoras theorem, if sum of square of two sides is equal to square of third side, triangle will be right angle, if less triangle will be acute angle else it will be obtuse angle triangle.
struct
point
{
int
x, y;
point() {}
point(
int
x,
int
y) : x(x), y(y) {}
};
// Utility method to return square of x
int
square(
int
x)
{
return
x * x;
}
// Utility method to sort a, b, c; after this
// method a <= b <= c
void
order(
int
&a,
int
&b,
int
&c)
{
int
copy[3];
copy[0] = a;
copy[1] = b;
copy[2] = c;
sort(copy, copy + 3);
a = copy[0];
b = copy[1];
c = copy[2];
}
// Utility method to return Square of distance
// between two points
int
euclidDistSquare(point p1, point p2)
{
return
square(p1.x - p2.x) + square(p1.y - p2.y);
}
// Method to classify side
string getSideClassification(
int
a,
int
b,
int
c)
{
// if all sides are equal
if
(a == b && b == c)
return
"Equilateral"
;
// if any two sides are equal
else
if
(a == b || b == c)
return
"Isosceles"
;
else
return
"Scalene"
;
}
// Method to classify angle
string getAngleClassification(
int
a,
int
b,
int
c)
{
// If addition of sum of square of two side
// is less, then acute
if
(a + b > c)
return
"acute"
;
// by pythagoras theorem
else
if
(a + b == c)
return
"right"
;
else
return
"obtuse"
;
}
// Method to classify triangle by sides and angles
void
classifyTriangle(point p1, point p2, point p3)
{
// Find squares of distances between points
int
a = euclidDistSquare(p1, p2);
int
b = euclidDistSquare(p1, p3);
int
c = euclidDistSquare(p2, p3);
// Sort all squares of distances in increasing order
order(a, b, c);
cout <<
"Triangle is "
+
getAngleClassification(a, b, c) +
" and "
+
getSideClassification(a, b, c) << endl;
}