Skip to content

Instantly share code, notes, and snippets.

@rfprod
Created May 1, 2017 17:42
Show Gist options
  • Save rfprod/1fbf31fa9648d24d8d07dae5939be23e to your computer and use it in GitHub Desktop.
Save rfprod/1fbf31fa9648d24d8d07dae5939be23e to your computer and use it in GitHub Desktop.
Dartboard Score
function dartboardScore(x, y) {
// console.log(x, y);
const radius = {
DB: 12.7 / 2,
SB: 31.8 / 2,
trippleInner: 198 / 2,
trippleOuter: 214 / 2,
doubleInner: 324 / 2,
doubleOuter: 340 / 2
};
const radiusKeys = Object.keys(radius);
const length = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
// console.log('length:', length);
let radiusCode = 'X';
let prevVal = 0;
checkRadius:
for (let key of radiusKeys) {
// console.log(key);
const val = radius[key];
if (length > prevVal && length < val) {
radiusCode = key;
break checkRadius;
}
prevVal = val;
}
if (/(Inner|Outer)/.test(radiusCode)) {
radiusCode = (/Inner/.test(radiusCode)) ? '' : (/tripple/.test(radiusCode)) ? 'T' : 'D';
}
const degree = 360 / 20;
let boardSections = [
{ start: 9, end: 9 + degree, score: 1 },
{ start: 9, end: 9, score: 18 },
{ start: 9, end: 9, score: 4 },
{ start: 9, end: 9, score: 13 },
{ start: 9, end: 9, score: 6 },
{ start: 9, end: 9, score: 10 },
{ start: 9, end: 9, score: 15 },
{ start: 9, end: 9, score: 2 },
{ start: 9, end: 9, score: 17 },
{ start: 9, end: 9, score: 3 },
{ start: 9, end: 9, score: 19 },
{ start: 9, end: 9, score: 7 },
{ start: 9, end: 9, score: 16 },
{ start: 9, end: 9, score: 8 },
{ start: 9, end: 9, score: 11 },
{ start: 9, end: 9, score: 14 },
{ start: 9, end: 9, score: 9 },
{ start: 9, end: 9, score: 12 },
{ start: 9, end: 9, score: 5 },
{ start: 9, end: 9, score: 20 }
];
boardSections = boardSections.map((item, index, arr) => {
item.start = (!index) ? 9 : arr[index - 1].end;
item.end = (index === arr.length - 1) ? 369 : item.start + degree;
return item;
});
// console.log('boardSections:', boardSections);
let angle = 0;
if (x > 0 && y > 0) {
angle = Math.acos((y / length)) * 180 / Math.PI;
} else if (x > 0 && y < 0) {
angle = 90 + Math.acos((x / length)) * 180 / Math.PI;
} else if (x < 0 && y < 0) {
angle = 180 + Math.acos((Math.abs(y) / length)) * 180 / Math.PI;
} else if (x < 0 && y > 0) {
angle = 270 + Math.acos((Math.abs(x) / length)) * 180 / Math.PI;
}
// console.log('angle:', angle);
angle = (angle < 9) ? 360 + angle : angle; // correct angle to match sections map properly
const section = boardSections.filter(item => angle > item.start && angle < item.end)[0];
// console.log('section:', section);
return (!/(DB|SB|X)/.test(radiusCode)) ? radiusCode + section.score : radiusCode;
}
// TEST
dartboardScore(-133.69, -147.38); // "X"
dartboardScore(4.06, 0.71); // "DB"
dartboardScore(2.38, -6.06); // "SB"
dartboardScore(-73.905, -95.94); // "7"
dartboardScore(55.53, -87.95); // "T2"
dartboardScore(-145.19, 86.53); // "D9"

Dartboard Score

The function dartboardScore(x, y) accepts coordinates of thrown dart and returns gained score, e.g.:

Outside of the board: "X"
Bull's eye: "DB"
Bull: "SB"
A single number, example: "10"
A triple number: "T10"
A double number: "D10"

The coordinates (x, y) are relative to the center of the board (0, 0). The unit is millimeters. If dart is thrown 2 centimeters to the left and 1 centimeters below, it is written as: getDartboardScore(-20, -10);

A script by V.

License.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment