@brad_promptnow/vec3
v1.0.1
Published
Small, dependency-free 3D vector math for TypeScript and JavaScript.
Readme
@brad_promptnow/vec3
Small, dependency-free 3D vector math for TypeScript and JavaScript.
- Plain objects (
{ x, y, z }), no classes - Full TypeScript types included
- Every function returns a new value and never modifies its inputs
Install
npm install @brad_promptnow/vec3Quick start
import { v3_add, v3_cross, v3_length } from "@brad_promptnow/vec3";
import type { V3 } from "@brad_promptnow/vec3";
const a: V3 = { x: 1, y: 0, z: 0 };
const b: V3 = { x: 0, y: 1, z: 0 };
v3_add(a, b); // { x: 1, y: 1, z: 0 }
v3_cross(a, b); // { x: 0, y: 0, z: 1 }
v3_length({ x: 2, y: 3, z: 6 }); // 7CommonJS also works:
const { v3_add } = require("@brad_promptnow/vec3");API
Type
type V3 = { x: number; y: number; z: number };Functions
| Function | Returns | Description |
|---|---|---|
| v3_add(a, b) | V3 | a + b |
| v3_sub(a, b) | V3 | a - b |
| v3_mul(a, b) | V3 | Component-wise multiply: (a.x*b.x, a.y*b.y, a.z*b.z) |
| v3_scale(a, s) | V3 | Multiply every component by the number s |
| v3_dot(a, b) | number | Dot product |
| v3_cross(a, b) | V3 | Cross product, perpendicular to both inputs |
| v3_length(a) | number | Length (magnitude) of the vector |
Notes
- Dot product is positive when the vectors point the same way,
0when perpendicular, and negative when opposite. - Cross product is order-sensitive:
v3_cross(a, b)points the opposite way fromv3_cross(b, a). v3_mulvsv3_scale:v3_multakes two vectors and multiplies each axis separately.v3_scaletakes a vector and a single number.
Examples
import { v3_sub, v3_scale, v3_dot } from "@brad_promptnow/vec3";
// direction from point p to point q
const dir = v3_sub({ x: 4, y: 5, z: 6 }, { x: 1, y: 2, z: 3 }); // { x: 3, y: 3, z: 3 }
// move a position by velocity * time
const next = v3_scale({ x: 1, y: 0, z: 0 }, 2.5); // { x: 2.5, y: 0, z: 0 }
// are two vectors perpendicular?
v3_dot({ x: 1, y: 0, z: 0 }, { x: 0, y: 1, z: 0 }) === 0; // trueDevelopment
npm install
npm run build # compiles src/ to dist/
npm pack # creates a .tgz to test locallyReleasing a new version:
npm version patch # or minor / major
npm run build
npm publish --access publicLicense
ISC
