# Compute the bounding box of a Logo program # # See Part 3 module bound import visitor import turtle redef class Logo # Compute the bounding box of the program fun bounding_box: BoundBox do var v = new BoundVisitor v.visit(self) return v.bounding_box end end # An Axis-aligned minimum bounding box that grows to enclose all the given points. class BoundBox # Current minimum x coordinate var xmin: Float = inf # Current minimum y coordinate var ymin: Float = inf # Current maximum x coordinate var xmax: Float = -inf # Current maximum y coordinate var ymax: Float = -inf redef fun to_s do return "({xmin},{ymin})--({xmax},{ymax})" # Update the current min&max coordinate to include the given point. fun update(x,y: Float) do if x > xmax then xmax = x if x < xmin then xmin = x if y > ymax then ymax = y if y < ymin then ymin = y end end # Visitor that interprets a Logo program and update the associated bounding box class BoundVisitor super Visitor # The moving turtle var turtle = new Turtle # The growing bounding box var bounding_box = new BoundBox # TODO Finish the class end print logo.fd(10).main.bounding_box print logo_star.bounding_box print logo_home.bounding_box