@vicerp/utils
v1.0.0
Published
Shared utilities for ViceRP - math, types, and common helpers
Readme
@vicerp/utils
Shared utilities for ViceRP - math, types, and common helpers.
Installation
npm install @vicerp/utilsUsage
Static Utility Class (No Dependencies)
import { MathUtility, Vector3 } from '@vicerp/utils';
// Check if a point is inside a 2D polygon
const isInside = MathUtility.isPointInArea2D([5, 5], [[0,0], [10,0], [10,10], [0,10]]);
// Calculate vector magnitude
const length = MathUtility.modulus({ x: 3, y: 4, z: 0 }); // 5
// Check if position is within a 3D sphere
const inRadius = MathUtility.isPositionWithinRadius(
{ x: 5, y: 5, z: 5 },
{ x: 0, y: 0, z: 0 },
10
);
// Check if position is within a 3D polygon volume (supports sloped terrain)
const inPolygon = MathUtility.isPositionWithinPolygon3D(
{ x: 5, y: 5, z: 2 },
[{ x: 0, y: 0, z: 0 }, { x: 10, y: 0, z: 0 }, { x: 10, y: 10, z: 5 }, { x: 0, y: 10, z: 5 }],
3
);NestJS Injectable Service
import { Module } from '@nestjs/common';
import { MathModule, MathService } from '@vicerp/utils';
@Module({
imports: [MathModule],
})
export class AppModule {}
// In your service
@Injectable()
export class MyService {
constructor(private readonly mathService: MathService) {}
checkPosition(position: Vector3) {
return this.mathService.isPositionWithinRadius(position, { x: 0, y: 0, z: 0 }, 100);
}
}API
Types
Vector3- 3D vector with x, y, z componentsVector2- 2D vector with x, y componentsRadialArea- Vector3 with radiusPolygonalArea- Polygon vertices with height
MathUtility
Static utility class with the following methods:
isPointInArea2D(point, area)- Check if 2D point is inside polygonmodulus(vector)- Calculate vector magnitudegetAngleSumBetweenPositionAndVertices(position, vertices)- Calculate angle sum for 3D point-in-polygoninterpolateZAtPosition(x, y, vertices)- Interpolate Z coordinate at XY positionisPositionWithinPolygon3D(position, vertices, height)- Check if point is in 3D polygon volumeisPositionWithinRadius(position, center, radius)- Check if point is in sphere
Constants
TWOPI- 2 * PI (6.283185307179586476925287)EPSILON- Small value for floating point comparisons (0.0000001)
License
MIT
