Class: TkComponent::Turtle
- Inherits:
-
Object
- Object
- TkComponent::Turtle
- Defined in:
- lib/tk_component/extras/turtle.rb
Constant Summary collapse
- FULL_CIRCLE =
2 * Math::PI
Instance Attribute Summary collapse
-
#canvas ⇒ Object
Returns the value of attribute canvas.
-
#color ⇒ Object
Returns the value of attribute color.
-
#current_x ⇒ Object
Returns the value of attribute current_x.
-
#current_y ⇒ Object
Returns the value of attribute current_y.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
- #clear ⇒ Object
- #current_point ⇒ Object
- #degrees ⇒ Object (also: #deg)
- #down ⇒ Object
- #down? ⇒ Boolean
- #forward(length) ⇒ Object
-
#initialize(options = {}) ⇒ Turtle
constructor
A new instance of Turtle.
- #move_to_center ⇒ Object
- #point_east ⇒ Object
- #point_north ⇒ Object
- #point_south ⇒ Object
- #point_west ⇒ Object
- #radians ⇒ Object (also: #rad)
- #turn_left(angle) ⇒ Object
- #turn_right(angle) ⇒ Object
- #up ⇒ Object
- #up? ⇒ Boolean
- #user_angle(angle) ⇒ Object
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( = {}) @canvas = [:canvas] @current_x = 0 @current_y = 0 @angle_unit = :radians @current_angle = FULL_CIRCLE / 4.0 @turtle_down = false @color = [:color] || 'black' @width = [:width] || 1 end |
Instance Attribute Details
#canvas ⇒ Object
Returns the value of attribute canvas.
5 6 7 |
# File 'lib/tk_component/extras/turtle.rb', line 5 def canvas @canvas end |
#color ⇒ Object
Returns the value of attribute color.
8 9 10 |
# File 'lib/tk_component/extras/turtle.rb', line 8 def color @color end |
#current_x ⇒ Object
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_y ⇒ Object
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 |
#width ⇒ Object
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
#clear ⇒ Object
22 23 24 |
# File 'lib/tk_component/extras/turtle.rb', line 22 def clear @canvas.delete('all') end |
#current_point ⇒ Object
42 43 44 |
# File 'lib/tk_component/extras/turtle.rb', line 42 def current_point [@current_x, @current_y] end |
#degrees ⇒ Object Also known as: deg
85 86 87 |
# File 'lib/tk_component/extras/turtle.rb', line 85 def degrees @angle_unit = :degrees end |
#down ⇒ Object
26 27 28 |
# File 'lib/tk_component/extras/turtle.rb', line 26 def down @turtle_down = true end |
#down? ⇒ 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_center ⇒ Object
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_east ⇒ Object
73 74 75 |
# File 'lib/tk_component/extras/turtle.rb', line 73 def point_east @current_angle = 0.0 end |
#point_north ⇒ Object
69 70 71 |
# File 'lib/tk_component/extras/turtle.rb', line 69 def point_north @current_angle = FULL_CIRCLE / 4.0 end |
#point_south ⇒ Object
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_west ⇒ Object
81 82 83 |
# File 'lib/tk_component/extras/turtle.rb', line 81 def point_west @current_angle = FULL_CIRCLE / 2.0 end |
#radians ⇒ Object 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 |
#up ⇒ Object
30 31 32 |
# File 'lib/tk_component/extras/turtle.rb', line 30 def up @turtle_down = false end |
#up? ⇒ 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 |