Question
Fill onGroup method.

package edu.luc.etl.cs313.android.shapes.model;

import java.util.List;

/**
 * A shape visitor for calculating the bounding box, that is, the smallest
 * rectangle containing the shape. The resulting bounding box is returned as a
 * rectangle at a specific location.
 */
public class BoundingBox implements Visitor<Location> {

   // TODO entirely your job (except onCircle)

   @Override
   public Location onCircle(final Circle c) {
      final int radius = c.getRadius();
      return new Location(-radius, -radius, new Rectangle(2 * radius, 2 * radius));
   }

   @Override
   public Location onFill(final Fill f) {
      return f.getShape().accept(this);
   }

   @Override
   public Location onGroup(final Group g) {
      // Fill this method
   }

   @Override
   public Location onLocation(final Location l) {
      Location location = l.shape.accept(this);
      return new Location(l.x+location.x, l.y+location.y, location.shape);
   }

   @Override
   public Location onRectangle(final Rectangle r) {
      return new Location(0, 0, new Rectangle(r.getWidth(), r.getHeight()));
   }

   @Override
   public Location onStroke(final Stroke c) {
      return c.getShape().accept(this);
   }

   @Override
   public Location onOutline(final Outline o) {
      return o.getShape().accept(this);
   }

   @Override
   public Location onPolygon(final Polygon s) {
      // Fill this method
   }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1
package edu.luc.etl.cs313.android.shapes.model;

import java.util.List;

/**
 * A shape visitor for calculating the bounding box, that is, the smallest
 * rectangle containing the shape. The resulting bounding box is returned as a
 * rectangle at a specific location.
 */
public class BoundingBox implements Visitor<Location> {

   // TODO entirely your job (except onCircle)

   @Override
   public Location onCircle(final Circle c) {
      final int radius = c.getRadius();
      return new Location(-radius, -radius, new Rectangle(2 * radius, 2 * radius));
   }

   @Override
   public Location onFill(final Fill f) {
      return f.getShape().accept(this);
   }

   @Override
   public Location onGroup(final Group g) {
      int xMin=0, xMax=0, yMin=0, yMax=0;
      int count = 0;
      Location location;
      Rectangle rectangle;
      for(Shape shape : g.shapes) {
         location = shape.accept(this);
         rectangle = (Rectangle) location.shape;
         if(count == 0) {
            xMin = location.x;
            xMax = location.x + rectangle.width;
            yMin = location.y;
            yMax = location.y + rectangle.height;
         } else {
            if(location.x < xMin) {
               xMin = location.x;
            }
            if(location.y < yMin) {
               yMin = location.y;
            }
            if(location.x + rectangle.width > xMax) {
               xMax = location.x + rectangle.width;
            }
            if(location.y + rectangle.height > yMax) {
               yMax = location.y + rectangle.height;
            }
         }
         count++;
      }
      return new Location(xMin, yMin, new Rectangle(xMax-xMin, yMax-yMin));
   }

   @Override
   public Location onLocation(final Location l) {
      Location location = l.shape.accept(this);
      return new Location(l.x+location.x, l.y+location.y, location.shape);
   }

   @Override
   public Location onRectangle(final Rectangle r) {
      return new Location(0, 0, new Rectangle(r.getWidth(), r.getHeight()));
   }

   @Override
   public Location onStroke(final Stroke c) {
      return c.getShape().accept(this);
   }

   @Override
   public Location onOutline(final Outline o) {
      return o.getShape().accept(this);
   }

   @Override
   public Location onPolygon(final Polygon s) {
      int xMin=0, xMax=0, yMin=0, yMax=0;
      int count = 0;
      Point point;
      for(Shape shape : s.shapes) {
         point = (Point) shape;
         if(count == 0) {
            xMin = point.x;
            xMax = point.x;
            yMin = point.y;
            yMax = point.y;
         } else {
            if(point.x < xMin) {
               xMin = point.x;
            }
            if(point.y < yMin) {
               yMin = point.y;
            }
            if(point.x > xMax) {
               xMax = point.x;
            }
            if(point.y > yMax) {
               yMax = point.y;
            }
         }
         count++;
      }
      return new Location(xMin, yMin, new Rectangle(xMax-xMin, yMax-yMin));
   }
}
Add a comment
Know the answer?
Add Answer to:
package edu.luc.etl.cs313.android.shapes.model; import java.util.List; /** * A shape visitor for calculating the bounding box, that is,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT