Skip to content

Instantly share code, notes, and snippets.

@wallabra
Created November 23, 2017 13:53
Show Gist options
  • Save wallabra/23a53de20df9db93832325e319554c09 to your computer and use it in GitHub Desktop.
Save wallabra/23a53de20df9db93832325e319554c09 to your computer and use it in GitHub Desktop.
Make and print right triangles
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