Hacker Challenge: Triangular Shapes

Let us devise a way to identify triangular shapes from the given coordinates.

We'll cover the following

In this lesson, we will write a program that takes three coordinates of a triangle and determines whether it is one of the following triangles: equilateral triangle, isosceles triangle, right triangle, isosceles-right triangle, or a scalene triangle.

So let’s begin!

Triangular shapes

A triangle is a polygon that has three edges and three vertices. We have different shapes of the triangle given below. If a triangle has three equal sides, we call it an equilateral triangle. If a triangle has two equal sides, we call it an isosceles triangle. If a triangle has one angle of 90 degrees, we call it a right triangle. If a triangle has no equal sides, we call it a scalene triangle.

Problem statement

Write a program that takes as input three points each with x-axis and y-axis coordinates from a user–p1x, p1y, p2x, p2y, p3x, p3y–and determines whether these points are the coordinates of an isosceles, equilateral, right-angled or, a scalene triangle.

Sample input

P1	0 0
P2	1 0
P3	1 1

Sample output

Right-Angled Triangle

To solve the above problem, we need three points, where each point has an x-axis and a y-axis coordinate. For each point, we will take two numbers as input, so we need a total of six numbers as input for three points.

Let’s break it down:

Let’s say we have six variables p1x, p1y, p2x, p2y, p3x, and p3y. For each point p, we are taking two inputs, x and y.

int p1x, p1y, p2x, p2y, p3x, p3y;
cin >> p1x >> p1y; // taking input for 1st point P1
cin >> p2x >> p2y; // taking input for 2nd point P2
cin >> p3x >> p3y; // taking input for 3rd point P3

Now we have three points. To solve this problem, we need to calculate the distance of each side. Each side has two coordinates. How do we calculate the distances of the sides and compare each side with the other sides to determine the shape?

We discussed the distance-square formula earlier. We will use the same formula to compute the squared distance metric of these three sides.

s1 = ((p2x-p1x)*(p2x-p1x)) + ((p2y-p1y)*(p2y-p1y)); //Distance between point 1 and point 2
s2 = ((p3x-p2x)*(p3x-p2x)) + ((p3y-p2y)*(p3y-p2y)); //Distance between point 2 and point 3
s3 = ((p3x-p1x)*(p3x-p1x)) + ((p3y-p1y)*(p3y-p1y)); //Distance between point 3 and point 1

After computing the distance square of the three sides s1, s2, and s3, we will compare each side with another side to determine whether it is an isosceles, equilateral, right-angled, or scalene triangle.

To check whether a given triangle is a right triangle or not, we need to know the Pythagoras theorem, which states:

(hypotenuse)2=(perpendicular)2+(base)2(hypotenuse)^2={(perpendicular)^2 + (base)^2}

To simplify the above formula:

hypotenuse=(perpendicular)2+(base)2hypotenuse=\sqrt{(perpendicular)^2 + (base)^2}

To compute the distance of the perpendicular and the base, we will use the distance-square formula.

DistanceSquare=(p2x−p1x)2+(p2y−p1y)2Distance Square=(p2x-p1x)^2 + (p2y-p1y)^2

According to the formula above, we will have:

s3=s2+s1s3 ={s2 + s1}

Let’s draw this on the x-y plane:

In a right-angled triangle, the sum of the two sides will be equal to the third side. Which two sides? Pause for a while and think about it.

In the above diagram, s1 and s2 are equal, and the sum of both s1 and s2 is equal to s3. The three sides may be rotated counterclockwise, resulting in s2s_2 being the hypotenuse, s3s_3 and s1s_1 being the base and perpendicular; or one more rotation is also possible, then s1s_1 may qualify as the diagonal and the rest as base and perpendicular.

Let us write the complete code below:

#include <iostream>
using namespace std;

int main()
{
    int p1x, p1y, p2x, p2y, p3x, p3y, s1, s2, s3 ;
    cout << "The x and y coordinates of point 1: " << endl;
    cin >> p1x >> p1y;
    cout << "The x and y coordinates of point 2: " << endl;
    cin >> p2x >> p2y;
    cout << "The x and y coordinates of point 3: " << endl;
    cin >> p3x >> p3y;
    s1 = ((p2x-p1x)*(p2x-p1x)) + ((p2y-p1y)*(p2y-p1y));
    s2 = ((p3x-p2x)*(p3x-p2x)) + ((p3y-p2y)*(p3y-p2y));
    s3 = ((p3x-p1x)*(p3x-p1x)) + ((p3y-p1y)*(p3y-p1y));
    // Distances-squares of each side has been computed as s1, s2, s3
    // Write your remaining code here...

    
    return 0;
}



Program for triangle