A triangle is a shape with three straight lines and three corners. There are different types of triangles:

**Equilateral triangle:**All three sides of this triangle are the same length.**Isosceles triangle:**This triangle has two sides that are the same length.**Right triangle:**One of the angles in this triangle is 90 degrees.**Scalene triangle:**None of the sides in this triangle are the same length.

Create a program that takes inputs the coordinates of three points (`point1x`

, `point1y`

, `point2x`

, `point2y`

, `point3x`

, `point3y`

). The program should then determine if these points form an isosceles, equilateral, right-angled, or scalene triangle.

**Sample input**

Point1 0 0Point2 1 0Point3 1 1

Points of a triangle

**Sample output**

Triangle is an Isosceles Right triangle.

As we have three points, we need to find the distance between each pair of points to determine the length of each side. With two coordinates for each side, we can calculate the distance using a formula. Then, we can compare the lengths of each side to figure out the shape of the triangle.

To find the length of each side, we'll use a formula to calculate the squared distance between pairs of points.

Here’s how it works:

s1 = ((point2x-point1x)*(point2x-point1x)) + ((point2y-point1y)*(point2y-point1y)); //Distance between point 1 and point 2s2 = ((point3x-point2x)*(point3x-point2x)) + ((point3y-point2y)*(point3y-point2y)); //Distance between point 2 and point 3s3 = ((point1x-point3x)*(point1x-point3x)) + ((point1y-point3y)*(point1y-point3y)); //Distance between point 3 and point 1

Once we have these distance squares, we’ll compare each side to the others to figure out if the triangle is isosceles, equilateral, right-angled, or scalene.

**Equilateral triangle:**If three sides are all equal, it’s an equilateral triangle.

**Isosceles right triangle:**If any two sides are equal:

An isosceles right triangle is a triangle with at least two sides of equal length. The angles opposite the equal sides are also equal.

If not, it’s just an isosceles triangle.

**Right triangle:**If the sum of the squares of two sides equals the square of the third side, it’s a right triangle.

**Scalene triangle:**If none of the above conditions are met, it’s a scalene triangle.

Let us write the complete code below:

#include <iostream>using namespace std;int main(){int point1x, point1y, point2x, point2y, point3x, point3y, s1, s2, s3 ;point1x = 0, point1y = 0;point2x = 1, point2y = 0;point3x = 1, point3y = 1;s1 = ((point2x-point1x)*(point2x-point1x)) + ((point2y-point1y)*(point2y-point1y));s2 = ((point3x-point2x)*(point3x-point2x)) + ((point3y-point2y)*(point3y-point2y));s3 = ((point1x-point3x)*(point1x-point3x)) + ((point1y-point3y)*(point1y-point3y));// Distances-squares of each side has been computed as s1, s2, s3if (s1==s2 && s2==s3){cout<<"Triangle is an Equilateral triangle.";}else if ( s1==s2 || s1==s3 || s2==s3 ){if ( s1==s2+s3 || s2==s1+s3 || s3==s1+s2 ){cout<<"Triangle is an Isosceles Right triangle." << endl;}else{cout<<" Triangle is Isosceles Triangle." << endl;}}else if ( s1==s2+s3 || s2==s1+s3 || s3==s1+s2 ){cout<<" Triangle is a Right-Angled Triangle." << endl;}else{cout<<" Triangle is Scalene Triangle." << endl;}return 0;}

**Lines 7–9:**We initialize the variables for the coordinates of three points.**Lines 10–12:**We calculate the squared distance of each side of the triangle:`s1`

is the squared distance between point 1 and point 2.`s2`

is the squared distance between point 2 and point 3.`s3`

is the squared distance between point 3 and point 1.

**Lines 14–17:**We check if all three sides are equal. If true, we conclude that the triangle is equilateral.**Lines 18–28:**If any two sides are equal, we further check if the triangle is an isosceles right triangle or just an isosceles triangle:If the sum of the squares of the equal sides equals the square of the third side, it's an isosceles right triangle.

Otherwise, it's just an isosceles triangle.

**Lines 29–32:**We check if the triangle is a right-angled triangle:If the sum of the squares of two sides equals the square of the third side, it’s a right-angled triangle.

**Lines 33–36:**If none of the above conditions are met, we conclude that the triangle is a scalene triangle.

This answer covers calculating the distances between points and comparing these distances to determine the type of triangle. By using distance formulas and logical conditions, we can classify a triangle as equilateral, isosceles, right-angled, or scalene. This method helps in understanding geometric properties through coding.

Copyright ©2024 Educative, Inc. All rights reserved

TRENDING TOPICS