Evenly spaced polygon dilation [closed]

I would like to calculate a scaled polygon, such that its sides would be evenly distanced from the original polygon ones.

I’ve tried to use scaling, but it looks like I don’t get a perfect result: enter image description here

For rectangles, it works just fine, but with polygons, it seems that the sides are not spaced evenly. Furthermore, the angles don’t look equal.

  • Is this a normal situation with scaling?
  • Is there any better way to achieve my goal?
  • Maybe there’s something wrong with my calculations?

Here are the listings of code which I used:

    public static float[] getBypassPoints(Polygon polygon, float offsetX, float offsetY) {     Rectangle bbox = polygon.getBoundingRectangle();      float width = bbox.getWidth();     float height = bbox.getHeight();     float origScaleX = polygon.getScaleX();     float origScaleY = polygon.getScaleY();     float xScale = ((width + offsetX * 2) / width) * origScaleX;     float yScale = ((height + offsetY * 2) / height) * origScaleY;      setPolygonOrigin(polygon);     polygon.setScale(xScale, yScale);      float[] transformedVertices = polygon.getTransformedVertices();     float[] bypassPoints = transformedVertices.clone();      polygon.setScale(origScaleX, origScaleY);     return bypassPoints; }  public static void setPolygonOrigin(Polygon polygon) {     float[] vertices = polygon.getVertices();     int countOfCoords = vertices.length;      Vector2 centroid = polygonCentroid(             vertices, 0, countOfCoords, new Vector2(polygon.getX(), polygon.getY()));      polygon.setOrigin(centroid.x, centroid.y); } 

    public float[] getTransformedVertices () {     if (!dirty) return worldVertices;     dirty = false;      final float[] localVertices = this.localVertices;     if (worldVertices == null || worldVertices.length != localVertices.length) worldVertices = new float[localVertices.length];      final float[] worldVertices = this.worldVertices;     final float positionX = x;     final float positionY = y;     final float originX = this.originX;     final float originY = this.originY;     final float scaleX = this.scaleX;     final float scaleY = this.scaleY;     final boolean scale = scaleX != 1 || scaleY != 1;     final float rotation = this.rotation;     final float cos = MathUtils.cosDeg(rotation);     final float sin = MathUtils.sinDeg(rotation);      for (int i = 0, n = localVertices.length; i < n; i += 2) {         float x = localVertices[i] - originX;         float y = localVertices[i + 1] - originY;          // scale if needed         if (scale) {             x *= scaleX;             y *= scaleY;         }          // rotate if needed         if (rotation != 0) {             float oldX = x;             x = cos * x - sin * y;             y = sin * oldX + cos * y;         }          worldVertices[i] = positionX + x + originX;         worldVertices[i + 1] = positionY + y + originY;     }     return worldVertices; }