[arrayTools] add quantizeRect

This commit is contained in:
Cosimo Lupo 2023-03-08 19:03:50 +00:00
parent 1a202ab1be
commit 92fbac8a64
No known key found for this signature in database
GPG Key ID: DF65A8A5A119C9A8

View File

@ -282,6 +282,27 @@ def intRect(rect):
return (xMin, yMin, xMax, yMax)
def quantizeRect(rect, factor=1):
"""
>>> bounds = (72.3, -218.4, 1201.3, 919.1)
>>> quantizeRect(bounds)
(72, -219, 1202, 920)
>>> quantizeRect(bounds, factor=10)
(70, -220, 1210, 920)
>>> quantizeRect(bounds, factor=100)
(0, -300, 1300, 1000)
"""
if factor < 1:
raise ValueError(f"Expected quantization factor >= 1, found: {factor!r}")
xMin, yMin, xMax, yMax = normRect(rect)
return (
int(math.floor(xMin / factor) * factor),
int(math.floor(yMin / factor) * factor),
int(math.ceil(xMax / factor) * factor),
int(math.ceil(yMax / factor) * factor),
)
class Vector(_Vector):
def __init__(self, *args, **kwargs):
warnings.warn(