// =========================================================================== // Copyright 2018 Autodesk, Inc. All rights reserved. // // Use of this software is subject to the terms of the Autodesk license // agreement provided at the time of installation or download, or which // otherwise accompanies this software in either electronic or hard copy form. // =========================================================================== // // // // // // // int normalize( float $v[] ) // // // Normalizes a float array of 3 values. // // // float[] $v Array of 3 floats to be normalized // // // int : -1 if the float array was zero length. // 1 if the normalized vector was successfully calculated. // // // // float $v0[] = {3,6,9}; // normalize($v0); // // Results: 1 // // // // global proc int normalize( float $v[] ) { if ( size($v) != 3 ) warning (uiRes("m_normalize.kGiveFloatArray")); float $factor = 1.0; float $len = $v[0]*$v[0] + $v[1]*$v[1] + $v[2]*$v[2]; if( $len == 0.0 ) return -1; else $factor = 1.0/sqrt($len); if ( $factor != 1.0 ) { $v[0] *= $factor; $v[1] *= $factor; $v[2] *= $factor; } return 1; }