Created
November 23, 2017 13:53
-
-
Save wallabra/23a53de20df9db93832325e319554c09 to your computer and use it in GitHub Desktop.
Make and print right triangles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RightTriangle | |
constructor: (@angle) -> | |
@angle = (@angle % 90) * Math.PI / 180 | |
print: (size) => | |
res = "" | |
last = 0 | |
ot = Math.tan(@angle) | |
for i in [0...size] | |
if i == 0 | |
res += "|" + "`" + "-".repeat(d = Math.floor(Math.tan(@angle))) | |
else | |
t = (i + 1) * Math.tan(@angle) | |
res += "|" + (if i == size - 1 then "_" else " ").repeat(d = Math.floor(t)) + (if d == last then "|" else (if d == last + 1 then "\\" else "`" + "-".repeat(Math.floor(ot - 1)))) | |
last = d | |
res += "\n" | |
return res | |
sides: (size) => | |
return { | |
h: size | |
o: size * Math.sin(@angle) | |
a: size * Math.cos(@angle) | |
} | |
module.exports = RightTriangle |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment