Created
October 21, 2014 00:10
-
-
Save ymurase/aec532a82f5862170356 to your computer and use it in GitHub Desktop.
A sample of ruby processing
This file contains 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
require 'pp' | |
class Pendulum | |
def initialize(x, y, r) | |
@origin = PVector.new(x, y) | |
@r = r | |
@angle = Math::PI / 4 | |
@a_velocity = 0.0 | |
@a_acceleration = 0.0 | |
@damping = 0.995 | |
end | |
def update | |
gravity = 0.4 | |
@a_acceleration = (-1.0 * gravity / @r) * Math.sin(@angle) | |
@a_velocity += @a_acceleration | |
@angle += @a_velocity | |
@a_velocity *= @damping | |
end | |
def display | |
x = @r * Math.sin(@angle) + @origin.x | |
y = @r * Math.cos(@angle) + @origin.y | |
stroke(0) | |
line(@origin.x, @origin.y, x, y) | |
fill(175) | |
ellipse(x, y, 16, 16) | |
end | |
end | |
def setup | |
size(640,360) | |
@pendulum = Pendulum.new(width/2, 10, 125) | |
end | |
def draw | |
background(255) | |
@pendulum.update | |
@pendulum.display | |
end |
monkstone
commented
Feb 18, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment