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
}

You're given a formula, which takes 1 line of code.
You've given a template which has everything except that line of code.

What do YOU want?

I want to find out how to determine the lengths of each side of the triangle? The I can save those lengths as a, b, & c. Determine which is the hypotenuse (c). Then check if c^2 = a^2 + b^2 therefore it being a right angle triangle.

So I am just asking for some help in determining the lengths of each side of the triangle if I have for example:
p1 = 0, 0
p2 = 1, 1
p3 = 3, 0

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.

Your are trying to say:
you are trying to find the length in this way
I then use the equation p2.x - p1.x to get l1 (length 1).
I then use the equation p3.x - p1.x to get l2 (length 2).

So then find the other length too.
p3.x-p2.x=l3.

To find the length with two coodirnates in hand we have the formula
X(x1,x2), Y(y1,y2)

Then
Len= Sqrt( (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))

Now, Using this find the three lengths.

For the isRightAngle() function consider the code below

if()
{
l1*l1=l2*l2+l3*l3;
print: Right traingle
  }
  else
   {
    if ()
        {
          l2*l2=l1*l1+l3*l3;
          print: Right traingle
        }
         else
                {
                 if ()
                      {
                        l3*l3=l1*l1+l2*l2;
                        print: Right traingle
                        }
                         else
                               {
                                 Print: Not a Right triangle
                                 }
                    }
     }
 }

So p2.x - p1.x = l1 or does ....

(p2.x - p1.x) + (p2.y - p1.y) = l1

I am terrible at maths, but aren't I supposed to consider the y coordinates of the points also to work out the length?

Sorry
I have reedited my post but i dont know why, my corrections were missing....here once again i am giving the formula....

If you have two co-ordinates say X(x1,x2); Y(y1,y2)....
then the length between the coordinates is defined by the formula

len=Sqrt((x1-y1)*(x1-y1)+(x2-y2)*(x2-y2))

in you case

p1 = 0, 0
p2 = 1, 1
p3 = 3, 0

l1=sqrt ((0-1)*(0-1)+(0-1)*(0-1))=sqrt(2)
l2=sqrt ((1-3)*(1-3)+(1-0)*(1-0))=sqrt(5)
l3=sqrt ((3-0)*(3-0)+(0-0)*(0-0))=sqrt(9)

You can use coordinates in any order..it doesnt matter as we are squaring the resulting value.....

P.S: Here i have represented ((3-0)*(3-0)....dont get confused its just squaring.....i dont know other way to represent square of a number.

Hope this is clear.

Just helping you out by compiling all the fact that has already been posted.
1. Find the squares of lengths a,b,c by using distance formula : [tex]d^2=(x_{1}-x_{2})^2+(y_{1}-y_{2})^2[/tex]
2. Check if the greatest of the three square is equal to the sum of the square of other two sides.
[tex]a^2=b^2+c^2 \forall a> b\geq c[/tex]

Now code the above algorithm and let us know if your find any problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.