Class: Pixelflow::Turtle
- Inherits:
-
Object
- Object
- Pixelflow::Turtle
- Defined in:
- lib/pixelflow_canvas.rb
Instance Attribute Summary collapse
-
#color ⇒ Object
Returns the value of attribute color.
-
#pen_down ⇒ Object
Returns the value of attribute pen_down.
-
#phi ⇒ Object
Returns the value of attribute phi.
-
#x ⇒ Object
Returns the value of attribute x.
-
#y ⇒ Object
Returns the value of attribute y.
Instance Method Summary collapse
- #forward(l) ⇒ Object
-
#initialize(canvas) ⇒ Turtle
constructor
A new instance of Turtle.
- #set_color(r = 0, g = nil, b = nil) ⇒ Object
- #turn_left(phi) ⇒ Object
- #turn_right(phi) ⇒ Object
Constructor Details
#initialize(canvas) ⇒ Turtle
Returns a new instance of Turtle.
852 853 854 855 856 857 858 |
# File 'lib/pixelflow_canvas.rb', line 852 def initialize(canvas) @canvas = canvas @x = canvas.width / 2.0 @y = canvas.height / 2.0 @phi = 270.0 @pen_down = true end |
Instance Attribute Details
#color ⇒ Object
Returns the value of attribute color.
860 861 862 |
# File 'lib/pixelflow_canvas.rb', line 860 def color @color end |
#pen_down ⇒ Object
Returns the value of attribute pen_down.
860 861 862 |
# File 'lib/pixelflow_canvas.rb', line 860 def pen_down @pen_down end |
#phi ⇒ Object
Returns the value of attribute phi.
860 861 862 |
# File 'lib/pixelflow_canvas.rb', line 860 def phi @phi end |
#x ⇒ Object
Returns the value of attribute x.
860 861 862 |
# File 'lib/pixelflow_canvas.rb', line 860 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
860 861 862 |
# File 'lib/pixelflow_canvas.rb', line 860 def y @y end |
Instance Method Details
#forward(l) ⇒ Object
862 863 864 865 866 867 868 |
# File 'lib/pixelflow_canvas.rb', line 862 def forward(l) nx = @x + Math.cos(@phi * Math::PI / 180.0) * l ny = @y + Math.sin(@phi * Math::PI / 180.0) * l @canvas.draw_line(@x, @y, nx, ny) if @pen_down @x = nx @y = ny end |
#set_color(r = 0, g = nil, b = nil) ⇒ Object
870 871 872 |
# File 'lib/pixelflow_canvas.rb', line 870 def set_color(r = 0, g = nil, b = nil) @canvas.set_color(r, g, b) end |
#turn_left(phi) ⇒ Object
874 875 876 |
# File 'lib/pixelflow_canvas.rb', line 874 def turn_left(phi) @phi -= phi end |
#turn_right(phi) ⇒ Object
878 879 880 |
# File 'lib/pixelflow_canvas.rb', line 878 def turn_right(phi) @phi += phi end |