Class | LogoTurtle |
In: |
LogoTurtle.rb
|
Parent: | Object |
LogoTurtle provides turtle graphics objects which manage the status of a drawing turtle and methods to control it. LogoTurtle objects do not actively communicate with other components but store their status information until it is requested. All messages sent to the LogoTurtle and all actions invoked through its methods are stored in a list. All actually drawn lines are also stored seperately.
Anything is allowed as ‘color’ attribute. The LogoTurtle can not determine if a string is a valid color description or not. It is up to the user interface to evaluate (or ignore) the color attribute according to its capabilities. The color attributes can be set directly. This should only be used by the user interface to correct an invalid entry with a valid default value. Normal color changes should be initiated through the LogoTurtle#setColor and LogoTurtle#setBackground methods to include the change in the message log.
Line | = | Struct.new("Line", :startx, :starty, :endx, :endy, :color) | Container for the attributes which define a line drawn by the LogoTurtle. | |
DEG | = | Math::PI / 180 | Converts degrees to radians |
background | [RW] | Current background color of the drawing area [String] |
color | [RW] | Current color of the drawing pen [String] |
direction | [R] | Current heading in degrees, relative to the starting heading [Float] |
lines | [R] | List of lines drawn by the turtle [Array] |
messages | [R] | List of status messages recorded by the turtle [Array] |
pen | [R] | Pen connected to the drawing area [Boolean] |
posx | [R] | Current position on the horizontal plane, relative to starting position [Float] |
posy | [R] | Current position on the vertical plane, relative to starting position [Float] |
visible | [R] | Turtle visible on drawing area [Boolean] |
Return an initialized LogoTurtle instance with specified starting attributes.
Parameters:
posx: The turtle’s position on the horizontal plane [Float]
posy: The turtle’s position on the vertical plane [Float]
direction: The turtle’s heading in degrees [Float]
color: The color of the drawing pen [String]
background: Background color of the drawing area [String]
Returns:
The initialized LogoTurtle [LogoTurtle]
# File LogoTurtle.rb, line 68 def initialize(posx=0.0, posy=0.0, direction=0.0, color="black", background="white") @posx, @posy, @direction = posx.to_f, posy.to_f, direction.to_f @color, @background, @pen, @visible = color.to_s, background.to_s, true, true @lines, @messages = [], [] @homeposx, @homeposy, @homedir, @homecol = @posx, @posy, @direction, @color end
Move the turtle for the given distance in the opposite direction than the one it’s currently heading.
Parameters:
distance: Distance to be travelled by the turtle [Float]
Returns:
The latest entry in the turtle’s message log [String]
# File LogoTurtle.rb, line 104 def backward(distance) forward(-distance.to_f) end
Delete all messages from the log but keep the current state of the turtle and all drawn lines
Returns:
The empty log [Array]
# File LogoTurtle.rb, line 306 def clearMessages @messages.clear end
Move the turtle for the given distance in the direction it’s currently heading.
Parameters:
distance: Distance to be travelled by the turtle [Float]
Returns:
The latest entry in the turtle’s message log [String]
# File LogoTurtle.rb, line 85 def forward(distance) newx = @posx + distance.to_f * Math.sin(@direction * DEG) newy = @posy + distance.to_f * Math.cos(@direction * DEG) @lines << Line.new(@posx, @posy, newx, newy, @color) if @pen @posx, @posy = newx, newy log "MOVE FORWARD #{distance}" end
call block for each line to be drawn, passing the Line object as a parameter
Returns:
A Container with the attributes of the line [Line]
# File LogoTurtle.rb, line 333 def getLines lines.each { |line| yield line } end
Make the turtle invisible. It can still perform all its actions and draw lines.
Returns:
The latest entry in the turtle’s message log [String]
# File LogoTurtle.rb, line 166 def hide @visible = false log "STATUS: HIDE TURTLE" end
Move the turtle directly from its current position to it’s home position. (the position the turtle had during initialization). This will draw a line (unless the pen is up) but not change the turtle’s heading.
Returns:
The latest entry in the turtle’s message log [String]
# File LogoTurtle.rb, line 246 def home move(0, 0) end
Turn the turtle for the given amount of degrees to the left.
Parameters:
degrees: Amount of degrees by which the turtle will turn [Float]
Returns:
The latest entry in the turtle’s message log [String]
# File LogoTurtle.rb, line 118 def left(degrees) @direction = normalize(@direction - degrees.to_f) log "TURN LEFT #{degrees % 360}" end
Write the given message into the turtle’s log. This method is used for status messages of movement methods, syntax errors from a parser and other outside information the turtle should, tell to an object requesting it’s actions.
Parameters:
message: Message to be appended to the log [String]
Returns:
The latest entry in the turtle’s message log [String]
# File LogoTurtle.rb, line 294 def log(message) @messages << message.to_s message.to_s end
Move the turtle directly from its current position to the given coordinates. This will draw a line (unless the pen is up) but not change the turtle’s heading.
Parameters:
x: The new position on the horizontal plane [Float]
y: The new position on the vertical plane [Float]
Returns:
The latest entry in the turtle’s message log [String]
# File LogoTurtle.rb, line 230 def move(x, y) newx = @homeposx + x.to_f newy = @homeposy + y.to_f @lines << Line.new(@posx, @posy, newx, newy, @color) if @pen @posx, @posy = newx, newy log "MOVE TO POSITION [#{x}, #{y}]" end
calculates a direction between 0 and 360 degrees which is equivalent to the given direction.
Parameters:
direction: Direction to be normalized [Float]
Returns:
An equivalent direction between 0° and 360° [Float]
# File LogoTurtle.rb, line 321 def normalize(direction) while direction.to_f < 0 do direction += 360 end direction % 360 end
Lower the pen onto the drawing area. Moving the turtle will draw lines.
Returns:
The latest entry in the turtle’s message log [String]
# File LogoTurtle.rb, line 155 def pendown @pen = true log "STATUS: PEN DOWN" end
Raise the pen from the drawing area. Moving the turtle will not draw lines.
Returns:
The latest entry in the turtle’s message log [String]
# File LogoTurtle.rb, line 144 def penup @pen = false log "STATUS: PEN UP" end
Put the turtle back to it’s starting state and delete all past actions from the log.
Returns:
The latest entry in the turtle’s message log [String]
# File LogoTurtle.rb, line 274 def reset @lines.clear @messages.clear @posx, @posy, @direction = @homeposx, @homeposy, @homedir @color, @pen, @visible = @homecol, true, true log "STATUS: RESET TURTLE" end
Turn the turtle for the given amount of degrees to the right.
Parameters:
degrees: Amount of degrees by which the turtle will turn [Float]
Returns:
The latest entry in the turtle’s message log [String]
# File LogoTurtle.rb, line 133 def right(degrees) @direction = normalize(@direction + degrees.to_f) log "TURN RIGHT #{degrees % 360}" end
Assign a new background color to the turtle. Validity of the given color can not be confirmed by the turtle, it depends on the capabilities of the interface which implements the turtle.
Parameters:
color: The new background color [String]
Returns:
The latest entry in the turtle’s message log [String]
# File LogoTurtle.rb, line 211 def setBackground(color) @background = color log "STATUS: BACKGROUND #{color}" end
Assign a new drawing color to the turtle. Validity of the given color can not be confirmed by the turtle, it depends on the capabilities of the interface which implements the turtle.
Parameters:
color: The new drawing color [String]
Returns:
The latest entry in the turtle’s message log [String]
# File LogoTurtle.rb, line 194 def setColor(color) @color = color log "STATUS: COLOR #{color}" end
Make the turtle visible.
Returns:
The latest entry in the turtle’s message log [String]
# File LogoTurtle.rb, line 177 def show @visible = true log "STATUS: SHOW TURTLE" end
returns an array representation of the LogoTurtle instance
Returns:
An array containing all attributes of the LogoTurtle’s current state [Array]
# File LogoTurtle.rb, line 343 def to_a return [@posx, @posy, @direction, @color, @background, @pen, @visible, @lines, @messages] end
returns a string representation of the LogoTurtle
Returns:
A semicolon seperated string containing all attributes of the LogoTurtle’s current state [String]
# File LogoTurtle.rb, line 355 def to_s return [@posx, @posy, @direction, @color, @background, @pen, @visible, @lines.join(", "), @messages.join(", ")].join(";") end
Turn the turtle into the given absolute direction. 0° (=360°) represents the direction during initialization. Positive numbers represent clockwise rotation.
Parameters:
direction: Direction to which the turtle will turn [Float]
Returns:
The latest entry in the turtle’s message log [String]
# File LogoTurtle.rb, line 262 def turn(direction) @direction = normalize(@homedir + direction.to_f) log "TURN TO #{@direction}" end