| | |
Right Angled Triangle C++
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2009
Posts: 7
Reputation:
Solved Threads: 0
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
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
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
distance formula = sqrt( (x2-x1)^2 + (y2-y1)^2 ) )
Pythagorean theory = a^2 + b^2 = c^2
if you write
If your thread is solved please mark it as solved
using namespace std; you do not need to write std::something in your program.If your thread is solved please mark it as solved
•
•
•
•
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)
(0,4) , (4,1), (0, 1)
(4 - 0) / (1 - 1) = undifend
if you write
If your thread is solved please mark it as solved
using namespace std; you do not need to write std::something in your program.If your thread is solved please mark it as solved
•
•
•
•
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...
Isn't this the assignment similar to this one? http://www.daniweb.com/forums/post86...tml#post864074
Read my post: http://www.daniweb.com/forums/post864074.html#9 I have posted the algorithm. Try to implement it and let us know
Read my post: http://www.daniweb.com/forums/post864074.html#9 I have posted the algorithm. Try to implement it and let us know
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
•
•
Join Date: Nov 2008
Posts: 392
Reputation:
Solved Threads: 72
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
etc.
For two vectors, the dot product is defined as.
)
where
means the magnitude of A and
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

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.
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
For two vectors, the dot product is defined as.
where
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
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.
Last edited by StuXYZ; May 8th, 2009 at 5:07 am.
experience is the most expensive way to learn anything
![]() |
Similar Threads
- trigonometry c++ (C++)
- C To Vb 6 (Visual Basic 4 / 5 / 6)
- How to check the input (Pascal and Delphi)
- Triangle (C++)
Other Threads in the C++ Forum
- Previous Thread: cursor shapes
- Next Thread: cin in do while loop
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets





