Class: Oekaki::Turtle

Inherits:
Tool
  • Object
show all
Defined in:
lib/oekaki.rb

Instance Attribute Summary collapse

Attributes inherited from Tool

#height, #width, #window

Instance Method Summary collapse

Methods inherited from Tool

#arc, #clear, #get_pic, #get_window_size, #line, #lines, #load_pic, #point, #polygon, #rectangle, #save_pic, #show_pic, #star, #text, #timer_stop

Constructor Details

#initializeTurtle

Returns a new instance of Turtle.



196
197
198
199
200
201
202
# File 'lib/oekaki.rb', line 196

def initialize
  @pen = Tool.new
  @pen_po = Vector[0, 0]
  @dir = Vector[1, 0]
  @color_t = [65535, 65535, 65535]
  @width, @height = @pen.get_window_size
end

Instance Attribute Details

#dirObject

Returns the value of attribute dir.



203
204
205
# File 'lib/oekaki.rb', line 203

def dir
  @dir
end

#pen_poObject

Returns the value of attribute pen_po.



203
204
205
# File 'lib/oekaki.rb', line 203

def pen_po
  @pen_po
end

Instance Method Details

#back(length) ⇒ Object



224
225
226
# File 'lib/oekaki.rb', line 224

def back(length)
  forward(-length, false)
end

#circle(radius, fill = false) ⇒ Object



233
234
235
236
# File 'lib/oekaki.rb', line 233

def circle(radius, fill = false)
  @pen.color(*@color_t)
  @pen.circle(fill, @width / 2 + @pen_po[0], @height / 2 - @pen_po[1], radius)
end

#color(r, g, b) ⇒ Object



228
229
230
231
# File 'lib/oekaki.rb', line 228

def color(r, g, b)
  @color_t = [r, g, b]
  @pen.color(*@color_t)
end

#forward(length, draw = true) ⇒ Object



214
215
216
217
218
219
220
221
222
# File 'lib/oekaki.rb', line 214

def forward(length, draw = true)
  next_po = @pen_po + @dir * length
  if draw
    @pen.color(*@color_t)
    @pen.line(@width / 2 + next_po[0], @height / 2 - next_po[1],
       @width / 2 + @pen_po[0], @height / 2 - @pen_po[1])
  end
  @pen_po = next_po
end

#left(deg) ⇒ Object



205
206
207
208
# File 'lib/oekaki.rb', line 205

def left(deg)
  θ = PI * deg / 180
  @dir = Matrix[[cos(θ), -sin(θ)], [sin(θ), cos(θ)]] * @dir
end

#move(x, y) ⇒ Object



238
239
240
# File 'lib/oekaki.rb', line 238

def move(x, y)
  @pen_po = Vector[x, y]
end

#right(deg) ⇒ Object



210
211
212
# File 'lib/oekaki.rb', line 210

def right(deg)
  left(-deg)
end