Skip to content

Instantly share code, notes, and snippets.

@john212
Last active February 10, 2020 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save john212/d6d248af14a15a1c388c0ca141bea351 to your computer and use it in GitHub Desktop.
Save john212/d6d248af14a15a1c388c0ca141bea351 to your computer and use it in GitHub Desktop.
Triangle Inequality Theorem and Triangle Classification
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Triangle Inequality Theorem and Triangle Classification</title>
</head>
<body>
<h2>Triangle Inequality Theorem and Triangle Classification</h2>
<P>This program will take 3 side lengths input by a user and then determine if
those sides can form a triangle. The Triangle Inequality Theorem
states that the sum of any two side lengths in a triangle must be greater than
the third side.</P>
<P>If the three sides can form a triangle, the triangle will be classified as
acute (all three angles are less than 90 degrees), right (one of the angles
is 90 degrees, the other two are acute), or obtuse (one of
the angles is greater than 90 degrees and the other two are acute). The Pythagorean
Theorem states that in a right triangle, the square of the longest side (the
hypotenuse) is equal to the sum of the squares of the other two sides. If the
longest side squared of any triangle is bigger than the sum of the squares of the other two
sides (i.e. bigger than it needs to be to make a right triangle), then the
triangle must be obtuse. If the longest side squared is smaller than the sum
of the squares of the other two sides (i.e. smaller than it needs to be to make
a right triangle), then the triangle must be acute. If the longest side
squared is equal to the sum of the squares of the other two sides, then the
triangle must be a right triangle.</P>
<P>If 2 sides are equal in length the triangle will be classified as isosceles. If
all 3 sides are equal in length the triangle will be classified as equilatereal.
Finally, if none of the sides are equal in length, the triangle will be classified
as scalene.</P>
<P>The 3 angles will be calculated in degrees using the Law of Cosines.</P>
<P>The area of the triangle will be calculated using Heron's Formula. Heron's formula uses the
three side lengths only and the semi-perimeter s = the perimeter divided by 2, i.e.
(a + b+ c)/2. Area = sqrt(s*(s-a)(s-b)(s-c)). Note that a property of the semi-perimeter is
that it is always longer than any side length.</P>
<!-- ************************************************************
Notepad++ Topic:
Adding HTML Tags and AutoClose of HTML Tags
Click Plugins-Plugins Admn then check "TextFX" Install, Restart N++
Next, Click TextFX-TextFX Settings and select "AutoClose HTML Tags"
Also: note difference between html comment tags and Javascript comment tags.
*****************************************************************-->
<P>This program is written in JavaScript.</P>
<P>By Mr. C. January, 2020</P>
<P>*********************results********************************</P>
<script type="text/javascript">
/******************************************************************
Question: Why use the main() function?
Answer: Provides a convenient way to stop a Javascipt program
Suppose you want to add simple data validation rules for user input. In
this program a user might enter a zero or a negative value for the
length of a triangle. You would like the program to stop and "start over" after
telling the user that length needs to be a positive number.
Surrounding the entire program with an "if block" will work, but that would get
very messy if you had a lot of data validation rules or if other reasons cropped up
that needed the program stopped.
An alternative way that is clearer and more elegant, and more in keeping
with the way programs like Wiring (used for Arduino programming) or Processing
(aka p5.js) work, is to make the entire program a function, typically called "main()".
Note Processing and its JavaScript port use Setup() and Draw(). The overarching
functionds are usually parameterless, but don't have to be. A "return" call from
the overarching (outer) function is all that is needed to stop
the Javascript program execution.
Reference ( Sep 28 '11 at 14:12 by Jhep):
https://stackoverflow.com/questions/550574/how-to-terminate-the-script-in-javascript
Note: you must call the main() function to run the program even if you
don't return any values from the function. If you define the function
using an expression then you must define it before you call it. This
means function expressions are NOT "hoisted".
Function Expression Format:
var myFunction = function [name]([param1[, param2[, ..., paramN]]]) {
statements
};
If you use a function delaration (as is done for main() in this program),
you can call the funtion from anywhere before or after the function is
defined. Defining a function using a constructor also works like this.
The term for this is "hoisted". Function declarations in JavaScript are
hoisted to the top of the enclosing function or global scope. You can
use the function before you declared it:
Function Declaration Format:
function name([param[, param,[..., param]]]) {
[statements]
}
******************************************************************/
main(); //must call the main() function to run the program.
function main() //See reason for main() function above in comments.
{ //start main() function block
// **** Description ****
// Determine if three side lengths input by the user can make a
// triangle and if so, classify the triangle as as acute, right,
// or obtuse. Also return the values of the angles (in degrees)
// and the area of the triangle (in square units).
// *********************
// **** Define and initialize variables ****
var a = ""; // first side length of triangle, input by user.
var b = ""; // second side length of triangle, input by user.
var c = ""; // third side length of triangle, input by user.
// *****************************************
// **** Collect user input via popup boxes ****
a = prompt("Enter the first side of the triangle", "Type the value here");
b = prompt("Enter the second side of the triangle", "Type the value here");
c = prompt("Enter the third side of the triangle", "Type the value here");
// ********************************************
/***** Convert user text input into decimal numbers ****
The parseFloat("string") function returns a number,
where "string" is the string value you want to parse.
Leading and trailing spaces are allowed and will be ignored.
If the first non space character is not a sign (+ or -), numeral (0-9),
decimal point or an exponent, and cannot be converted to a number,
parseFloat returns NaN. While parsing after the first non-space character,
if the function encounters a character other than a sign (+ or -),
numeral (0-9), a decimal point, or an exponent, it returns the value
up to that point and ignores that character and all subsequent characters.
*******************************************************/
a = parseFloat(a);
b = parseFloat(b);
c = parseFloat(c);
// Data Validation - Ensure user input is > 0 for side lengths.
if(!(a > 0) || !(b > 0) || !(c > 0))
{ //start data validation if block
document.write("<P>a=", a, ", b=" , b, ", c=" , c );
document.write("<P>a, b, and c must be greater than zero");
return // stops execution of main() function
} //endof data validation if block
/**** Determine if the three side lengths can make a triangle ****
Triangle Inequality Theorem.
The sum of any two side lengths in a triangle must be greater
than the third side.
******************************************************************/
// Initialize sums to be used in Triangle Inequality Theorem test.
var sumab = 0.0; // sum used in Triangle Ineq. Th., calculated.
var sumac = 0.0; // sum used in Triangle Ineq. Th., calculated.
var sumbc = 0.0; // sum used in Triangle Ineq. Th., calculated.
// Calculate the three possibe sums.
sumab = a + b;
sumac = a + c;
sumbc = b + c;
// Display the three side lengths.
document.write("<P>a=", a, ", b=" , b, ", c=" , c );
// Initialize and set isTriangle flag, "true" or ""false" (must be lower case).
// Set flags for Equilateral, Isosceles, Scalene Triangles.
// Set "side equality" flages.
var isTriangle = false;
var isEquilateral = false;
var isIsosceles = false;
var isScalene = false;
var aEqualsb = false;
var aEqualsc = false;
var bEqualsc = false;
// Perform the Triangle Inequality Theorem test.
// Programming note: consider using a "Case" statement in lieu of
// multiple "Elseif" statements as are used here.
// Determine if the 3 sides satisfy the Triangle Inequality Theorem
if(sumab <= c)
{//start sumab if block
document.write("<P>a, b, and c do not make a triangle, a + b =", sumab, ", which is not greater than c =", c);
return // stops execution of main() function
}//endof sumab if block
else if (sumac <= b)
{//start sumac if block
document.write("<P>a, b, and c do not make a triangle, a + c =", sumac, ", which is not greater than b =", b);
return // stops execution of main() function
}//endof sumac if block
else if (sumbc <= a)
{//start sumbc if block
document.write("<P>a, b, and c do not make a triangle, b + c =", sumbc, ", which is not greater than a =", a);
return // stops execution of main() function
}//endof sumbc if block
else
{//start else block: Triangle PASSES Triangle Inequality Theorem test.
document.write("<P>a, b, and c pass the Triangle Inequality Theorem and make a triangle");
isTriangle = true; // set isTriangle flag to TRUE
}//endof else block: Triangle PASSES Triangle Inequality Theorem test.
// Classify the triangle as Acute, Right, or Obtuse using the Pythagorean Theorem
// Note the built-in function Math.pow(base,exponent) raises a number to a power.
// example: Math.pow(x,2) = x*x
var a2 = 0.0; // first side length squared of triangle, calculated.
var b2 = 0.0; // second side length squared of triangle, calculated.
var c2 = 0.0; // third side length squared of triangle, calculated.
a2 = Math.pow(a,2);
b2 = Math.pow(b,2);
c2 = Math.pow(c,2);
// Triangle Calculations
if(isTriangle == true)
{//start isTriangle TRUE if block
if (a == b){
aEqualsb = true;
}
if (a == c){
aEqualsc = true;
}
if (b == c){
bEqualsc = true;
}
//**************************************************************************************************************
// Equilateral Triangle block:
if ((aEqualsb) && (aEqualsc) && (bEqualsc)) // a=b=c=equilateral triangle
{ //start eqilateral if block
isEquilateral = true;
document.write("<P>a, b, and c make an acute and equilateral triangle, a = b = c");
} //endof equilateral if block
//**************************************************************************************************************
// Isosceles Triangle block:
if (!isEquilateral) // only check for isosceles if not an equilateral triangle
{ //start isosceles check if block
if (aEqualsb)
{ //start aEqualsb if block
isIsosceles = true; // set isosceles flag to true
if (c2 > (a2 + b2)) // isosceles obtuse triangle
{
document.write("<P>a, b, and c make an obtuse isosceles triangle, c<sup>2</sup> =", c2, ", a<sup>2</sup> + b<sup>2</sup> =", a2 + b2);
} else if (c2 < (a2 + b2)) //isosceles acute triangle
{
document.write("<P>a, b, and c make an acute isosceles triangle, c<sup>2</sup> =", c2, ", a<sup>2</sup> + b<sup>2</sup> =", a2 + b2);
} else if (c2 == (a2 + b2)) //isosceles right triangle
{
document.write("<P>a, b, and c make a right isosceles triangle, c<sup>2</sup> =", c2, ", a<sup>2</sup> + b<sup>2</sup> =", a2 + b2);
}
} // endof aEqualsb if block
if (aEqualsc)
{ // start aEqualsc if block
isIsosceles = true; // set isosceles flag to true
if (b2 > (a2 + c2)) // isosceles obtuse triangle
{
document.write("<P>a, b, and c make an obtuse isosceles triangle, b<sup>2</sup> =", b2, ", a<sup>2</sup> + c<sup>2</sup> =", a2 + c2);
} else if (b2 < (a2 + c2)) //isosceles acute triangle
{
document.write("<P>a, b, and c make an acute isosceles triangle, b<sup>2</sup> =", b2, ", a<sup>2</sup> + c<sup>2</sup> =", a2 + c2);
} else if (bc2 == (a2 + c2)) //isosceles right triangle
{
document.write("<P>a, b, and c make a right isosceles triangle, b<sup>2</sup> =", b2, ", a<sup>2</sup> + c<sup>2</sup> =", a2 + c2);
}
} // endof aEqualsc if block
if (bEqualsc)
{ // start bEqualsc if block
isIsosceles = true; // set isosceles flag to true
if(a2 > (b2 + c2)) // isosceles obtuse triangle
{
document.write("<P>a, b, and c make an obtuse isosceles triangle, a<sup>2</sup> =", a2, ", b<sup>2</sup> + c<sup>2</sup> =", b2 + c2);
} else if (a2 < (b2 + c2)) //isosceles acute triangle
{
document.write("<P>a, b, and c make an acute isosceles triangle, a<sup>2</sup> =", a2, ", b<sup>2</sup> + c<sup>2</sup> =", b2 + c2);
} else if (a2 == (b2 + c2)) //isosceles right triangle
{
document.write("<P>a, b, and c make a right isosceles triangle, a<sup>2</sup> =", a2, ", b<sup>2</sup> + c<sup>2</sup> =", b2 + c2);
}
} // endof bEqualsc if block
} // endof isosceles check if block
//**************************************************************************************************************
// Scalene triangle block
if ((!isEquilateral) && (!isIsosceles)) // triangle is scalene if not equilateral or isosceles
{ //start scalene if block
isScalene = true; // triangle is scalene if not equilateral or isosceles
if ((a > b) && (a > c)) // a is biggest side
{ // start side a biggest if block
if (a2 > b2 + c2)
{ // start obtuse if block
document.write("<P>a, b, and c make an obtuse scalene triangle, a<sup>2</sup> =", a2, ", b<sup>2</sup> + c<sup>2</sup> =", b2 + c2);
} // endof obtuse if block
else if (a2 < b2 + c2)
{ // start acute if block
document.write("<P>a, b, and c make an acute scalene triangle, a<sup>2</sup> =", a2, ", b<sup>2</sup> + c<sup>2</sup> =", b2 + c2);
} // endof acute if block
else if (a2 == b2 + c2)
{ // start right if block
document.write("<P>a, b, and c make a right scalene triangle, a<sup>2</sup> =", a2, ", b<sup>2</sup> + c<sup>2</sup> =", b2 + c2);
} // endof right if block
} // endof side a biggest if block
if ((b > a) && (b > c)) // b is biggest side
{ // start side a biggest if block
if (b2 > a2 + c2)
{ // start obtuse if block
document.write("<P>a, b, and c make an obtuse scalene triangle, b<sup>2</sup> =", b2, ", a<sup>2</sup> + c<sup>2</sup> =", a2 + c2);
} // endof obtuse if block
else if (b2 < a2 + c2)
{ // start acute if block
document.write("<P>a, b, and c make an acute scalene triangle, b<sup>2</sup> =", b2, ", a<sup>2</sup> + c<sup>2</sup> =", a2 + c2);
} // endof acute if block
else if (b2 == a2 + c2)
{ // start right if block
document.write("<P>a, b, and c make a right scalene triangle, b<sup>2</sup> =", b2, ", a<sup>2</sup> + c<sup>2</sup> =", a2 + c2);
} // endof right if block
} // endof side b biggest if block
if ((c > a) && (c > b)) // c is biggest side
{ // start side a biggest if block
if (c2 > a2 + b2)
{ // start obtuse if block
document.write("<P>a, b, and c make an obtuse scalene triangle, c<sup>2</sup> =", c2, ", a<sup>2</sup> + b<sup>2</sup> =", a2 + b2);
} // endof obtuse if block
else if (c2 < a2 + b2)
{ // start acute if block
document.write("<P>a, b, and c make an acute scalene triangle, c<sup>2</sup> =", c2, ", a<sup>2</sup> + b<sup>2</sup> =", a2 + b2);
} // endof acute if block
else if (c2 == a2 + b2)
{ // start right if block
document.write("<P>a, b, and c make a right scalene triangle, c<sup>2</sup> =", c2, ", a<sup>2</sup> + b<sup>2</sup> =", a2 + b2);
} // endof right if block
} // endof side c biggest if block
} // endof scalene if block
//**************************************************************************************************************
// Use the Law of Cosines (to avoid the Ambiguous Case of the Law of Sines) to find each angle measure.
// a^2 = b^2 + c^2 - 2bc*cos(A) where a, b, c, are sides and A, B, C are the opposite angles.
// b^2 = a^2 + c^2 - 2ac*cos(B)
// c^2 = a^2 + b^2 - 2ab*cos(C) Note: if C=90 deg, this simplifies to the Pythagorean Theorem.
// acos() is the arccos aka inverse cosine function and returns the angle given the cosine.
// the angle is returned in radians, multiply by 180 deg/pi rad to convert to degrees.
var A = 0.0;
var B = 0.0;
var C = 0.0;
A = (Math.acos(((a*a) - (b*b) - (c*c))/(-2*b*c))) * (180/Math.PI);
document.write("<P>Angle A =", A, " degrees");
B = (Math.acos(((b*b) - (a*a) - (c*c))/(-2*a*c))) * (180/Math.PI);
document.write("<P>Angle B =", B, " degrees");
C = (Math.acos(((c*c) - (a*a) - (b*b))/(-2*a*b))) * (180/Math.PI);
document.write("<P>Angle C =", C, " degrees");
// Use Heron's Formula for the area of a triangle given side-side-side.
// Memory aid, the formula involve a square root.
// So you need length to the 4th power under the square root.
// H.F.: area = sqrt[(s)(s-a)(s-b)(s-c)] where s is the semi-perimeter.
var s = 0.0;
var area = 0.0;
s = (a + b + c)/2; //calculate semi-perimeter, s is greater than a, b, or c.
area = Math.sqrt((s)*(s-a)*(s-b)*(s-c));
document.write("<P>Area of triangle =", area, " units<sup>2</sup>");
} //endof isTriangle TRUE if block
// ***************************************************
// debugging code - comment out as needed
/*
console.log("isTriangle is ",isTriangle);
console.log("aEqualsb is: ",aEqualsb);
console.log("aEqualsc is: ",aEqualsc);
console.log("bEqualsc is: ",bEqualsc);
console.log("isEquilateral is ",isEquilateral);
console.log("isIsosceles is:", isIsosceles);
console.log("isScalene is ",isScalene);
*/
// ***************************************************
} //endof main() function block
</script>
<p><b>
To check your answers, or to get more ideas to program, visit:<br>
<a href="http://www.triangle-calculator.com/">Triangle-calculator.com</a>
</b></p>
</body>
</html>
@john212
Copy link
Author

john212 commented Feb 10, 2020

Updated 2-9-2019.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment