Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Turned a math practice problem into a program.
- // triangles holds an array of arrays of points, each
- // of which defines a triangle. The program finds out
- // whether or not those are right triangles.
- // I decided to write this program because doing it
- // by hand was boring and I was bored. Hey, who says
- // nodejs has to be just for web apps / servers? Works
- // fine for a scripting language too!
- var inverse = num => 1 / num;
- var slope = (p1, p2) => (p1.y - p2.y) / (p1.x - p2.x);
- function right(p1, p2, p3){
- // call node with flag '--harmony-destructuring' to enable this
- var [ s1, s2, s3 ] = [ slope(p1, p2), slope(p1, p3), slope(p2, p3) ];
- return s1 == inverse(s2) || s1 == inverse(s3) || s2 == inverse(s3);
- }
- var triangles = [
- [
- {x:0, y:7},
- {x:-4, y:-2},
- {x:5, y:2}
- ],
- [
- {x:5, y:5},
- {x:5*Math.sqrt(3), y:-5*Math.sqrt(3)},
- {x:-5, y:-5}
- ],
- [
- {x:4, y:5},
- {x:-4, y:-1},
- {x:2, y:-9}
- ],
- [
- {x:8, y:6},
- {x:4, y:4},
- {x:-1, y:10}
- ]
- ];
- console.log(triangles.map(
- function(set){
- return "Is a right triangle? " + right(set[0], set[1], set[2]);
- }));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement