# Moving a turtle with a Logo program # # See Part 2. module turtle import logo # A oriented cursor on a plane class Turtle # x coordinate var x = 0.0 is writable # y coordinate var y = 0.0 is writable # angle in degree var angle = 0.0 is writable redef fun to_s do return "({x},{y};{angle})" # Move the turtle forward fun fd(distance: Int) do var angle_rad = angle * pi / 180.0 x += distance.to_f * angle_rad.cos y += distance.to_f * angle_rad.sin end # Turn the turtle right fun rt(angle: Int) do self.angle += angle.to_f while self.angle >= 360.0 do self.angle -= 360.0 while self.angle < 0.0 do self.angle += 360.0 end end redef class Logo # Apply the instruction on the turtle fun apply(turtle: Turtle) is abstract end # TODO var t t = new Turtle logo.fd(50).main.apply(t) print t t = new Turtle logo.fd(10).rt(90).fd(20).main.apply(t) print t t = new Turtle logo.repeat(6).fd(5).repeat(5).fd(10).fini.rt(90).main.apply(t) print t