Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,180 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,376 people online right now. Registration is fast and FREE... Join Now!




Triangle

 
Reply to this topicStart new topic

Triangle

Vimrod
21 May, 2008 - 12:12 AM
Post #1

New D.I.C Head
*

Joined: 27 Mar, 2008
Posts: 20

CODE

#include <iostream>

using namespace std;
  
struct pointType
{
  int x;
  int y;
};

void read(pointType& p);

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

int main()
{
    // please don't change stuff in here
    
    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 (isRigtTriangle(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;
  
    system("pause");    
    return 0;          
}

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


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

    if((p1.x + p1.y * p1.x + p1.y) + (p2.x + p2.y * p2.x + p2.y) == (p3.x + p3.y) * (p3.x + p3.y))
    return true;

    // only stuck in this part
}


I can only get one part of the output:
This part:
Enter two integers for point 1: 0 0
Enter two integers for point 2: 0 1
Enter two integers for point 3: 2 0
The three points form a right-angled triangle.
Press any key to continue . . .

But not this part:
Enter two integers for point 1: 1 1
Enter two integers for point 2: 3 0
Enter two integers for point 3: 0 0
The three points form a right-angled triangle.
Press any key to continue . . .

Like it should say:
Enter two integers for point 1: 1 1
Enter two integers for point 2: 3 0
Enter two integers for point 3: 0 0
The three points don't form a right-angled triangle.
Press any key to continue . . .


I'm just having trouble with the boolean function, like isn't it just if it is true, output does form a right-angled triangle else false? I also tried return((p1.x + p1.y * p1.x + p1.y) + (p2.x + p2.y * p2.x + p2.y) == (p3.x + p3.y) * (p3.x + p3.y)); as that should output either true or false but it then just outputs only that the three points don't from a right-angled triangle. What am i missing here? Thanks in advance!



User is offlineProfile CardPM
+Quote Post

joske
RE: Triangle
21 May, 2008 - 02:03 AM
Post #2

D.I.C Head
**

Joined: 4 Sep, 2007
Posts: 158



Thanked: 12 times
My Contributions
shouldn't
CODE
if((p1.x + p1.y * p1.x + p1.y) + (p2.x + p2.y * p2.x + p2.y) == (p3.x + p3.y) * (p3.x + p3.y))

be
CODE
if((p1.x + p1.y) * (p1.x + p1.y) + (p2.x + p2.y) * (p2.x + p2.y) == (p3.x + p3.y) * (p3.x + p3.y))

or something like that? What is the formula to calculate if a triangle is right-angled?

Also: in your example code the function isRigtTriangle returns nothing when the if-statement is not true... It should return false in that case.

This post has been edited by joske: 21 May, 2008 - 02:05 AM
User is offlineProfile CardPM
+Quote Post

Vimrod
RE: Triangle
21 May, 2008 - 04:20 AM
Post #3

New D.I.C Head
*

Joined: 27 Mar, 2008
Posts: 20

QUOTE(joske @ 21 May, 2008 - 03:03 AM) *

shouldn't
CODE
if((p1.x + p1.y * p1.x + p1.y) + (p2.x + p2.y * p2.x + p2.y) == (p3.x + p3.y) * (p3.x + p3.y))

be
CODE
if((p1.x + p1.y) * (p1.x + p1.y) + (p2.x + p2.y) * (p2.x + p2.y) == (p3.x + p3.y) * (p3.x + p3.y))

or something like that? What is the formula to calculate if a triangle is right-angled?

Also: in your example code the function isRigtTriangle returns nothing when the if-statement is not true... It should return false in that case.

oh yeah thats what i meant...thx for that...the formula to calculate is a^2 + b^2 = c^2 (^ means square) yeah so even if the formula is not there it will just return false no matter what. Coz the formula is right so isnt it just a matter of return true else return false?
User is offlineProfile CardPM
+Quote Post

joske
RE: Triangle
21 May, 2008 - 05:09 AM
Post #4

D.I.C Head
**

Joined: 4 Sep, 2007
Posts: 158



Thanked: 12 times
My Contributions
you can just do:
CODE
bool isRigtTriangle(const pointType p1, const pointType p2, const pointType p3)
{
    return ((p1.x + p1.y) * (p1.x + p1.y) + (p2.x + p2.y) * (p2.x + p2.y) == (p3.x + p3.y) * (p3.x + p3.y));
}


but still the formula is not correct I think: you have to take the length of the lines between the points for a, b, and c. Right now you are adding up the x and y coordinate and take the square of that...

This post has been edited by joske: 21 May, 2008 - 05:11 AM
User is offlineProfile CardPM
+Quote Post

joske
RE: Triangle
21 May, 2008 - 05:14 AM
Post #5

D.I.C Head
**

Joined: 4 Sep, 2007
Posts: 158



Thanked: 12 times
My Contributions
The length of a line between two points (x1,y1) and (x2,y2) is:

length = sqrt((x2-x1)^2 + (y2-y1)^2)

c++ code:
CODE
float length = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 11:09AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month