--Apple-Mail-1-448127278
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
charset O-8859-1;
formatðïwed
Begin forwarded message:
> From: "CñÅric Finance" <pticedric / gmail.com>
> Date: December 8, 2006 7:49:08 AM CST
> To: "submission / rubyquiz.com" <submission / rubyquiz.com>
> Subject: Please Forward: Ruby Quiz Submission
>
> Hello,
>
> Here is my solution for the turtle quizz.
> track is an array a broken lines in which the last point of
> each one is different from the first point of the next one.
>
> CñÅric
--Apple-Mail-1-448127278
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream;
x-unix-mode66;
name rtle.rb
Content-Disposition: attachment;
filename rtle.rb
# Created by Morton Goldberg on November 02, 2006.
# Modified on November 14, 2006
# turtle.rb
# An implementation of Turtle Procedure Notation (TPN) as described in
# H. Abelson & A. diSessa, "Turtle Geometry", MIT Press, 1981.
#
# Turtles navigate by traditional geographic coordinates: X-axis pointing
# east, Y-axis pointing north, and angles measured clockwise from the
# Y-axis (north) in degrees.
class Turtle
include Math # turtles understand math methods
DEG ath::PI / 180.0
attr_accessor :track
alias run instance_eval
def initialize
clear
end
attr_reader :xy, :heading
# Check if argument pt is a point
def __check_point(pt)
raise ArgumentError unless pt.kind_of? Array
raise ArgumentError unless pt[0].kind_of? Numeric
raise ArgumentError unless pt[1].kind_of? Numeric
end
def __update_track(new)
if pen_down?
if @track[-1] @xy
@track[-1] << new
else
@track << [@xy, new]
end
end
end
# Place the turtle at [x, y]. The turtle does not draw when it changes
# position.
def xy
oords)
__check_point coords
@xy oords
end
# Set the turtle's heading to <degrees>.
def heading
egrees)
raise ArgumentError unless degrees.kind_of? Numeric
@heading egrees % 360
end
# Raise the turtle's pen. If the pen is up, the turtle will not draw;
# i.e., it will cease to lay a track until a pen_down command is given.
def pen_up
@status up
end
# Lower the turtle's pen. If the pen is down, the turtle will draw;
# i.e., it will lay a track until a pen_up command is given.
def pen_down
@status down
end
# Is the pen up?
def pen_up?
@status :up
end
# Is the pen down?
def pen_down?
@status :down
end
# Places the turtle at the origin, facing north, with its pen up.
# The turtle does not draw when it goes home.
def home
@heading .0;
@xy 0.0, 0.0]
@status up
end
# Homes the turtle and empties out it's track.
def clear
home
@track ]
end
# Turn right through the angle <degrees>.
def right(degrees)
self.heading + egrees
end
# Turn left through the angle <degrees>.
def left(degrees)
self.heading - egrees
end
# Move forward by <steps> turtle steps.
def forward(steps)
raise ArgumentError unless steps.kind_of? Numeric
new @xy[0] + steps * Math.sin(@heading*DEG ) ,
@xy[1] + steps * Math.cos(@heading*DEG) ]
__update_track new
@xy ew
end
# Move backward by <steps> turtle steps.
def back(steps)
forward(-steps)
end
# Move to the given point.
def go(pt)
__check_point pt
__update_track pt
@xy t
end
# Turn to face the given point.
def toward(pt)
__check_point pt
self.heading ath.atan((pt[0]-@xy[0])/(pt[1]-@xy[1]))/DEG
end
# Return the distance between the turtle and the given point.
def distance(pt)
__check_point pt
Math.sqrt((pt[0]-@xy[0])**2+(pt[1]-@xy[1])**2)
end
# Traditional abbreviations for turtle commands.
alias fd forward
alias bk back
alias rt right
alias lt left
alias pu pen_up
alias pd pen_down
alias pu? pen_up?
alias pd? pen_down?
alias set_h headinglias set_xy xylias face toward
alias dist distance
end
--Apple-Mail-1-448127278
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset -ASCII;
formatwed
--Apple-Mail-1-448127278--