Hi, can you help me just write an algorithm (not coding) to solve to this task. I have to write a program that takes in 3 points on a graph & determines if they form a right-angled triangle. More detail below.
Can you give me some advice on how to work out the lengths of each side of the triangle.
For eg; I input point 1 (p1), then I input p2 & p3.
I then use the equation p2.x - p1.x to get l1 (length 1).
I then use the equation p2.x - p1.x to get l2 (length 2).
If l1 > l2 then l1 is the hypotenuse, else l2 is the hypotenuse.
and so on.
Modify program triangle.cpp (available on Moodle) using struct that defines a two-dimensional Point (x,y), and include a Boolean function isRightTriangle, which takes three parameters of type Point and returns true if the three points form a right-angled triangle, and false otherwise.
Note:1.For a right-angled triangle (refer to http://en.wikipedia.org/wiki/Right-angled_triangle), the square of the length of the hypotenuse equals the sum of the squares of the lengths of the two other sides. If the hypotenuse has length c, and the legs have lengths a and b, then the theorem states that a^2 + b^2 = c^2.
2.For example, three points (0, 0), (0, 1) and (2, 0) form a right-angled triangle.
Two examples are listed as follows:
Example 1:
Enter two integers for point 1: 0 0 Enter two integers for point 2: 0 1 Enter two integers for point 3: 2 0 The three points form a right-angled triangle.Example 2:
Enter two integers for point 1: 1 1 Enter two integers for point 2: 3 0 Enter two integers for point 3: 0 0 The three points don't form a right-angled triangle.
This is the template I am to use to create the program:
#include <iostream>
using namespace std;
struct pointType
{
int x;
int y;
};
void read(pointType& p);
// this function read values for point p
bool isRightTriangle(const pointType p1, const pointType p2, const pointType p3);
// this function return true if the 3 points form a right-angled triangle,
// and false otherwise
int main()
{
// don't modify any code in main()
pointType point1, point2, point3;
cout << "Enter two integers for point 1: " << flush;
read(point1);
cout << "Enter two integers for point 2: " << flush;
read(point2);
cout << "Enter two integers for point 3: " << flush;
read(point3);
// don't modify any code in main()
if (isRightTriangle(point1, point2, point3))
cout << "The three points form a right-angled triangle." << endl; // don't modify
else
cout << "The three points don't form a right-angled triangle." << endl; // don't modify
return 0; // don't modify
}
void read(pointType& p)
{
cin >> p.x >> p.y;
}
bool isRightTriangle(const pointType p1, const pointType p2, const pointType p3)
{
// write the body here
// to implement this function, you can define other function(s) if necessary
}