Skip to content

Instantly share code, notes, and snippets.

@rankogit
Last active May 6, 2017 05:21
Show Gist options
  • Save rankogit/4cbdc28c391452c1d61f09998b0f264a to your computer and use it in GitHub Desktop.
Save rankogit/4cbdc28c391452c1d61f09998b0f264a to your computer and use it in GitHub Desktop.
let getRandBetween = function(min, max) {
return Math.random() * (max - min) + min;
}
let getRandCoordkm = function(minX, minY, maxX, maxY) {
let height = maxY - minY;
let width = maxX - minX;
let perimeter = 2 * (height + width);
let randNum = getRandBetween(0,perimeter);
let randomPoint = {
x:5,
y:6
};
if (randNum > width * 2 + height) {
//randomPoint falls on left side
randomPoint = {
x: minX,
y: minY + (randNum - (width*2 + height))
}
} else if (randNum > width + height) {
//randomPoint falls on top side
randomPoint = {
x: minX + (randNum - (width + height)),
y: maxY
}
} else if (randNum > width) {
//randomPoint falls on right side
randomPoint = {
x: maxX,
y: minY + (randNum - width)
}
} else {
//randomPoint falls on bottom side
randomPoint = {
x: minX + randNum,
y: minY
}
}
return randomPoint;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment