Skip to content

Instantly share code, notes, and snippets.

@whyvez
Last active June 5, 2020 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save whyvez/ee7ec44155d199c1f541aa7cbe520e22 to your computer and use it in GitHub Desktop.
Save whyvez/ee7ec44155d199c1f541aa7cbe520e22 to your computer and use it in GitHub Desktop.
Gable board calculator.
#!/usr/bin/env node
const gabbleWidth = 22*12;
const boardCoverage = 6.375;
const boardLength = 144;
const nBoards = Math.ceil(gabbleWidth/boardCoverage);
const narray = (n, v) => {
let arr = [...Array(Math.ceil(n+1)).keys()];
arr.shift();
if (v) arr = arr.map(i => v);
return arr;
}
const xarray = (arr, x) => {
let result;
for (let i = 0; i < x; i++) {
result = arr.concat(arr);
}
return result;
}
const placements = narray(nBoards/2);
const lengths = placements.map((b) => {
return Math.tan(27 * Math.PI / 180) * (b*boardCoverage);
});
const totalLengths = xarray(lengths, 4);
// add existing cutoffs
const cuttoffs = []
.concat(narray(4, 56))
.concat(narray(2, 25))
.concat(narray(10, 32))
.concat(narray(8, 35));
let boards = 0;
do {
let cutoff, candidate, candidates;
const length = totalLengths.pop();
console.log(`Need a board of ${length} inches.`);
console.log(`Checking cutoffs...`);
candidates = cuttoffs.filter((c) => c > length);
console.log(`Found ${candidates.length} cutoffs that would work.`);
if (candidates.length > 0) {
console.log(`Looking for shortest cutoff...`);
candidate = candidates.reduce((m,c) => m < c ? m : c);
console.log(`Found a cutoff of ${candidate} inches.`);
cuttoffs.splice(cuttoffs.indexOf(candidate), 1);
cuttoffs.push(candidate-length);
} else {
console.log(`No cutoff found, cutting into a new board...`);
boards++;
cutoff = boardLength-length;
cuttoffs.push(cutoff);
}
}
while (totalLengths.length > 0);
console.log(`You will need ${boards} boards.`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment