Skip to content

Instantly share code, notes, and snippets.

@Demonstrandum
Last active June 9, 2017 22:05
Show Gist options
  • Save Demonstrandum/a1992fedad07ca5fedd7f53d6adcad9e to your computer and use it in GitHub Desktop.
Save Demonstrandum/a1992fedad07ca5fedd7f53d6adcad9e to your computer and use it in GitHub Desktop.
Sphere, sort of...
require 'matrix'
class Sphere
def initialize(shine, shineFocus, mood, r)
@shades = ['.', ':', '|', '*' 'o', 'e', '&', '%', '#', '@']
@shine = shine.normalize
@shineFocus = shineFocus
@mood = mood
@r = r
end
def strength brightness
if brightness <= 0
return @shades.length - 2
end
return [0, (1 - brightness) * (@shades.length - 1)].max
end
def plotPoint x, y
print(
if x**2 + y**2 <= @r**2
v = Vector[x, y, Math.sqrt(@r**2 - x**2 - y**2)].normalize
brightness = [0, (-1 * (@shine.dot(v)))].max
@shades[strength(brightness**@shineFocus + @mood)]
else
" "
end
)
end
def plotLine x
(((-2**@r).floor)..(2 * @r).ceil).each do |i|
plotPoint(x, (i / 2 + 0.5))
end
end
def plot
(([email protected])..(@r.ceil)).each do |i|
plotLine(i + 0.5)
puts
end
end
end
Sphere.new(
shine = Vector[30, 30, -60],
1,
0.0,
ARGV[0].to_i
).plot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment