Sha256: ed9de2f49768a7a9401a393310b0606394a2eedc4a0e83bd3855ff0b4540b4e1

Contents?: true

Size: 1.25 KB

Versions: 10

Compression:

Stored size: 1.25 KB

Contents

#
# Graphing 2D Equations
# by Daniel Shiffman. 
# 
# Graphics the following equation: 
# sin(n*cos(r) + 5*theta) 
# where n is a function of horizontal mouse location.  
#
 
def setup
  size(640, 360)
end

def draw
  load_pixels
  n = (mouse_x * 10.0) / width
  w = 16.0            # 2D space width
  h = 16.0            # 2D space height
  dx = w / width      # Increment x this amount per pixel
  dy = h / height     # Increment y this amount per pixel
  x = -w / 2          # Start x at -1 * width / 2
  width.times do |i|
    y = -h / 2        # Start y at -1 * height / 2
    height.times do |j|
      r = sqrt((x * x) + (y * y))    # Convert cartesian to polar
      theta = atan2(y, x)            # Convert cartesian to polar
      # Compute 2D polar coordinate function
      val = sin(n * cos(r) + 5 * theta)        # Results in a value between -1 and 1
      #val = cos(r)                            # Another simple function
      #val = sin(theta)                        # Another simple function
      # Map the resulting value to a grayscale value
      pixels[i + j * width] = color((val + 1.0) * 255.0/2.0)     # Scale to between 0 and 255
      y += dy                # Increment y
    end
    x += dx                  # Increment x
  end
  update_pixels
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
ruby-processing-2.6.3 samples/processing_app/basics/math/graphing_2_d_equation.rb
ruby-processing-2.6.2 samples/processing_app/basics/math/graphing_2_d_equation.rb
ruby-processing-2.6.1 samples/processing_app/basics/math/graphing_2_d_equation.rb
ruby-processing-2.6.0 samples/processing_app/basics/math/graphing_2_d_equation.rb
ruby-processing-2.5.1 samples/processing_app/basics/math/graphing_2_d_equation.rb
ruby-processing-2.5.0 samples/processing_app/basics/math/graphing_2_d_equation.rb
ruby-processing-2.4.4 samples/processing_app/basics/math/graphing_2_d_equation.rb
ruby-processing-2.4.3 samples/processing_app/basics/math/graphing_2_d_equation.rb
ruby-processing-2.4.2 samples/processing_app/basics/math/graphing_2_d_equation.rb
ruby-processing-2.4.1 samples/processing_app/basics/math/graphing_2_d_equation.rb