Created
September 23, 2021 19:42
-
-
Save onetdev/4376593a730cc80fea56ff03a96a0fe6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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