Skip to content

Commit 7e152bb

Browse files
authored
vecX: add truncate and midpoint functions. fixes #8 (#26)
* vecX: add truncate and midpoint functions * who lints ya, baby?
1 parent 3d6ecfc commit 7e152bb

File tree

6 files changed

+213
-0
lines changed

6 files changed

+213
-0
lines changed

src/vec2-impl.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,3 +680,32 @@ export function setLength(a: Vec2, len: number, dst?: Vec2) {
680680
normalize(a, dst);
681681
return mulScalar(dst, len, dst);
682682
}
683+
684+
/**
685+
* Ensure a vector is not longer than a max length
686+
*
687+
* @param a The vec2 to limit
688+
* @param maxLen The longest length of the resulting vector
689+
* @returns The vector, shortened to maxLen if it's too long
690+
*/
691+
export function truncate(a: Vec2, maxLen: number, dst?: Vec2) {
692+
dst = dst || new VecType(2);
693+
694+
if (length(a) > maxLen) {
695+
return setLength(a, maxLen, dst);
696+
}
697+
698+
return copy(a, dst);
699+
}
700+
701+
/**
702+
* Return the vector exactly between 2 endpoint vectors
703+
*
704+
* @param a Endpoint 1
705+
* @param b Endpoint 2
706+
* @returns The vector exactly residing between endpoints 1 and 2
707+
*/
708+
export function midpoint(a: Vec2, b: Vec2, dst?: Vec2) {
709+
dst = dst || new VecType(2);
710+
return lerp(a, b, 0.5, dst);
711+
}

src/vec3-impl.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,3 +896,32 @@ export function setLength(a: Vec3, len: number, dst?: Vec3) {
896896
normalize(a, dst);
897897
return mulScalar(dst, len, dst);
898898
}
899+
900+
/**
901+
* Ensure a vector is not longer than a max length
902+
*
903+
* @param a The vec3 to limit
904+
* @param maxLen The longest length of the resulting vector
905+
* @returns The vector, shortened to maxLen if it's too long
906+
*/
907+
export function truncate(a: Vec3, maxLen: number, dst?: Vec3) {
908+
dst = dst || new VecType(3);
909+
910+
if (length(a) > maxLen) {
911+
return setLength(a, maxLen, dst);
912+
}
913+
914+
return copy(a, dst);
915+
}
916+
917+
/**
918+
* Return the vector exactly between 2 endpoint vectors
919+
*
920+
* @param a Endpoint 1
921+
* @param b Endpoint 2
922+
* @returns The vector exactly residing between endpoints 1 and 2
923+
*/
924+
export function midpoint(a: Vec3, b: Vec3, dst?: Vec3) {
925+
dst = dst || new VecType(3);
926+
return lerp(a, b, 0.5, dst);
927+
}

src/vec4-impl.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,3 +646,32 @@ export function setLength(a: Vec4, len: number, dst?: Vec4) {
646646
normalize(a, dst);
647647
return mulScalar(dst, len, dst);
648648
}
649+
650+
/**
651+
* Ensure a vector is not longer than a max length
652+
*
653+
* @param a The vec4 to limit
654+
* @param maxLen The longest length of the resulting vector
655+
* @returns The vector, shortened to maxLen if it's too long
656+
*/
657+
export function truncate(a: Vec4, maxLen: number, dst?: Vec4) {
658+
dst = dst || new VecType(4);
659+
660+
if (length(a) > maxLen) {
661+
return setLength(a, maxLen, dst);
662+
}
663+
664+
return copy(a, dst);
665+
}
666+
667+
/**
668+
* Return the vector exactly between 2 endpoint vectors
669+
*
670+
* @param a Endpoint 1
671+
* @param b Endpoint 2
672+
* @returns The vector exactly residing between endpoints 1 and 2
673+
*/
674+
export function midpoint(a: Vec4, b: Vec4, dst?: Vec4) {
675+
dst = dst || new VecType(4);
676+
return lerp(a, b, 0.5, dst);
677+
}

test/tests/vec2-test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,5 +510,47 @@ describe('vec2', () => {
510510
});
511511
});
512512

513+
describe('truncate', function() {
514+
describe('limit a vector to a max length', function() {
515+
let vecA;
516+
517+
beforeEach(function () {
518+
vecA = [10.323759005323593, 10.323759005323593];
519+
});
520+
521+
it("should shorten the vector", function () {
522+
const result = vec2.truncate(vecA, 4.0);
523+
assertEqualApproximately(result, [2.82842712474619, 2.82842712474619]);
524+
assertEqualApproximately(vec2.length(result), 4.0);
525+
});
526+
527+
it("should preserve the vector when shorter than maxLen", function () {
528+
const result = vec2.truncate(vecA, 18.0);
529+
assertEqualApproximately(result, [10.323759005323593, 10.323759005323593]);
530+
assertEqualApproximately(vec2.length(result), 14.6);
531+
});
532+
});
533+
});
534+
535+
describe('midpoint', function() {
536+
describe('return the midpoint between 2 vectors', function() {
537+
538+
it("should return the midpoint", function () {
539+
const vecA = [ 0, 0 ]
540+
const vecB = [ 10, 10 ]
541+
const result = vec2.midpoint(vecA, vecB);
542+
assertEqualApproximately(result, [ 5, 5 ]);
543+
});
544+
545+
it("should handle negatives", function () {
546+
const vecA = [ -10, -10 ]
547+
const vecB = [ 10, 10 ]
548+
const result = vec2.midpoint(vecA, vecB);
549+
assertEqualApproximately(result, [ 0, 0 ]);
550+
});
551+
552+
});
553+
});
554+
513555
});
514556

test/tests/vec3-test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,5 +566,47 @@ describe('vec3', () => {
566566
});
567567
});
568568

569+
describe('truncate', function() {
570+
describe('limit a vector to a max length', function() {
571+
let vecA;
572+
573+
beforeEach(function () {
574+
vecA = [8.429313930168536, 8.429313930168536, 8.429313930168536];
575+
});
576+
577+
it("should shorten the vector", function () {
578+
const result = vec3.truncate(vecA, 4.0);
579+
assertEqualApproximately(result, [2.309401076758503, 2.309401076758503, 2.309401076758503]);
580+
assertEqualApproximately(vec3.length(result), 4.0);
581+
});
582+
583+
it("should preserve the vector when shorter than maxLen", function () {
584+
const result = vec3.truncate(vecA, 18.0);
585+
assertEqualApproximately(result, [8.429313930168536, 8.429313930168536, 8.429313930168536]);
586+
assertEqualApproximately(vec3.length(result), 14.6);
587+
});
588+
});
589+
});
590+
591+
describe('midpoint', function() {
592+
describe('return the midpoint between 2 vectors', function() {
593+
594+
it("should return the midpoint", function () {
595+
const vecA = [ 0, 0, 0 ]
596+
const vecB = [ 10, 10, 10 ]
597+
const result = vec3.midpoint(vecA, vecB);
598+
assertEqualApproximately(result, [ 5, 5, 5 ]);
599+
});
600+
601+
it("should handle negatives", function () {
602+
const vecA = [ -10, -10, -10 ]
603+
const vecB = [ 10, 10, 10 ]
604+
const result = vec3.midpoint(vecA, vecB);
605+
assertEqualApproximately(result, [ 0, 0, 0 ]);
606+
});
607+
608+
});
609+
});
610+
569611
});
570612

test/tests/vec4-test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,5 +419,47 @@ describe('vec4', () => {
419419
});
420420
});
421421

422+
describe('truncate', function() {
423+
describe('limit a vector to a max length', function() {
424+
let vecA;
425+
426+
beforeEach(function () {
427+
vecA = [8.429313930168536, 8.429313930168536, 8.429313930168536, 8.429313930168536];
428+
});
429+
430+
it("should shorten the vector", function () {
431+
const result = vec4.truncate(vecA, 4.0);
432+
assertEqualApproximately(result, [2, 2, 2, 2]);
433+
assertEqualApproximately(vec4.length(result), 4.0);
434+
});
435+
436+
it("should preserve the vector when shorter than maxLen", function () {
437+
const result = vec4.truncate(vecA, 18.0);
438+
assertEqualApproximately(result, [8.429313930168536, 8.429313930168536, 8.429313930168536, 8.429313930168536]);
439+
assertEqualApproximately(vec4.length(result), 16.858627860337073);
440+
});
441+
});
442+
});
443+
444+
describe('midpoint', function() {
445+
describe('return the midpoint between 2 vectors', function() {
446+
447+
it("should return the midpoint", function () {
448+
const vecA = [ 0, 0, 0, 0 ]
449+
const vecB = [ 10, 10, 10, 10 ]
450+
const result = vec4.midpoint(vecA, vecB);
451+
assertEqualApproximately(result, [ 5, 5, 5, 5 ]);
452+
});
453+
454+
it("should handle negatives", function () {
455+
const vecA = [ -10, -10, -10, -10 ]
456+
const vecB = [ 10, 10, 10, 10 ]
457+
const result = vec4.midpoint(vecA, vecB);
458+
assertEqualApproximately(result, [ 0, 0, 0, 0 ]);
459+
});
460+
461+
});
462+
});
463+
422464
});
423465

0 commit comments

Comments
 (0)