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;
};

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;
cout << "Enter two integers for point 2: " << flush;
cout << "Enter two integers for point 3: " << flush;

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;
}

{
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

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.