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.
818 819 820 821 822 823 824 |
# File 'lib/pixelflow_canvas.rb', line 818 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.
826 827 828 |
# File 'lib/pixelflow_canvas.rb', line 826 def color @color end |
#pen_down ⇒ Object
Returns the value of attribute pen_down.
826 827 828 |
# File 'lib/pixelflow_canvas.rb', line 826 def pen_down @pen_down end |
#phi ⇒ Object
Returns the value of attribute phi.
826 827 828 |
# File 'lib/pixelflow_canvas.rb', line 826 def phi @phi end |
#x ⇒ Object
Returns the value of attribute x.
826 827 828 |
# File 'lib/pixelflow_canvas.rb', line 826 def x @x end |
#y ⇒ Object
Returns the value of attribute y.
826 827 828 |
# File 'lib/pixelflow_canvas.rb', line 826 def y @y end |
Instance Method Details
#forward(l) ⇒ Object
828 829 830 831 832 833 834 |
# File 'lib/pixelflow_canvas.rb', line 828 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
836 837 838 |
# File 'lib/pixelflow_canvas.rb', line 836 def set_color(r = 0, g = nil, b = nil) @canvas.set_color(r, g, b) end |
#turn_left(phi) ⇒ Object
840 841 842 |
# File 'lib/pixelflow_canvas.rb', line 840 def turn_left(phi) @phi -= phi end |
#turn_right(phi) ⇒ Object
844 845 846 |
# File 'lib/pixelflow_canvas.rb', line 844 def turn_right(phi) @phi += phi end |