Skip to content

Instantly share code, notes, and snippets.

@jaymcgavren
Last active May 15, 2019 07:09
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jaymcgavren/89b1dafe08e3b922e71f0bfd2787b7f2 to your computer and use it in GitHub Desktop.
"tree" sample from ruby-processing, ported to Visor.
# http://processing.org/learning/topics/tree.html
# by Joe Holt
color_mode RGB, 1
smooth
@x = 0.0
@dx = width / 100
def draw
background 0
stroke 1, 1, 1
# Let's pick an angle 0 to 90 degrees based on the mouse position
a = (@x / width) * 90
# Convert it to radians
@theta = a * Math::PI / 180
# Start the tree from the bottom of the screen
translate(width / 2, height)
# Draw a line 60 pixels
h = height / 3
line(0, 0, 0, -h)
# Move to the end of that line
translate(0,-h)
# Start the recursive branching!
branch(h)
@x += @dx
if @x > width || @x < 0
@dx = - @dx
@x += @dx * 2
end
end
def branch(h)
# Each branch will be 2/3rds the size of the previous one
h *= 0.66
# All recursive functions must have an exit condition!!!!
# Here, ours is when the length of the branch is 2 pixels or less
if h > 2
push_matrix # Save the current state of transformation (i.e. where are we now)
rotate(@theta) # Rotate by theta
line(0, 0, 0, -h) # Draw the branch
translate(0, -h) # Move to the end of the branch
branch(h) # Ok, now call myself to draw two new branches!!
pop_matrix # Whenever we get back here, we "pop" in order to restore the previous matrix state
# Repeat the same thing, only branch off to the "left" this time!
push_matrix
rotate(- @theta)
line(0, 0, 0, -h)
translate(0, -h)
branch(h)
pop_matrix
end
end
# The MIT License (MIT)
#
# Copyright (c) 2019 Jay McGavren
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment