Right Angled Triangle C++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2009
Posts: 7
Reputation: SallyJ is an unknown quantity at this point 
Solved Threads: 0
SallyJ SallyJ is offline Offline
Newbie Poster

Right Angled Triangle C++

 
0
  #1
May 7th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 219
Reputation: NathanOliver is an unknown quantity at this point 
Solved Threads: 37
NathanOliver's Avatar
NathanOliver NathanOliver is offline Offline
Posting Whiz in Training

Re: Right Angled Triangle C++

 
0
  #2
May 7th, 2009
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
if you write using namespace std; you do not need to write std::something in your program.
If your thread is solved please mark it as solved
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 66
Reputation: djextreme5 is on a distinguished road 
Solved Threads: 4
djextreme5's Avatar
djextreme5 djextreme5 is offline Offline
Junior Poster in Training

Re: Right Angled Triangle C++

 
0
  #3
May 7th, 2009
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)
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 219
Reputation: NathanOliver is an unknown quantity at this point 
Solved Threads: 37
NathanOliver's Avatar
NathanOliver NathanOliver is offline Offline
Posting Whiz in Training

Re: Right Angled Triangle C++

 
0
  #4
May 7th, 2009
Originally Posted by djextreme5 View Post
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
if you write using namespace std; you do not need to write std::something in your program.
If your thread is solved please mark it as solved
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 66
Reputation: djextreme5 is on a distinguished road 
Solved Threads: 4
djextreme5's Avatar
djextreme5 djextreme5 is offline Offline
Junior Poster in Training

Re: Right Angled Triangle C++

 
0
  #5
May 7th, 2009
Originally Posted by NathanOliver View Post
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...
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 219
Reputation: NathanOliver is an unknown quantity at this point 
Solved Threads: 37
NathanOliver's Avatar
NathanOliver NathanOliver is offline Offline
Posting Whiz in Training

Re: Right Angled Triangle C++

 
0
  #6
May 7th, 2009
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.
if you write using namespace std; you do not need to write std::something in your program.
If your thread is solved please mark it as solved
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 793
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Right Angled Triangle C++

 
0
  #7
May 7th, 2009
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
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 219
Reputation: NathanOliver is an unknown quantity at this point 
Solved Threads: 37
NathanOliver's Avatar
NathanOliver NathanOliver is offline Offline
Posting Whiz in Training

Re: Right Angled Triangle C++

 
0
  #8
May 7th, 2009
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
if you write using namespace std; you do not need to write std::something in your program.
If your thread is solved please mark it as solved
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 16
Reputation: bugista is an unknown quantity at this point 
Solved Threads: 0
bugista bugista is offline Offline
Newbie Poster

Re: Right Angled Triangle C++

 
0
  #9
May 8th, 2009
Never Say Never
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Right Angled Triangle C++

 
1
  #10
May 8th, 2009
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.
Last edited by StuXYZ; May 8th, 2009 at 5:07 am.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC