c++clipperlib

Is that possible to give floating values to the clipper Paths


Clipper takes integer input but I want to pass floating values without losing the precision. Clipper struct for Integer and double value.

struct IntPoint {
  cInt X;
  cInt Y;
#ifdef use_xyz
  cInt Z;
  IntPoint(cInt x = 0, cInt y = 0, cInt z = 0): X(x), Y(y), Z(z) {};
#else
  IntPoint(cInt x = 0, cInt y = 0): X(x), Y(y) {};
#endif

  friend inline bool operator== (const IntPoint& a, const IntPoint& b)
  {
    return a.X == b.X && a.Y == b.Y;
  }
  friend inline bool operator!= (const IntPoint& a, const IntPoint& b)
  {
    return a.X != b.X  || a.Y != b.Y; 
  }
};

struct DoublePoint
{
  double X;
  double Y;
  DoublePoint(double x = 0, double y = 0) : X(x), Y(y) {}
  DoublePoint(IntPoint ip) : X((double)ip.X), Y((double)ip.Y) {}
};

why it is not taking the double value as an input.

Paths Polygon(2);
Polygon[0] << IntPoint(10, 10) << IntPoint(60, 10) << IntPoint(30, 100) ;
Polygon[1] << IntPoint(20, 20) << IntPoint(50, 20) << IntPoint(30, 80)  ; //it Works



Paths Line(1);
    line[0] << DoublePoint(40.2, 10.2) << DoublePoint(56.5, 85.45); //Not works

Solution

  • Clipper only uses integer point types.

    The IntPoint structure is used to represent all vertices in the Clipper Library. An integer storage type has been deliberately chosen to preserve numerical robustness. (Early versions of the library used floating point coordinates, but it became apparent that floating point imprecision would always cause occasional errors.) [src]

    You can however scale your input coordinates by a desirable factor.

    So instead of wanting this, (which doesn't exist)

    line[0] << DoublePoint(40.2, 10.2) << DoublePoint(56.5, 85.45); //Not works
    

    You can scale by 100.

    line[0] << IntPoint(4020, 1020) << IntPoint(5650, 8545); //works
    

    Just remember to scale output coordinates by 0.01 to get back to your coordinate system.