Hilbert curve: Difference between revisions

Content added Content deleted
No edit summary
Line 2,251: Line 2,251:
Implemented as a Lindenmayer System, depends on JRuby or JRubyComplete
Implemented as a Lindenmayer System, depends on JRuby or JRubyComplete
<lang Ruby>
<lang Ruby>
# frozen_string_literal: true

attr_reader :hilbert
attr_reader :hilbert
def settings
def settings
Line 2,290: Line 2,292:
end
end


Turtle = Struct.new(:x, :y, :theta)

# Hilbert Class has access to Sketch methods eg :line, :width, :height
class Hilbert
class Hilbert
include Processing::Proxy
include Processing::Proxy


attr_accessor :grammar, :axiom, :draw_length, :production, :turtle
attr_reader :grammar, :axiom, :draw_length, :production, :turtle
DELTA = Math::PI / 2
DELTA = 90.radians
XPOS = 0 # placeholders for turtle
YPOS = 1
THETA = 2
def initialize
def initialize
@axiom = 'FL'
@axiom = 'FL'
Line 2,308: Line 2,310:
stroke 0, 255, 0
stroke 0, 255, 0
stroke_weight 2
stroke_weight 2
@turtle = Array.new(3) # using an array as turtle
@turtle = Turtle.new(width / 9, height / 9, 0)
turtle[XPOS] = width / 9
turtle[YPOS] = height / 9
turtle[THETA] = 0
end
end


Line 2,320: Line 2,319:
draw_line(turtle)
draw_line(turtle)
when '+'
when '+'
@turtle[THETA] += DELTA
turtle.theta += DELTA
when '-'
when '-'
@turtle[THETA] -= DELTA
turtle.theta -= DELTA
when 'L'
when 'L'
when 'R'
when 'R'
Line 2,331: Line 2,330:


def draw_line(turtle)
def draw_line(turtle)
x_temp = turtle[XPOS]
x_temp = turtle.x
y_temp = turtle[YPOS]
y_temp = turtle.y
@turtle[XPOS] += draw_length * Math.cos(turtle[THETA])
turtle.x += draw_length * Math.cos(turtle.theta)
@turtle[YPOS] += draw_length * Math.sin(turtle[THETA])
turtle.y += draw_length * Math.sin(turtle.theta)
line(x_temp, y_temp, turtle[XPOS], turtle[YPOS])
line(x_temp, y_temp, turtle.x, turtle.y)
end
end

##############################
# create grammar from axiom and
# rules (adjust scale)
##############################


def create_grammar(gen)
def create_grammar(gen)