Skip to content

Deprecating SCALING_FACTOR

Michael Schwarz edited this page Nov 3, 2015 · 4 revisions

In versions of Pyclipper after 0.9.3b0, SCALING_FACTOR has been deprecated and is ignored by the library. You will either have to downgrade to a version of the library which still supports SCALING_FACTOR or adapt your code to use the new API as described under Porting code to the new API.

Newer versions of Pyclipper no longer automatically convert between a representation of paths and polygons using floats to represent coordinates and allowing fractional coordinate values and the int-based representation used by Clipper. This makes it possible to directly use the int-based representation when no fractional coordinate values are needed and thus conversion is unnecessary. The conversion is undesirable if very large coordinate values are used as Python's float type only has 53 significant bits and very large values are affected by rounding, even if they have no fractional part. See this article in the Clipper documentation for more information

Porting code to the new API

Previously, Pyclipper was using the global variable pyclipper.SCALING_FACTOR to control automatic conversion of path and polygon coordinates and the arguments and return values of some functions and methods. This variable has been replaced with dedicated methods to transform between the different representations.

In the following example, SCALING_FACOR has been set to 1000, giving tree decimal digits of precision to represent coordinate values in Clipper operations:

import pyclipper

path = [[0, 0], [1, 0], [1 / 2, (3 / 4) ** (1 / 2)]] # A triangle.
clip = [[0, 1 / 3], [1, 1 / 3], [1, 2 / 3], [0, 1 / 3]] # A rectangle.

pyclipper.SCALING_FACTOR = 1000

pc = pyclipper.Pyclipper()
pc.AddPath(path, pyclipper.PT_SUBJECT)
pc.AddPath(clip, pyclipper.PT_CLIP)
res = pc.Execute(pyclipper.CT_INTERSECTION)

To use the new API, the calls to AddPath() and Execute() need to be changed to convert the values using scale_to_clipper() and scale_from_clipper():

SCALING_FACTOR = 1000

pc = Pyclipper()
pc.AddPath(scale_to_clipper(path, SCALING_FACTOR), PT_SUBJECT)
pc.AddPath(scale_to_clipper(clip, SCALING_FACTOR), PT_CLIP)
res = scale_from_clipper(pc.Execute(CT_INTERSECTION), SCALING_FACTOR)

For most applications, the default scaling factor of 2 ** 32 can be used, which allows the code to be simplified to this:

pc = Pyclipper()
pc.AddPath(scale_to_clipper(path), PT_SUBJECT)
pc.AddPath(scale_to_clipper(clip), PT_CLIP)
res = scale_from_clipper(pc.Execute(CT_INTERSECTION))
Clone this wiki locally