Sha256: c08c576f5ebd072a78e2f8cad16dbbe82156b31772e01f1582f40d6a1d7bf652

Contents?: true

Size: 881 Bytes

Versions: 16

Compression:

Stored size: 881 Bytes

Contents

#!/usr/bin/env ruby
# Koch curve

require("gsl")
include Math

ONE_UNIT = 3

def koch(x, y, theta, size, order, file)
  if order == 0
    x += cos(theta)*size
    y += sin(theta)*size
    file.printf("%e %e\n", x, y)
  else
    x, y = koch(x, y, theta, size/3, order-1, file)
    theta += Math::PI/3
    x, y = koch(x, y, theta, size/3, order-1, file)
    theta -= 2.0*Math::PI/3    
    x, y = koch(x, y, theta, size/3, order-1, file)
    theta += Math::PI/3
    x, y = koch(x, y, theta, size/3, order-1, file)
  end
  return [x, y]
end

SIZE = 243
ORDER = 4

x = 0.0
y = 0.0
theta = 0.0
IO.popen("graph -T X -C -N x -N y", "w") do |io|
  io.printf("%e %e\n", x, y)
  x, y = koch(x, y, theta, SIZE, ORDER, io)
  theta -= 2.0*Math::PI/3
  x, y = koch(x, y, theta, SIZE, ORDER, io)
  theta -= 2.0*Math::PI/3
  x, y = koch(x, y, theta, SIZE, ORDER, io)
  theta -= 2.0*Math::PI/3
end

Version data entries

16 entries across 16 versions & 4 rubygems

Version Path
rb-gsl-1.16.0.4 examples/gallery/koch.rb
rb-gsl-1.16.0.3 examples/gallery/koch.rb
rb-gsl-1.16.0.3.rc1 examples/gallery/koch.rb
gsl-nmatrix-1.15.3.2 examples/gallery/koch.rb
gsl-nmatrix-1.15.3.1 examples/gallery/koch.rb
rb-gsl-1.16.0.2 examples/gallery/koch.rb
rb-gsl-1.16.0.1 examples/gallery/koch.rb
rb-gsl-1.16.0 examples/gallery/koch.rb
rb-gsl-1.15.3.2 examples/gallery/koch.rb
rb-gsl-1.15.3.1 examples/gallery/koch.rb
blackwinter-gsl-1.15.3.2 examples/gallery/koch.rb
gsl-nmatrix-1.15.3.0 examples/gallery/koch.rb
gsl-1.15.3 examples/gallery/koch.rb
gsl-1.14.7 examples/gallery/koch.rb
gsl-1.14.6 examples/gallery/koch.rb
gsl-1.14.5 examples/gallery/koch.rb