I want to make a program which takes 3 inputs by the user. Each of those numbers should be the border of a triangle.
There are three main types of triangles:
-triangle rectangle
-acute triangle
-obtuse triangle
The program should say, of which type such triangle could be.
To solve this problem I thought about the following algorithm:
I check the triangle rectangle with pytagoras, the acute if two sites are equal the imposible if adding two sites is greater or equal to the third and the obtuse as "else".
Code:
Is this the same logic about triangles that you had?
Acute = three angles 90 degrees or less
Obtuse = one angle is greater than 90 degrees
Rectangle Triangle = one angle is equal to 90 degrees
Putting into the program these three angles should show these results:
Thanks eugendakin. But actually the inputs aren't the angles. They are the length of the borders of the triangles. Sorry for expressing myself badly...
Does you code pass the test if you check for impossibility as first case?
Yes, I think there must be a mistake in my algorithm. Thanks enoizat anyway.
Thanks also for your help lastchance. With the discriminant you actually check the triangle on pythagoras, right? So if discriminant=0 the triangle should have one angle which is 90 degrees.
But what do you intend with the swaps you use for checking if it is imposible?
Just tested your program and it works!
Final program:
With the discriminant you actually check the triangle on pythagoras, right? So if discriminant=0 the triangle should have one angle which is 90 degrees.
But what do you intend with the swaps you use for checking if it is imposible?
The swaps are intended just to make sure that the lengths are in ascending order d1 <= d2 < d3 before doing anything else. It's easier to apply the remaining logic if you can be sure of which is the longest side and which are the other two.
My logic for triangle type actually came from applying the cosine rule (in reverse) for the angle between the two smallest sides. This will determine whether the angle is obtuse, acute or right-angled (cosine is negative, positive or zero). It generalises Pythagoras. It comes down to the same conditions as in the YouTube video that @MathHead200 drew your attention to, which reaches the same principle in a more informal way.