Last active
May 10, 2019 08:03
-
-
Save black-black-cat/3abfcabcc391bcd0b59282bc8182e2c0 to your computer and use it in GitHub Desktop.
getCursor
This file contains 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
console.log('Hello World!'); | |
/** | |
* | |
* @param {string|number} direIndex direction name or direction index | |
* @param {number} [angle] | |
* | |
*/ | |
function getCursor (direIndex, angle = 0) { | |
let direNames = ['n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw'] | |
let j = direNames.indexOf(direIndex) | |
if (j > -1) { | |
direIndex = j | |
} | |
let parts = buildParts(45) | |
angle = handleAngle(angle, -45/2) | |
let partIndex = 0; | |
parts.some((part, i) => { | |
let yes = angle >= part[0] && angle < part[1] | |
if (yes) { | |
partIndex = i | |
} | |
return yes | |
}) | |
let nameIndex = (direIndex + partIndex) % 8 | |
return direNames[nameIndex] | |
} | |
// parts | |
// [ | |
// [-22.5, 22.5], | |
// ... | |
// ] | |
function buildParts (piece = 45) { | |
let len = 8 | |
let arr = [] | |
for (let i = 0; i < len; i++) { | |
arr.push([i * piece - piece / 2, (i + 1) * piece - piece / 2]) | |
} | |
// arr[0][1] -= piece / 2 | |
// arr[len] = [len * piece, len * piece + piece / 2] | |
return arr | |
} | |
function handleAngle (angle, offset) { | |
angle %= 360 | |
if (angle < 0 && angle > offset) { | |
return angle | |
} | |
if (angle < 0) { | |
angle += 360 | |
} | |
return angle | |
} | |
log = console.log.bind(null) | |
log(getCursor(7, -22.511111111111111111111111)) | |
log(getCursor(7, 90)) | |
log(getCursor(7,-90)) | |
log(getCursor('nw', -90)) | |
This file contains 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
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>GistRun</title> | |
<link rel="stylesheet" href="styles.css"> | |
</head> | |
<body> | |
<h1>Hello world!</h1> | |
<script src="script.js"></script> | |
</body> | |
</html> |
This file contains 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
console.log('Hello World!'); | |
/** | |
* | |
* @param {string|number} direIndex direction name or direction index | |
* @param {number} [angle] | |
* | |
*/ | |
function getCursor (direIndex, angle = 0) { | |
let direNames = ['n', 'ne', 'e', 'se', 's', 'sw', 'w', 'nw'] | |
let j = direNames.indexOf(direIndex) | |
if (j > -1) { | |
direIndex = j | |
} | |
angle = handleAngle(angle) | |
let partIndex = 0 | |
let int = Math.floor(angle / 22.5) | |
let parts = buildParts() | |
parts.some((part, i) => { | |
let yes = part.indexOf(int) > -1 | |
if (yes) { | |
partIndex = i | |
} | |
return yes | |
}) | |
let nameIndex = (direIndex + partIndex) % 8 | |
return direNames[nameIndex] | |
} | |
function buildParts () { | |
let arr = [] | |
let len = 16 | |
for (i = 0; i < len; i += 2) { | |
let p = i - 1 | |
if (p < 0) { | |
p = len - 1 | |
} | |
arr.push([p, i]) | |
} | |
return arr | |
} | |
function handleAngle (angle) { | |
angle %= 360 | |
if (angle < 0) { | |
angle += 360 | |
} | |
return angle | |
} | |
log = console.log.bind(null) | |
log(getCursor(7, -22.511111111111111111111111)) | |
log(getCursor(7, 90)) | |
log(getCursor(7,-90)) | |
log(getCursor('nw', -90)) | |
This file contains 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
/* todo: add styles */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment