diff options
author | Mitchell Stokes <mogurijin@gmail.com> | 2011-08-11 13:40:14 +0400 |
---|---|---|
committer | Mitchell Stokes <mogurijin@gmail.com> | 2011-08-11 13:40:14 +0400 |
commit | 165e6dbc07c4bf00fe492d4f65ea276a39628a85 (patch) | |
tree | cac56c25b765bc8f6b3bed7ca6c2adf964cfce91 /source/blender/python/mathutils/mathutils_Vector.c | |
parent | 99caa9470e9dc420541dc49c29037f6bac55964e (diff) |
Adding a readonly length_squared property to mathutils.Vector. This is simply vector.dot(vector), so nothing new is really added, but it's nice for writing more intent revealing code. In other words:
if vec.dot(vec) > some_distance*some_distance:
do_something()
might not be quite as obvious looking as:
if vec.length_squared > some_distance*some_distance:
do_something()
As to why you'd want to use length_squared over length is that length uses a square root, which isn't always necessary for simple distance checks (e.g., closest object, checks like the ones above, ect).
Diffstat (limited to 'source/blender/python/mathutils/mathutils_Vector.c')
-rw-r--r-- | source/blender/python/mathutils/mathutils_Vector.c | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/source/blender/python/mathutils/mathutils_Vector.c b/source/blender/python/mathutils/mathutils_Vector.c index a954c07c98d..9d52f45665f 100644 --- a/source/blender/python/mathutils/mathutils_Vector.c +++ b/source/blender/python/mathutils/mathutils_Vector.c @@ -1728,6 +1728,21 @@ static int Vector_setLength(VectorObject *self, PyObject *value) return 0; } +/* vector.length_squared */ +static PyObject *Vector_getLengthSquared(VectorObject *self, void *UNUSED(closure)) +{ + double dot = 0.0f; + int i; + + if(BaseMath_ReadCallback(self) == -1) + return NULL; + + for(i = 0; i < self->size; i++){ + dot += (double)(self->vec[i] * self->vec[i]); + } + return PyFloat_FromDouble(dot); +} + /* Get a new Vector according to the provided swizzle. This function has little error checking, as we are in control of the inputs: the closure is set by us in Vector_createSwizzleGetSeter. */ @@ -1851,6 +1866,7 @@ static PyGetSetDef Vector_getseters[] = { {(char *)"z", (getter)Vector_getAxis, (setter)Vector_setAxis, (char *)"Vector Z axis (3D Vectors only).\n\n:type: float", (void *)2}, {(char *)"w", (getter)Vector_getAxis, (setter)Vector_setAxis, (char *)"Vector W axis (4D Vectors only).\n\n:type: float", (void *)3}, {(char *)"length", (getter)Vector_getLength, (setter)Vector_setLength, (char *)"Vector Length.\n\n:type: float", NULL}, + {(char *)"length_squared", (getter)Vector_getLengthSquared, (setter)NULL, (char *)"Vector length squared (v.dot(v)).\n\n:type: float", NULL}, {(char *)"magnitude", (getter)Vector_getLength, (setter)Vector_setLength, (char *)"Vector Length.\n\n:type: float", NULL}, {(char *)"is_wrapped", (getter)BaseMathObject_getWrapped, (setter)NULL, (char *)BaseMathObject_Wrapped_doc, NULL}, {(char *)"owner", (getter)BaseMathObject_getOwner, (setter)NULL, (char *)BaseMathObject_Owner_doc, NULL}, |