0

I'd like to know if there is a java way to, given a polygon, draw another one at a given distance and with the same center.

I tried AffineTransform but don't really know how it Works.

Thank you.

Ramon
  • 1
  • 1
  • g2.translate(x,y); g2.draw(polygon) might be simpler than using a transform matrix, if you are only interested in translation. – John Powell Jun 07 '14 at 18:22
  • John, sorry, I wasn´t clear enough. The idea is to build a second polygon outside from another one, but using the same center and twice the size. Thank you. – Ramon Jun 08 '14 at 09:26
  • g2.scale(2,2); g2.draw(polygon); in that case. You use AffineTransform and a transform matrix when you want to combine translate, scale, shear, rotate and flip in one operation. – John Powell Jun 08 '14 at 09:52
  • John, this is my code ..........p.addPoint(xx0, yy0); p.addPoint(xx1, yy1); p.addPoint(xx2, yy2); g2d.drawPolygon(p); g2d.scale(2, 2); g2d.setColor(Color.red); g2d.drawPolygon(p);......... What it does is to paint 2 polygons, one twice the size of the other, but the small one is not inside the big one. Sorry about the code format, but I tried to format it and I couldn't – Ramon Jun 08 '14 at 18:10
  • I have posted an answer that I think will do what you want. – John Powell Jun 09 '14 at 14:56
  • Hi John, Just to let you know that your code did work very well. Thank you so much. – Ramon Jun 21 '14 at 17:17
  • I'm not sure why I deleted it. I undeleted it, should you wish to accept it. Glad it works for you. – John Powell Jun 21 '14 at 17:27

1 Answers1

0

You need to translate your polygon by half its centroid width and height. I have included the code that comes from http://paulbourke.net/geometry/polygonmesh/PolygonUtilities.java to calculate the centroid of a polygon.

public void drawPolygon(){
   Graphics2D g2 = bufferedImage.createGraphics();
   Polygon poly=new Polygon();
   poly.addPoint(100, 100);
   poly.addPoint(200, 100);
   poly.addPoint(200, 200);
   poly.addPoint(150, 250);
   poly.addPoint(100, 200);
   poly.addPoint(100, 100);

   g2.setColor(Color.blue);
   g2.fillPolygon(poly);        
   g2.setColor(Color.red);

   Point2D.Double []pts=new Point2D.Double[poly.npoints];

   for (int i=0;i<poly.npoints;i++){
       pts[i]=new Point2D.Double(poly.xpoints[i],poly.ypoints[i]);            
   }

   Point2D centroid=centerOfMass(pts);

   g2.translate(-centroid.getX(), -centroid.getY());
   g2.scale(2, 2);

   g2.drawPolygon(poly);        
}  

public static double area(Point2D[] polyPoints) {
   int i, j, n = polyPoints.length;
   double area = 0;

   for (i = 0; i < n; i++) {
      j = (i + 1) % n;
      area += polyPoints[i].getX() * polyPoints[j].getY();
      area -= polyPoints[j].getX() * polyPoints[i].getY();
   }
   area /= 2.0;
   return (area);
}

/**
 * Function to calculate the center of mass for a given polygon, according
 * to the algorithm defined at
 * http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/
 * 
 * @param polyPoints
 * array of points in the polygon
 * @return point that is the center of mass
 */

public static Point2D centerOfMass(Point2D[] polyPoints) {
  double cx = 0, cy = 0;
  double area = area(polyPoints);
  // could change this to Point2D.Float if you want to use less memory
  Point2D res = new Point2D.Double();
  int i, j, n = polyPoints.length;
  double factor = 0;
  for (i = 0; i < n; i++) {
      j = (i + 1) % n;
      factor = (polyPoints[i].getX() * polyPoints[j].getY()
            - polyPoints[j].getX() * polyPoints[i].getY());
      cx += (polyPoints[i].getX() + polyPoints[j].getX()) * factor;
      cy += (polyPoints[i].getY() + polyPoints[j].getY()) * factor;
  }
  area *= 6.0f;
  factor = 1 / area;
  cx *= factor;
  cy *= factor;
  res.setLocation(cx, cy);
  return res;
}

Another way of doing this, common in the GIS world, is to buffer a polygon. There is a library called Java Topology Suite that will provide this functionality, although it might be harder to figure out what the scale factor is.

There are some very interesting discussions about polygon growing in this post: An algorithm for inflating/deflating (offsetting, buffering) polygons

Community
  • 1
  • 1
John Powell
  • 12,253
  • 6
  • 59
  • 67