Skip to content

Bug: Inflate produces very small corner in offset #1105

Description

@Ayamur0

When inflating the path in this example, it creates a very small edge in the offset.

Image Image

This small error causes issues when using ZCallbacks because the edges we get in the ZCallback do not make sense, since this corner should not be there, producing Z values that don't make sense.

Something similar and more obvious happens when using JoinType.Bevel (instead of Round like in the code below). Here a weird jagged edge appears on the right:

Image

Here is a small code sample with minimal points to reproduce this error:

public static void Main()
{
  var path = new PathsD
  {
      new PathD
        {
            new PointD(-13.027999877929688, 9.385499954223633),
            new PointD(-13.038999557495117, 9.331500053405762),
            new PointD(-13.041000366210938, 9.319499969482422),
            new PointD(-15.026000022888184, 9.319499969482422),
            new PointD(-15.024999618530273, 9.327500343322754),
        },
  };

  double scale = Math.Pow(10, 6);
  Paths64 paths64 = Clipper.ScalePaths64(path, scale);
  ClipperOffset clipperOffset = new() { };
  clipperOffset.AddPaths(paths64, JoinType.Round, EndType.Polygon);
  Paths64 solution = new();
  clipperOffset.Execute(3 * scale, solution);

  // Bug: There is a 90° small edge in the resulting offset polygon
  // Detect a roughly 90° edge in the offset polygon
  foreach (var p in solution)
  {
    for (int i = 0; i < p.Count; i++)
    {
      var prev = p[(i - 1 + p.Count) % p.Count];
      var curr = p[i];
      var next = p[(i + 1) % p.Count];

      long dx1 = curr.X - prev.X;
      long dy1 = curr.Y - prev.Y;

      long dx2 = next.X - curr.X;
      long dy2 = next.Y - curr.Y;

      double angle1 = Math.Atan2(dy1, dx1);
      double angle2 = Math.Atan2(dy2, dx2);

      double diff = Math.Abs((angle2 - angle1) * 180.0 / Math.PI);
      if (diff > 180)
        diff = 360 - diff;

      if (Math.Abs(diff - 90) < 5)
      {
        Console.WriteLine($"90° corner at ({curr.X}, {curr.Y})");
      }
    }
  }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions