File size: 1,120 Bytes
6ce4ca6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
export interface Position3D {
	x: number;
	y: number;
	z: number;
}

/**
 * Generate a random position within the given ranges
 */
export function generateRandomPosition(
	xRange: [number, number],
	yRange: [number, number],
	zRange: [number, number]
): Position3D {
	return {
		x: Math.random() * (xRange[1] - xRange[0]) + xRange[0],
		y: Math.random() * (yRange[1] - yRange[0]) + yRange[0],
		z: Math.random() * (zRange[1] - zRange[0]) + zRange[0]
	};
}

/**
 * Clamp a position within bounds
 */
export function clampPosition(
	position: Position3D,
	bounds: {
		x: [number, number];
		y: [number, number];
		z: [number, number];
	}
): Position3D {
	return {
		x: Math.max(bounds.x[0], Math.min(bounds.x[1], position.x)),
		y: Math.max(bounds.y[0], Math.min(bounds.y[1], position.y)),
		z: Math.max(bounds.z[0], Math.min(bounds.z[1], position.z))
	};
}

/**
 * Calculate distance between two positions
 */
export function getDistance(pos1: Position3D, pos2: Position3D): number {
	const dx = pos2.x - pos1.x;
	const dy = pos2.y - pos1.y;
	const dz = pos2.z - pos1.z;
	return Math.sqrt(dx * dx + dy * dy + dz * dz);
}