Jump to content

Hilbert curve: Difference between revisions

No edit summary
Line 2,251:
Implemented as a Lindenmayer System, depends on JRuby or JRubyComplete
<lang Ruby>
# frozen_string_literal: true
 
attr_reader :hilbert
def settings
Line 2,290 ⟶ 2,292:
end
 
Turtle = Struct.new(:x, :y, :theta)
 
# Hilbert Class has access to Sketch methods eg :line, :width, :height
class Hilbert
include Processing::Proxy
 
attr_accessorattr_reader :grammar, :axiom, :draw_length, :production, :turtle
DELTA = Math::PI / 290.radians
XPOS = 0 # placeholders for turtle
YPOS = 1
THETA = 2
def initialize
@axiom = 'FL'
Line 2,308 ⟶ 2,310:
stroke 0, 255, 0
stroke_weight 2
@turtle = ArrayTurtle.new(3)width #/ using9, anheight array/ as9, turtle0)
turtle[XPOS] = width / 9
turtle[YPOS] = height / 9
turtle[THETA] = 0
end
 
Line 2,320 ⟶ 2,319:
draw_line(turtle)
when '+'
@turtle[THETA].theta += DELTA
when '-'
@turtle[THETA].theta -= DELTA
when 'L'
when 'R'
Line 2,331 ⟶ 2,330:
 
def draw_line(turtle)
x_temp = turtle[XPOS].x
y_temp = turtle[YPOS].y
@turtle[XPOS].x += draw_length * Math.cos(turtle[THETA].theta)
@turtle[YPOS].y += draw_length * Math.sin(turtle[THETA].theta)
line(x_temp, y_temp, turtle[XPOS].x, turtle[YPOS].y)
end
 
##############################
# create grammar from axiom and
# rules (adjust scale)
##############################
 
def create_grammar(gen)
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.