Clipper接受整数输入,但是我想传递浮点值而不损失精度。
整数和双精度值的Clipper结构。

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) {}
};


为什么不将double值作为输入。

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

最佳答案

Clipper仅使用integer point types


  IntPoint结构用于表示Clipper库中的所有顶点。特意选择了整数存储类型以保留数值稳定性。 (该库的早期版本使用浮点坐标,但是很明显,浮点不精确总是会导致偶尔的错误。)[src]


但是,您可以按需要的比例缩放输入坐标。

因此,而不是想要这个(不存在)

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


您可以按100缩放。

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


只需记住将输出坐标缩放0.01即可返回到坐标系。

09-06 17:21