Class: Ray::Turtle
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
-
#angle_unit ⇒ Object
The unit used for angle.
-
#color ⇒ Object
The color the turtle uses for drawing.
-
#pen_width ⇒ Object
Returns the value of attribute pen_width.
-
#pos ⇒ Object
Returns the value of attribute pos.
-
#target ⇒ Ray::Target
readonly
The target the turtle is drawing on.
Instance Method Summary collapse
-
#angle ⇒ Float
The angle, either in radians or in degrees, depending on angle_unit.
-
#angle=(val) ⇒ Object
Sets the angle.
-
#backward(dist) ⇒ Object
Same as forward(-dist).
-
#center ⇒ Object
Makes the turtle go to the center.
-
#clear ⇒ Object
Clears the content of the image and call reset.
-
#dist(point) ⇒ Object
Returns the distance between the turtle and a point.
-
#dist_square(point) ⇒ Object
Returns the square of the distance between the turtle and a point.
-
#drawing? ⇒ Boolean
(also: #pen_down?)
Whether the turtle is drawing.
-
#forward(dist) ⇒ Object
Moves the turtle in the direction it’s facing.
-
#go_to(point) ⇒ Object
Makes the turtle move to (x, y).
-
#initialize(target) ⇒ Turtle
constructor
A new instance of Turtle.
-
#left(angle) ⇒ Object
Turns the turtle counter-clockwise.
-
#pen_down ⇒ Object
Starts drawing.
-
#pen_up ⇒ Object
Stops drawing.
-
#pen_up? ⇒ Boolean
The opposite of #pen_down?.
-
#reset ⇒ Object
Resets the turtle’s position, angle, and color but not the content of the image.
-
#right(angle) ⇒ Object
Turns the turtle clockwise.
- #x ⇒ Object
- #x=(val) ⇒ Object
- #y ⇒ Object
- #y=(val) ⇒ Object
Constructor Details
#initialize(target) ⇒ Turtle
Returns a new instance of 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_unit ⇒ Object
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 |
#color ⇒ Object
The color the turtle uses for drawing.
190 191 192 |
# File 'lib/ray/turtle.rb', line 190 def color @color end |
#pen_width ⇒ Object
Returns the value of attribute pen_width.
193 194 195 |
# File 'lib/ray/turtle.rb', line 193 def pen_width @pen_width end |
#pos ⇒ Object
Returns the value of attribute pos.
161 162 163 |
# File 'lib/ray/turtle.rb', line 161 def pos @pos end |
#target ⇒ Ray::Target (readonly)
Returns The target the turtle is drawing on.
135 136 137 |
# File 'lib/ray/turtle.rb', line 135 def target @target end |
Instance Method Details
#angle ⇒ Float
Returns The angle, either in radians or in degrees, depending on angle_unit.
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)
66 67 68 |
# File 'lib/ray/turtle.rb', line 66 def backward(dist) forward(-dist) end |
#center ⇒ Object
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 |
#clear ⇒ Object
Clears the content of the image and call reset.
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.
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.
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?
Returns Whether the turtle is drawing.
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.
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.
74 75 76 |
# File 'lib/ray/turtle.rb', line 74 def left(angle) self.angle += angle end |
#pen_down ⇒ Object
Starts drawing
36 37 38 |
# File 'lib/ray/turtle.rb', line 36 def pen_down @drawing = true end |
#pen_up ⇒ Object
Stops drawing
31 32 33 |
# File 'lib/ray/turtle.rb', line 31 def pen_up @drawing = false end |
#pen_up? ⇒ Boolean
Returns The opposite of #pen_down?.
48 49 50 |
# File 'lib/ray/turtle.rb', line 48 def pen_up? !@drawing end |
#reset ⇒ Object
Resets the turtle’s position, angle, and color but not the content of the image. Also sets the pen down.
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.
82 83 84 |
# File 'lib/ray/turtle.rb', line 82 def right(angle) self.angle -= angle end |
#x ⇒ Object
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 |
#y ⇒ Object
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 |