I am going though a functional source that has the following class:

 class Point3D
       int x ,y,z;

       int vSize2() const
           return x*x +
                  y*y +

       int vSize() const
         return sqrt(vSize2());

It is hard to understand the functionalities of the above two functions (vSize2() and vSize()). It seems to me that it is calculating the length between the point and the origin.

Any thoughts?


No, a point does not have a length. A point is only a location - it has no extent in any direction.

You are correct in guessing that the function vSize() returns the distance from the origin to the point. The other function, vSize2() returns the square of that distance. This is used in calculating the distance, and in some cases it may be useful to work directly with the square of the distance instead of the distance. This avoids calculating square roots, so can lead to improvements in speed and accuracy.

Note that as Nero points out, the functions are using only ints (integer variables and return values). The distance between a point and the origin is not in general an integer value, even if the coordinates of the point are all integers. The distance squared returned by vSize2() will be accurate, but the distance itself returned by vSize() will be accurate only to the nearest integer.

  • 2
    $\begingroup$ Additionally, the code for vSize() does not return very precise results as the square root of an integer is not necessarily also an integer, but the method returns an int. Actually, in most cases the given method will return imprecise results. $\endgroup$
    – Nero
    May 20 '16 at 15:36
  • $\begingroup$ It might be used as a hash of some kind. Or maybe they are using a integer quantized field. $\endgroup$
    – joojaa
    May 20 '16 at 20:22
  • $\begingroup$ Basically the integer is 64-bit. Will it make any difference in that case ? $\endgroup$
    – sajis997
    May 21 '16 at 9:56
  • $\begingroup$ 64 bit integers allow for a larger maximum number than 32 bit integers, but they can still only take whole number values. You could ask why integers are used as a separate question, but that would require access to more of the code and some context on what it is used for. Without that context it is difficult to judge whether this would be on topic for Computer Graphics. $\endgroup$
    – trichoplax
    May 21 '16 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.