Re: KRect and KRectF proposal

Thiago Macieira <[email protected]> Tue, 07 Apr 2026 16:30:25 -0700
Newsgroups gmane.comp.kde.devel.general,gmane.comp.kde.devel.frameworks
Message-ID <27516696.1r3eYUQgxm@tjmaciei-mobl5>
On Tuesday, 7 April 2026 12:56:19 Pacific Daylight Time Vlad Zahorodnii wrote:
> > Speaking of negative, what happens if you have negative height and width?
> > Which edge is exclusive?
> 
> Such rectangles will be considered invalid. So `::contains()` will
> return false. You'll need to use either `::normalized()` or `::span()`
> to get a proper rectangle. That's another (intentional) change that I
> forgot to mention in the original message.

Ok, so it means it's not a good replacement for QRect because it removes a 
functionality, in addition to fixing the known issue.

> >> - `RectF::toRect()` rounds the coordinates of top, left, right, and
> >> bottom edges;
> > 
> > Away from each other, towards each other, or towards a particular
> > direction? In other words, how do the height() and width() get rounded?
> 
> The final coordinates will be as follows:
> 
> - left = std::round(rect.x())
> - right = std::round(rect.x() + rect.width())
> - top = std::round(rect.y())
> - bottom = std::round(rect.y() + rect.height())
> 
> or
> 
> - x = std::round(rect.x())
> - y = std::round(rect.y())
> - width = std::round(rect.x() + rect.width()) - std::round(rect.x())
> - height = std::round(rect.y() + rect.height()) - std::round(rect.y())

So you may end up with a zero-sized integer rectangle when the original FP one 
isn't. Consider rounding fixing the rounding directions and in opposite of each 
other.

> The main reasoning behind this is HiDPI or fractional scaling to be
> specific. We need a predictable rounding algorithm, which doesn't tend
> to introduce gaps, and that's universal for both apps and compositors.

Agreed, which is why it shouldn't be rounding to nearest.

> Although note that the rectangles are stored as (left, top, right,
> bottom) tuples rather than (x, y, width, height) tuples, so it's just
> four std::round() calls. The reasoning behind it is that the LTRB format
> is nicer for contains(), intersects(), Region, etc.

Consider:
top() = -0.4999
left() = -0.4999
bottom() = 0.4999
right() = 0.4999

width() and height() are 0.9998, which clearly rounds to 1.0. But all of the 
four coordinates round to 0.

> >> I would like to propose adding a new library, e.g. kprimitivetypes, that
> >> would host `KRect` and `KRectF`. There is a slight challenge with KF
> >> though. It will be nice if KConfig and maybe other tier 1 libraries are
> >> able use `KRect(F)`. In order to achieve that, the new library will need
> >> to live either outside of KF or perhaps there could be tier 0 in KF?
> > 
> > Bring this discussion to the Qt dev mailing list so we can discuss having
> > it in QtCore.
> 
> Okay, will do.

You'll need to offer an convincing reason why negative sizes aren't allowed.
-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
  Principal Engineer - Intel Data Center - Platform & Sys. Eng.
signature.asc (application/pgp-signature, 870 B)
-----BEGIN PGP SIGNATURE-----

iQJPBAABCAA5FiEECozPt7VWsJgFlTT/jR/izwvmghcFAmnVk5EbFIAAAAAABAAO
bWFudTIsMi41KzEuMTIsMiwyAAoJEI0f4s8L5oIXwUYP/3AayXjUguOpVur89TQg
4ipNu0qJ8d12BgV6mTIHt2Lwg3BCvFo5HN/3sgpn1CxV7F4OtyFtZyjCBzWQ42YV
8of833kAFnbvsy/4bqKzMFwYPZH7soGIyFuzY1wjM30S29d2duCr4pOH/VDC3q3W
h2CnmLdlwyqIJ8Zilc+/FSO60iF7XO47Xu4xmOZy49069VeSww6PBxMvYuj1U04H
DyqZJj/TZE4JMsn6YAGBZBAyvjf+gZ8K89Cp0uodpyDt0TfVu2SBMg3V6gi6AaR7
OQEsdFXhouuN2GaQbrf0dn+7rz3fttw2uP78klQjvg/QLourrtOk8TY1vRA7i9Hp
8KdpoCufmRv+d6bq+nm8bjBcsTpApHDORin1iWP8iNFVO4JGGh0mMGXA36Mz5EGo
ofmLkSd1FhPE2e0Xcxn7mATucGjmZJ7hH/naUmV2U+WyF9VQrG5xzQX19sb8QNnz
FuZpNMKv4cy8Cs0ze01QpIlU1okKO2LoXb7jOBGttdPPio1FKXNQ9aJy7x7lf+yT
FXm1k9y73S4Fc/R1NLTeUutTu8nQUYqV2vIrECFEkjk5VeI5Z/xnHQ7r3DTcuyOR
BGB2+sU2C057Ht7bAHBRzlbooUF/m7rWA7vZNBJVEREJZXZLkkElLDKi2Nb+vHgb
IWtVy4i6Eygju1HlvGZPy718
=0ey4
-----END PGP SIGNATURE-----