Hi
I need help with a function to determine if a triangle is a right angled triangle when three points are entered. Hers's what I have so far, it's the function at the bottomthat I struggle with:

#include <iostream>

using namespace std;

struct pointType
{
int x;
int y;
};

void read(pointType& p);

bool isRightTriangle(const pointType p1, const pointType p2, const pointType p3);

int 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);

if (isRightTriangle(point1, point2, point3))
cout << "The three points form a right-angled triangle." << endl;
else
cout << "The three points don't form a right-angled triangle." << endl;

return 0;
}

void read(pointType& p)
{
cin >> p.x >> p.y;
}

bool isRightTriangle(const pointType p1, const pointType p2, const pointType p3)
{

// This is the function I'm struggling with

}

Thanks

## Recommended Answers

you need to use the distance formula to get length of the legs of the triangle and then the Pythagorean theory to see if its a right triangle.

distance formula = sqrt( (x2-x1)^2 + (y2-y1)^2 ) )
Pythagorean theory = a^2 + b^2 = c^2

An easy way to solve this problem is by find the slope of the three lines and checking if any of the slopes happen to be negative reciprocals of the remaining two...

Slope(m) = (Y2 - Y1) / (X2 - X1)

This thread is getting a bit messy. So I am going to write a quick summary and how I would tackle the problem.

First: Coordinates are almost never integers, and once you have solved a 2d problem you often need a 3d solution, and sometimes higher dimensions.

What …

## All 10 Replies

you need to use the distance formula to get length of the legs of the triangle and then the Pythagorean theory to see if its a right triangle.

distance formula = sqrt( (x2-x1)^2 + (y2-y1)^2 ) )
Pythagorean theory = a^2 + b^2 = c^2

An easy way to solve this problem is by find the slope of the three lines and checking if any of the slopes happen to be negative reciprocals of the remaining two...

Slope(m) = (Y2 - Y1) / (X2 - X1)

An easy way to solve this problem is by find the slope of the three lines and checking if any of the slopes happen to be negative reciprocals of the remaining two...

Slope(m) = (Y2 - Y1) / (X2 - X1)

the only problem with this is that if the triangle has a horizontal leg and a vertical leg you will have division by 0
(0,4) , (4,1), (0, 1)

(4 - 0) / (1 - 1) = undifend

the only problem with this is that if the triangle has a horizontal leg and a vertical leg you will have division by 0
(0,4) , (4,1), (0, 1)

(4 - 0) / (1 - 1) = undifend

I am pretty sure that problem can be solved by putting that in an 'if statement' if it is solvable, it will else it should output something that you want to output. I haven't tried that though...

you defiantly can use an if statement and keep track that you have an undefined slope because a slope of 0 would be perpendicular to that.

i just new i had seen this problem before just couldnt find the thread to get the link. siddhant3s your equations look better than mine :(

Refer this....

This thread is getting a bit messy. So I am going to write a quick summary and how I would tackle the problem.

First: Coordinates are almost never integers, and once you have solved a 2d problem you often need a 3d solution, and sometimes higher dimensions.

What has been missed (I think), is a clear understanding of what can be done with coordinate transforms and vectors.

Considers a triangle defined by 3 points A, B ,C and each point
is A is (ax,ay) , B is (bx,by) etc...

Any linear translation or rotation or scaling, does not change the angle . For example triangle if you cut a triangle from paper and then drop it on your desk. You can rotate it any way you like cand you can place it anywhere you like and the angles don't change.

That relates to the problem in that you can translate the triangle to an origin. e.g after entering three points A,B,C as above, you shift the triangle by -A to get three new points which include the origin (0,0).

However, you can find the vector displacement that connects the points. For example, if you want to go from A to B, then you need to move (bx-ax,by-ay), likewize B to C is (cx-bx,cy-by) and going from C to A is (ax-cx,ay-cy), these vectors can be written as
$$\vec{AB}$$ etc.

For two vectors, the dot product is defined as.
$$\vec{A}.\vec{B}=|\vec{A}||\vec{B}|\cos(\theta)$$
where $$|\vec{A}|$$ means the magnitude of A and
$$\theta$$ is the angle between the vectors

What is really useful is that it is VERY easy to calcuate.
It is readily provable that for a vector in cartisian coordinates (x,y,z etc) as you have, that
$$\vec{A}.\vec{B}=ax*bx+ay*by$$
where * is the multiplication.

E.g. if A=(1,2) and B=(4,7)
The dot product is 1*4+7*2=18.

Now the most useful thing about this is that cos(90) = 0.
So you test two vectors and they are perpendicular then you always get a zero dot product.

So to test your triangle, you calculate the three vectors |AB|, |BC| and |CA| and determine if any two have zero dot product. This is normally easier than calculating all the length. It doesn't involve sqrt functions, [Although you can avoid them in the length system if you are careful]. It also allows you to calcuate a large number of other things when needed. e.g. the area (use a cross product), regardless if the triangle has a 90 angle.

This method requires at maximium, three vector subtrations ,and three vector multiplications. It is mostly robust to special cases : You only need to check that two of the points are not identical. That makes it simpler than the methods proposed above.

commented: Sounds like a plan! +17

Some notes:
If points coordinates are not integers you never (or rarery) get exactly zero dot product. Furthemore, siddhant3s's solution (based on Pythagorean theorem) is faster than that (no need to calculate three expressions in the worst case). However siddhant3s's solution is incomplete too (does not take into account that it's floating-point calculations)...

Be a part of the DaniWeb community

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