Sha256: 41c9731a1f36e786d2d3e029c56f624fc74aa1b417bc9de24b46dd0697b93b17

Contents?: true

Size: 877 Bytes

Versions: 6

Compression:

Stored size: 877 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

6 entries across 6 versions & 2 rubygems

Version Path
gsl-2.1.0.3 examples/gallery/koch.rb
gsl-2.1.0.2 examples/gallery/koch.rb
gsl-2.1.0.1 examples/gallery/koch.rb
gsl-2.1.0 examples/gallery/koch.rb
gsl-1.16.0.6 examples/gallery/koch.rb
rb-gsl-1.16.0.5 examples/gallery/koch.rb