Skip to content

Instantly share code, notes, and snippets.

@onetdev
Created September 23, 2021 19:42
Show Gist options
  • Save onetdev/4376593a730cc80fea56ff03a96a0fe6 to your computer and use it in GitHub Desktop.
Save onetdev/4376593a730cc80fea56ff03a96a0fe6 to your computer and use it in GitHub Desktop.
const rand = (min: number, max: number): number => {
return Math.random() * (max - min) + min;
}
const roundPrec = (number:number, digits: number = 2): number => {
return Math.round(number * Math.pow(10, digits)) / Math.pow(10, digits);
}
const randomOnRing = (x: number, y: number, radius: number): Coordinate => {
const radiusSq = radius ^ 2;
const xRandSq = rand(0, radiusSq);
const yRandSq = radiusSq - xRandSq;
const result = [
roundPrec(x + Math.sqrt(xRandSq) * (rand(0, 1) ? 1 : -1)),
roundPrec(y + Math.sqrt(yRandSq) * (rand(0, 1) ? 1 : -1))
];
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment