Skip to content

Instantly share code, notes, and snippets.

@jgphilpott
Last active November 18, 2022 12:18
Show Gist options
  • Save jgphilpott/1378cc2cccde6d65c5fb2b6111b5a98f to your computer and use it in GitHub Desktop.
Save jgphilpott/1378cc2cccde6d65c5fb2b6111b5a98f to your computer and use it in GitHub Desktop.
A collection of functions for performing trigonometry in JavaScript.
# Requirement: https://gist.github.com/jgphilpott/092c0f3e1bcfa75f543e8485b9b23e7d
# In all triangles the sum of the angles is always equal to 180 degrees.
angle$angles = (a = null, b = null) ->
if a + b < 180
return 180 - a - b
else
return null
# Pythagorean Theorem is a^2 + b^2 = c^2 ... where 'c' is the hypotenuse.
side$sides = (a = null, b = null, c = null) ->
if typeof a == "number" and typeof b == "number" and not c
return Math.sqrt Math.pow(a, 2) + Math.pow(b, 2)
else if typeof a == "number" and not b and typeof c == "number"
return Math.sqrt Math.pow(c, 2) - Math.pow(a, 2)
else if not a and typeof b == "number" and typeof c == "number"
return Math.sqrt Math.pow(c, 2) - Math.pow(b, 2)
else
return null
# Remember: SOH-CAH-TOA
angle$sides = (hypotenuse = null, adjacent = null, opposite = null) ->
if typeof hypotenuse == "number" and not adjacent and typeof opposite == "number"
return rad$deg Math.asin opposite / hypotenuse
else if typeof hypotenuse == "number" and typeof adjacent == "number" and not opposite
return rad$deg Math.acos adjacent / hypotenuse
else if not hypotenuse and typeof adjacent == "number" and typeof opposite == "number"
return rad$deg Math.atan opposite / adjacent
else
return null
# Remember: SOH-CAH-TOA
side$angle = (angle = null, hypotenuse = null, adjacent = null, opposite = null) ->
if typeof angle == "number" and angle < 180
if typeof hypotenuse == "number"
if adjacent
return hypotenuse * Math.cos deg$rad angle
else if opposite
return hypotenuse * Math.sin deg$rad angle
else
return null
else if typeof adjacent == "number"
if hypotenuse
return adjacent / Math.cos deg$rad angle
else if opposite
return adjacent * Math.tan deg$rad angle
else
return null
else if typeof opposite == "number"
if hypotenuse
return opposite / Math.sin deg$rad angle
else if adjacent
return opposite / Math.tan deg$rad angle
else
return null
else
return null
else
return null
// Generated by CoffeeScript 2.7.0
// Requirement: https://gist.github.com/jgphilpott/092c0f3e1bcfa75f543e8485b9b23e7d
var angle$angles, angle$sides, side$angle, side$sides;
// In all triangles the sum of the angles is always equal to 180 degrees.
angle$angles = function(a = null, b = null) {
if (a + b < 180) {
return 180 - a - b;
} else {
return null;
}
};
// Pythagorean Theorem is a^2 + b^2 = c^2 ... where 'c' is the hypotenuse.
side$sides = function(a = null, b = null, c = null) {
if (typeof a === "number" && typeof b === "number" && !c) {
return Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
} else if (typeof a === "number" && !b && typeof c === "number") {
return Math.sqrt(Math.pow(c, 2) - Math.pow(a, 2));
} else if (!a && typeof b === "number" && typeof c === "number") {
return Math.sqrt(Math.pow(c, 2) - Math.pow(b, 2));
} else {
return null;
}
};
// Remember: SOH-CAH-TOA
angle$sides = function(hypotenuse = null, adjacent = null, opposite = null) {
if (typeof hypotenuse === "number" && !adjacent && typeof opposite === "number") {
return rad$deg(Math.asin(opposite / hypotenuse));
} else if (typeof hypotenuse === "number" && typeof adjacent === "number" && !opposite) {
return rad$deg(Math.acos(adjacent / hypotenuse));
} else if (!hypotenuse && typeof adjacent === "number" && typeof opposite === "number") {
return rad$deg(Math.atan(opposite / adjacent));
} else {
return null;
}
};
// Remember: SOH-CAH-TOA
side$angle = function(angle = null, hypotenuse = null, adjacent = null, opposite = null) {
if (typeof angle === "number" && angle < 180) {
if (typeof hypotenuse === "number") {
if (adjacent) {
return hypotenuse * Math.cos(deg$rad(angle));
} else if (opposite) {
return hypotenuse * Math.sin(deg$rad(angle));
} else {
return null;
}
} else if (typeof adjacent === "number") {
if (hypotenuse) {
return adjacent / Math.cos(deg$rad(angle));
} else if (opposite) {
return adjacent * Math.tan(deg$rad(angle));
} else {
return null;
}
} else if (typeof opposite === "number") {
if (hypotenuse) {
return opposite / Math.sin(deg$rad(angle));
} else if (adjacent) {
return opposite / Math.tan(deg$rad(angle));
} else {
return null;
}
} else {
return null;
}
} else {
return null;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment