Class: Ray::Turtle

Inherits:
Object
  • Object
show all
Includes:
Math
Defined in:
lib/ray/turtle.rb

Overview

Implementation of turtle graphics.

A turtle keeps the following caracteristics:

  • An image which it is manipulating.

  • Its position (x, y), relative to the image. Set to (0, 0) by default.

  • An angle, in radians or in degrees (using degrees by default). Set to 0 by default, i.e. heading East.

  • Whether the turtle should draw when moved (defaults to true)

  • The color in which lines will be drawn. Defaults to white.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Turtle



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ray/turtle.rb', line 16

def initialize(target)
  @target  = target.is_a?(Image) ? Ray::ImageTarget.new(target) : target
  @drawing = true

  @pos = Ray::Vector2[0, 0]
  @angle = 0 # in rad

  @angle_unit = :deg

  @pen_width = 1

  @color = Ray::Color.white
end

Instance Attribute Details

#angle_unitObject

The unit used for angle. Either :deg (degrees) or :rad (radians)



165
166
167
# File 'lib/ray/turtle.rb', line 165

def angle_unit
  @angle_unit
end

#colorObject

The color the turtle uses for drawing.



190
191
192
# File 'lib/ray/turtle.rb', line 190

def color
  @color
end

#pen_widthObject

Returns the value of attribute pen_width.



193
194
195
# File 'lib/ray/turtle.rb', line 193

def pen_width
  @pen_width
end

#posObject

Returns the value of attribute pos.



161
162
163
# File 'lib/ray/turtle.rb', line 161

def pos
  @pos
end

#targetRay::Target (readonly)



135
136
137
# File 'lib/ray/turtle.rb', line 135

def target
  @target
end

Instance Method Details

#angleFloat



178
179
180
# File 'lib/ray/turtle.rb', line 178

def angle
  @angle_unit == :deg ? rad_to_deg(@angle) : @angle
end

#angle=(val) ⇒ Object

Sets the angle.



185
186
187
# File 'lib/ray/turtle.rb', line 185

def angle=(val)
  @angle = @angle_unit == :deg ? deg_to_rad(val) : val
end

#backward(dist) ⇒ Object

Same as forward(-dist)

See Also:



66
67
68
# File 'lib/ray/turtle.rb', line 66

def backward(dist)
  forward(-dist)
end

#centerObject

Makes the turtle go to the center. Draws if the pen is down.



96
97
98
# File 'lib/ray/turtle.rb', line 96

def center
  go_to @target.clip.center
end

#clearObject

Clears the content of the image and call reset.

See Also:



114
115
116
117
# File 'lib/ray/turtle.rb', line 114

def clear
  @target.clear Ray::Color.none
  reset
end

#dist(point) ⇒ Object

Returns the distance between the turtle and a point.

See Also:



121
122
123
# File 'lib/ray/turtle.rb', line 121

def dist(point)
  sqrt dist_square(point)
end

#dist_square(point) ⇒ Object

Returns the square of the distance between the turtle and a point. Since this doesn’t require to call sqrt, it is faster to use this when possible.

See Also:



130
131
132
# File 'lib/ray/turtle.rb', line 130

def dist_square(point)
  (pos - point.to_vector2).length
end

#drawing?Boolean Also known as: pen_down?



41
42
43
# File 'lib/ray/turtle.rb', line 41

def drawing?
  @drawing
end

#forward(dist) ⇒ Object

Moves the turtle in the direction it’s facing. Draws a line if the pen is down.

See Also:



57
58
59
60
61
62
# File 'lib/ray/turtle.rb', line 57

def forward(dist)
  x = pos.x + dist * cos(@angle)
  y = pos.y - dist * sin(@angle) # The y-axis is reversed

  go_to [x, y]
end

#go_to(point) ⇒ Object

Makes the turtle move to (x, y). Draws if the pen is down.



87
88
89
90
91
92
93
# File 'lib/ray/turtle.rb', line 87

def go_to(point)
  if pen_down?
    @target.draw Ray::Polygon.line(pos, point, @pen_width, @color)
  end

  self.pos = point
end

#left(angle) ⇒ Object

Turns the turtle counter-clockwise.

See Also:



74
75
76
# File 'lib/ray/turtle.rb', line 74

def left(angle)
  self.angle += angle
end

#pen_downObject

Starts drawing



36
37
38
# File 'lib/ray/turtle.rb', line 36

def pen_down
  @drawing = true
end

#pen_upObject

Stops drawing



31
32
33
# File 'lib/ray/turtle.rb', line 31

def pen_up
  @drawing = false
end

#pen_up?Boolean



48
49
50
# File 'lib/ray/turtle.rb', line 48

def pen_up?
  !@drawing
end

#resetObject

Resets the turtle’s position, angle, and color but not the content of the image. Also sets the pen down.

See Also:



103
104
105
106
107
108
109
110
# File 'lib/ray/turtle.rb', line 103

def reset
  self.angle     = 0
  self.pos       = [0, 0]
  self.color     = Ray::Color.white
  self.pen_width = 1

  pen_down
end

#right(angle) ⇒ Object

Turns the turtle clockwise.

See Also:



82
83
84
# File 'lib/ray/turtle.rb', line 82

def right(angle)
  self.angle -= angle
end

#xObject



137
138
139
# File 'lib/ray/turtle.rb', line 137

def x
  pos.x
end

#x=(val) ⇒ Object



145
146
147
# File 'lib/ray/turtle.rb', line 145

def x=(val)
  self.pos = [val, y]
end

#yObject



141
142
143
# File 'lib/ray/turtle.rb', line 141

def y
  pos.y
end

#y=(val) ⇒ Object



149
150
151
# File 'lib/ray/turtle.rb', line 149

def y=(val)
  self.pos = [x, val]
end