c++ - Converting polygon coordinates from Double to Long for use with Clipper library -
i have 2 polygons vertices stored double coordinates. i'd find intersecting area of these polygons, i'm looking @ clipper library (c++ version). problem is, clipper works integer math (it uses long type).
is there way can safely transform both polygons same scale factor, convert coordinates longs, perform intersection algorithm clipper, , scale resulting intersection polygon down same factor, , convert double without loss of precision?
i can't quite head around how that.
you can use simple multiplier convert between two:
/* using power-of-two because representable , makes scaling operation (not rounding!) lossless. value 1024 preserves 3 decimal digits. */ double const scale = 1024.0; // representable range double const min_value = std::numeric_limits<long>::min() / scale; double const max_value = std::numeric_limits<long>::max() / scale; long to_long(double v) { if(v < 0) { if(v < min_value) throw out_of_range(); return static_cast<long>(v * scale - 0.5); } else { if(v > max_value) throw out_of_range(); return static_cast<long>(v * scale + 0.5); } }
note larger make scale, higher precision be, lowers range. effectively, converts floating-point number fixed-point number.
lastly, should able locate code compute intersections between line segments using floating-point math easily, wonder why want use clipper.
Comments
Post a Comment