Skip to content
Snippets Groups Projects
Commit bb1a14d4 authored by Christoph-Anton Schwierz's avatar Christoph-Anton Schwierz
Browse files

vecNormalize

parent 7c01ae13
No related branches found
No related tags found
No related merge requests found
...@@ -13,7 +13,8 @@ ...@@ -13,7 +13,8 @@
<script type="module" src="/src/cg/pgIntro.ts"></script> <script type="module" src="/src/cg/pgIntro.ts"></script>
<script type="module" src="/src/cg/vector.ts"></script> <script type="module" src="/src/cg/vector.ts"></script>
<script type="module" src="/src/cg/perspectiveDivide.ts"></script> <script type="module" src="/src/cg/perspectiveDivide.ts"></script>
<script type="module" src="/src/cg/walkToVec.ts"></script--> <script type="module" src="/src/cg/walkToVec.ts"></script>
<script type="module" src="/src/cg/basis.ts"></script> <script type="module" src="/src/cg/basis.ts"></script-->
<script type="module" src="/src/cg/testing.ts"></script>
</body> </body>
</html> </html>
import Playground from "./playground";
import { vecNormalize } from "./utils";
import { vecLength } from "./utils";
const pg = new Playground();
// Some vectors
const v = [2,2,3]
const normalized = vecNormalize(v);
console.log(vecLength(normalized));
// Visualize
pg.gridXZ(); // Grid
pg.visVector(v,{color:"orange",label:"V"});
pg.visVector(normalized,{color:"red",label:"normalized"});
...@@ -33,4 +33,19 @@ export function vecLength(vec: Array<number>){ ...@@ -33,4 +33,19 @@ export function vecLength(vec: Array<number>){
length = Math.sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]); length = Math.sqrt(vec[0]*vec[0] + vec[1]*vec[1] + vec[2]*vec[2]);
return length; return length;
} }
export function vecNormalize(vec: Array<number>){
let computedLength = vecLength(vec);
if(computedLength == 0){computedLength = 1};
let inverse = 1 / computedLength;
let normalized: Array<number> = new Array;
normalized[0] = vec[0] * inverse;
normalized[1] = vec[1] * inverse;
normalized[2] = vec[2] * inverse;
return normalized;
}
// //
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment