PDA

View Full Version : Why doesn't QPointF have a norm function?



Cruz
22nd September 2012, 23:24
I find it so annoying that when working with QPointF objects I always have to type out the Euklidean norm or transform it to some other object or to find a whatever workaround for something so simple and obvious. It has a (in my opinion mostly useless) manhattan norm. Why can't it just have a norm() function as well?

wysota
23rd September 2012, 02:09
Can you explain what you mean by "norm" in this context? QPointF is supposed to be a pair of coordinates, nothing more, nothing less.

ChrisW67
23rd September 2012, 03:11
Cruz means the Euclidean norm (http://en.wikipedia.org/wiki/Norm_%28mathematics%29#Euclidean_norm), aka length, of the the vector... if QPointF represented a vector. QVector2D, which as its name suggests represents a 2d vector, has a length() function that returns what Cruz is expecting (at a cost), and other functions supporting the mathematical notion of a vector.

The "mostly useless" manhattan length is used as a fast, approximate measure of distance between points for UI purposes like the one described in the doc for QPoint::manhattanLength(). Such operations are as common as muck in any GUI.

Cruz
23rd September 2012, 15:00
I don't know about "any gui". I have written plenty and never used the manhattan distance before. I do, however, frequently feel the need to calculate the distance between the mouse pointer and something else and I also have the extra nanosecond to spare to evaluate a square root to get the real thing instead of an approximation. I just don't understand why somebody would bother to include a method to calculate the manhattan distance and "forget" about the much more common euklidean norm. If a square root is too slow for you for some obscure reason, you can still use the manhattan length, but why make life harder for every day QPointF users who just want to evaluate a natural, circular shaped distance and not provide a plain vanilla norm()?

wysota
23rd September 2012, 17:35
I don't know about "any gui". I have written plenty and never used the manhattan distance before. I do, however, frequently feel the need to calculate the distance between the mouse pointer and something else and I also have the extra nanosecond to spare to evaluate a square root to get the real thing instead of an approximation. I just don't understand why somebody would bother to include a method to calculate the manhattan distance and "forget" about the much more common euklidean norm. If a square root is too slow for you for some obscure reason, you can still use the manhattan length, but why make life harder for every day QPointF users who just want to evaluate a natural, circular shaped distance and not provide a plain vanilla norm()?

Manhattan distance is more commonly used than you think. Usually it is a good enough approximation of 2D (or actually N-D) length, especially for small distances. It is used by Qt (probably among other things) to evaluate whether a mouse movement is big enough that it should start a drag operation and probably this is why it was included in QPoint API. Qt doesn't have any use for Euclidean distance with QPoint (it's quite slow to calculate, contrary to what you may think -- remember not every computer is powered by a GHz processor) and thus it was not included in the API.

For more info read about Minkowski distance (http://en.wikipedia.org/wiki/Minkowski_distance).

If you frequently do something and you have nanoseconds to spare then writing a function that does it should not pose a problem. You could even spend some extra nanoseconds and contribute that function to Qt.

By the way, it is quite similar to avoiding calculating sine value for small angles by approximating the result with the angle itself.

lisong
7th February 2018, 21:00
I happen to be looking for the same function today. I know it is not that hard to come up with your own sqrt on sum of power of 2. The disappointment is why such a common utility function is missing in qt.

Uwe
8th February 2018, 08:11
I do, however, frequently feel the need to calculate the distance between the mouse pointer and something else ...
This would be QLineF( pos1, pos2 ).length().

Uwe