From: James Edward Gray II Date: 2006-12-08T23:00:10+09:00 Subject: Fwd: Please Forward: Ruby Quiz Submission --Apple-Mail-1-448127278 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=ISO-8859-1; format=flowed Begin forwarded message: > From: "C=E9dric Finance" > Date: December 8, 2006 7:49:08 AM CST > To: "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=E9dric --Apple-Mail-1-448127278 Content-Transfer-Encoding: 7bit Content-Type: application/octet-stream; x-unix-mode=0666; name=turtle.rb Content-Disposition: attachment; filename=turtle.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 = Math::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=(coords) __check_point coords @xy = coords end # Set the turtle's heading to . def heading=(degrees) raise ArgumentError unless degrees.kind_of? Numeric @heading = degrees % 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.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 . def right(degrees) self.heading += degrees end # Turn left through the angle . def left(degrees) self.heading -= degrees end # Move forward by 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 = new end # Move backward by turtle steps. def back(steps) forward(-steps) end # Move to the given point. def go(pt) __check_point pt __update_track pt @xy = pt end # Turn to face the given point. def toward(pt) __check_point pt self.heading = Math.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 heading= alias set_xy xy= alias face toward alias dist distance end --Apple-Mail-1-448127278 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; format=flowed --Apple-Mail-1-448127278--