class TkComponent::Turtle

Constants

FULL_CIRCLE

Attributes

canvas[RW]
color[RW]
current_x[RW]
current_y[RW]
width[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/tk_component/extras/turtle.rb, line 11
def initialize(options = {})
  @canvas = options[:canvas]
  @current_x = 0
  @current_y = 0
  @angle_unit = :radians
  @current_angle = FULL_CIRCLE / 4.0
  @turtle_down = false
  @color = options[:color] || 'black'
  @width = options[:width] || 1
end

Public Instance Methods

clear() click to toggle source
# File lib/tk_component/extras/turtle.rb, line 22
def clear
  @canvas.delete('all')
end
current_point() click to toggle source
# File lib/tk_component/extras/turtle.rb, line 42
def current_point
  [@current_x, @current_y]
end
deg()
Alias for: degrees
degrees() click to toggle source
# File lib/tk_component/extras/turtle.rb, line 85
def degrees
  @angle_unit = :degrees
end
Also aliased as: deg
down() click to toggle source
# File lib/tk_component/extras/turtle.rb, line 26
def down
  @turtle_down = true
end
down?() click to toggle source
# File lib/tk_component/extras/turtle.rb, line 34
def down?
  @turtle_down
end
forward(length) click to toggle source
# File lib/tk_component/extras/turtle.rb, line 51
def forward(length)
  new_x = @current_x + length * Math.cos(@current_angle)
  new_y = @current_y - length * Math.sin(@current_angle)
  if down?
    TkcLine.new(@canvas, [current_point, [new_x, new_y]], fill: @color, width: @width)
  end
  @current_x = new_x
  @current_y = new_y
end
move_to_center() click to toggle source
# File lib/tk_component/extras/turtle.rb, line 46
def move_to_center
  @current_x = @canvas.winfo_width / 2
  @current_y = @canvas.winfo_height / 2
end
point_east() click to toggle source
# File lib/tk_component/extras/turtle.rb, line 73
def point_east
  @current_angle = 0.0
end
point_north() click to toggle source
# File lib/tk_component/extras/turtle.rb, line 69
def point_north
  @current_angle = FULL_CIRCLE / 4.0
end
point_south() click to toggle source
# File lib/tk_component/extras/turtle.rb, line 77
def point_south
  @current_angle = FULL_CIRCLE * 3.0 / 4.0
end
point_west() click to toggle source
# File lib/tk_component/extras/turtle.rb, line 81
def point_west
  @current_angle = FULL_CIRCLE / 2.0
end
rad()
Alias for: radians
radians() click to toggle source
# File lib/tk_component/extras/turtle.rb, line 90
def radians
  @angle_unit = :radians
end
Also aliased as: rad
turn_left(angle) click to toggle source
# File lib/tk_component/extras/turtle.rb, line 61
def turn_left(angle)
  @current_angle = (@current_angle + user_angle(angle)) % FULL_CIRCLE
end
turn_right(angle) click to toggle source
# File lib/tk_component/extras/turtle.rb, line 65
def turn_right(angle)
  @current_angle = (@current_angle - user_angle(angle)) % FULL_CIRCLE
end
up() click to toggle source
# File lib/tk_component/extras/turtle.rb, line 30
def up
  @turtle_down = false
end
up?() click to toggle source
# File lib/tk_component/extras/turtle.rb, line 38
def up?
  !down?
end
user_angle(angle) click to toggle source
# File lib/tk_component/extras/turtle.rb, line 95
def user_angle(angle)
  return angle if @angle_unit == :radians
  (angle % 360.0) * FULL_CIRCLE / 360.0
end