Class: TkComponent::Turtle

Inherits:
Object
  • Object
show all
Defined in:
lib/tk_component/extras/turtle.rb

Constant Summary collapse

FULL_CIRCLE =
2 * Math::PI

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Turtle

Returns a new instance of Turtle.



11
12
13
14
15
16
17
18
19
20
# 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

Instance Attribute Details

#canvasObject

Returns the value of attribute canvas.



5
6
7
# File 'lib/tk_component/extras/turtle.rb', line 5

def canvas
  @canvas
end

#colorObject

Returns the value of attribute color.



8
9
10
# File 'lib/tk_component/extras/turtle.rb', line 8

def color
  @color
end

#current_xObject

Returns the value of attribute current_x.



6
7
8
# File 'lib/tk_component/extras/turtle.rb', line 6

def current_x
  @current_x
end

#current_yObject

Returns the value of attribute current_y.



7
8
9
# File 'lib/tk_component/extras/turtle.rb', line 7

def current_y
  @current_y
end

#widthObject

Returns the value of attribute width.



9
10
11
# File 'lib/tk_component/extras/turtle.rb', line 9

def width
  @width
end

Instance Method Details

#clearObject



22
23
24
# File 'lib/tk_component/extras/turtle.rb', line 22

def clear
  @canvas.delete('all')
end

#current_pointObject



42
43
44
# File 'lib/tk_component/extras/turtle.rb', line 42

def current_point
  [@current_x, @current_y]
end

#degreesObject Also known as: deg



85
86
87
# File 'lib/tk_component/extras/turtle.rb', line 85

def degrees
  @angle_unit = :degrees
end

#downObject



26
27
28
# File 'lib/tk_component/extras/turtle.rb', line 26

def down
  @turtle_down = true
end

#down?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/tk_component/extras/turtle.rb', line 34

def down?
  @turtle_down
end

#forward(length) ⇒ Object



51
52
53
54
55
56
57
58
59
# 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_centerObject



46
47
48
49
# 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_eastObject



73
74
75
# File 'lib/tk_component/extras/turtle.rb', line 73

def point_east
  @current_angle = 0.0
end

#point_northObject



69
70
71
# File 'lib/tk_component/extras/turtle.rb', line 69

def point_north
  @current_angle = FULL_CIRCLE / 4.0
end

#point_southObject



77
78
79
# File 'lib/tk_component/extras/turtle.rb', line 77

def point_south
  @current_angle = FULL_CIRCLE * 3.0 / 4.0
end

#point_westObject



81
82
83
# File 'lib/tk_component/extras/turtle.rb', line 81

def point_west
  @current_angle = FULL_CIRCLE / 2.0
end

#radiansObject Also known as: rad



90
91
92
# File 'lib/tk_component/extras/turtle.rb', line 90

def radians
  @angle_unit = :radians
end

#turn_left(angle) ⇒ Object



61
62
63
# 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) ⇒ Object



65
66
67
# File 'lib/tk_component/extras/turtle.rb', line 65

def turn_right(angle)
  @current_angle = (@current_angle - user_angle(angle)) % FULL_CIRCLE
end

#upObject



30
31
32
# File 'lib/tk_component/extras/turtle.rb', line 30

def up
  @turtle_down = false
end

#up?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/tk_component/extras/turtle.rb', line 38

def up?
  !down?
end

#user_angle(angle) ⇒ Object



95
96
97
98
# 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