943,867 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2429
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 7th, 2009
0

Right Angled Triangle C++

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SallyJ is offline Offline
7 posts
since Apr 2009
May 7th, 2009
0

Re: Right Angled Triangle C++

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
Reputation Points: 215
Solved Threads: 186
Veteran Poster
NathanOliver is offline Offline
1,066 posts
since Apr 2009
May 7th, 2009
0

Re: Right Angled Triangle C++

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)
Reputation Points: 76
Solved Threads: 6
Junior Poster
djextreme5 is offline Offline
103 posts
since Mar 2009
May 7th, 2009
0

Re: Right Angled Triangle C++

Click to Expand / Collapse  Quote originally posted by djextreme5 ...
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
Reputation Points: 215
Solved Threads: 186
Veteran Poster
NathanOliver is offline Offline
1,066 posts
since Apr 2009
May 7th, 2009
0

Re: Right Angled Triangle C++

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...
Reputation Points: 76
Solved Threads: 6
Junior Poster
djextreme5 is offline Offline
103 posts
since Mar 2009
May 7th, 2009
0

Re: Right Angled Triangle C++

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.
Reputation Points: 215
Solved Threads: 186
Veteran Poster
NathanOliver is offline Offline
1,066 posts
since Apr 2009
May 7th, 2009
0

Re: Right Angled Triangle C++

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
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
May 7th, 2009
0

Re: Right Angled Triangle C++

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
Reputation Points: 215
Solved Threads: 186
Veteran Poster
NathanOliver is offline Offline
1,066 posts
since Apr 2009
May 8th, 2009
0

Re: Right Angled Triangle C++

Reputation Points: 10
Solved Threads: 0
Newbie Poster
bugista is offline Offline
19 posts
since Apr 2009
May 8th, 2009
1

Re: Right Angled Triangle C++

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.
Reputation Points: 732
Solved Threads: 134
Practically a Master Poster
StuXYZ is offline Offline
659 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: cursor shapes
Next Thread in C++ Forum Timeline: cin in do while loop





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC