All checks were successful
Sonarqube Scanner / Build and analyze (push) Successful in 51s
33 lines
1009 B
JavaScript
33 lines
1009 B
JavaScript
export function randomPointInCircle(origin, radiusPx) {
|
|
const angle = randomUnitFloat() * Math.PI * 2;
|
|
const distance = Math.sqrt(randomUnitFloat()) * radiusPx;
|
|
|
|
return {
|
|
x: origin.x + Math.cos(angle) * distance,
|
|
y: origin.y + Math.sin(angle) * distance
|
|
};
|
|
}
|
|
|
|
export function isWithinTeleportRadius(originCenter, candidateCenter, radiusPx) {
|
|
const dx = candidateCenter.x - originCenter.x;
|
|
const dy = candidateCenter.y - originCenter.y;
|
|
return Math.hypot(dx, dy) <= radiusPx;
|
|
}
|
|
|
|
export function sceneDistanceToPixels(distance) {
|
|
const gridSize = canvas.scene.grid.size;
|
|
const distancePerGrid = canvas.scene.grid.distance || 5;
|
|
return distance / distancePerGrid * gridSize;
|
|
}
|
|
|
|
function randomUnitFloat() {
|
|
const cryptoApi = globalThis.crypto;
|
|
if (!cryptoApi?.getRandomValues) {
|
|
throw new Error("A cryptographically secure random generator is not available.");
|
|
}
|
|
|
|
const values = new Uint32Array(1);
|
|
cryptoApi.getRandomValues(values);
|
|
return values[0] / 0x100000000;
|
|
}
|