getTargetedSquare = (target, style, uid) => {
if (style == undefined) {
style = {
highlight: { fill: "yellow", stroke: "yellow", "stroke-width": "1px" },
overlay: { fill: "none", stroke: "black", "stroke-width": "2px" },
default: { fill: "crimson", stroke: "red", "stroke-width": "1px" }
};
} else {
style = style;
}
if (uid == undefined) {
uid = Math.random().toString(36).substr(2, 4);
} else {
uid = uid;
}
if (typeof target == "string") {
let desc = target.split(" ").reverse();
if (Object.keys(dir).indexOf(desc[0]) > -1) {
let lookupMatrix = [];
let layer1 = [];
let layer2 = [];
let layer3 = [];
let layer4 = [];
switch (desc.length) {
case 1:
if (["Sh", "Wh", "Eh", "Nh"].indexOf(desc[desc.length - 1]) == -1) {
layer1 = scaleApply(matrixQuarter, 8).map((d) =>
d.map((d) => (d == dir[desc[0]] ? 1 : 0))
);
lookupMatrix = getAllIndexes(printMatrix(layer1, 16, 16), 1);
} else {
if (["Sh", "Nh"].indexOf(desc[0]) > -1) {
layer1 = scaleApply(matrixHalfNS, 8).map((d) =>
d.map((d) => (d == dir[desc[0]] ? 1 : 0))
);
}
if (["Wh", "Eh"].indexOf(desc[0]) > -1) {
layer1 = scaleApply(matrixHalfWE, 8).map((d) =>
d.map((d) => (d == dir[desc[0]] ? 1 : 0))
);
}
lookupMatrix = getAllIndexes(printMatrix(layer1, 16, 16), 1);
}
break;
case 2:
layer1 = scaleApply(matrixQuarter, 8).map((d) =>
d.map((d) => (d == dir[desc[0]] ? 1 : 0))
);
if (["Sh", "Wh", "Eh", "Nh"].indexOf(desc[desc.length - 1]) == -1) {
layer2 = scaleApply(matrixQuarterQuarter, 4).map((d) =>
d.map((d) => (d == dir[desc[1]] ? 1 : 0))
);
lookupMatrix = getAllIndexes(
printMatrix(math.dotMultiply(layer1, layer2), 16, 16),
1
);
} else {
let subMatrix = getAllIndexes(printMatrix(layer1, 16, 16), 1);
lookupMatrix = makeMatrix(subMatrix, 8);
if (desc[desc.length - 1] == "Sh") {
lookupMatrix = lookupMatrix.splice(4, 7).flat();
}
if (desc[desc.length - 1] == "Wh") {
lookupMatrix = lookupMatrix.map((d) => d.splice(0, 4)).flat();
}
if (desc[desc.length - 1] == "Eh") {
lookupMatrix = lookupMatrix.map((d) => d.splice(4, 7)).flat();
}
if (desc[desc.length - 1] == "Nh") {
lookupMatrix = lookupMatrix.splice(0, 4).flat();
}
}
break;
case 3:
layer1 = scaleApply(matrixQuarter, 8).map((d) =>
d.map((d) => (d == dir[desc[0]] ? 1 : 0))
);
layer2 = scaleApply(matrixQuarterQuarter, 4).map((d) =>
d.map((d) => (d == dir[desc[1]] ? 1 : 0))
);
layer3 = scaleApply(matrixQuarterQuarterQuarter, 2).map((d) =>
d.map((d) => (d == dir[desc[2]] ? 1 : 0))
);
if (["Sh", "Wh", "Eh", "Nh"].indexOf(desc[desc.length - 1]) == -1) {
layer3 = scaleApply(matrixQuarterQuarterQuarter, 2).map((d) =>
d.map((d) => (d == dir[desc[2]] ? 1 : 0))
);
lookupMatrix = getAllIndexes(
printMatrix(
math.dotMultiply(math.dotMultiply(layer1, layer2), layer3),
16,
16
),
1
);
} else {
let subMatrix = getAllIndexes(
printMatrix(math.dotMultiply(layer1, layer2), 16, 16),
1
);
if (desc[desc.length - 1] == "Sh") {
lookupMatrix = makeMatrix(subMatrix, 4)[2].concat(
makeMatrix(subMatrix, 4)[3]
);
}
if (desc[desc.length - 1] == "Wh") {
lookupMatrix = makeMatrix(subMatrix, 4)
.map((d) => d.splice(0, 2))
.flat();
}
if (desc[desc.length - 1] == "Eh") {
lookupMatrix = makeMatrix(subMatrix, 4)
.map((d) => d.splice(2, 4))
.flat();
}
if (desc[desc.length - 1] == "Nh") {
lookupMatrix = makeMatrix(subMatrix, 4)[0].concat(
makeMatrix(subMatrix, 4)[1]
);
}
}
break;
case 4:
layer1 = scaleApply(matrixQuarter, 8).map((d) =>
d.map((d) => (d == dir[desc[0]] ? 1 : 0))
);
layer2 = scaleApply(matrixQuarterQuarter, 4).map((d) =>
d.map((d) => (d == dir[desc[1]] ? 1 : 0))
);
layer3 = scaleApply(matrixQuarterQuarterQuarter, 2).map((d) =>
d.map((d) => (d == dir[desc[2]] ? 1 : 0))
);
layer4 = matrixQuarterQuarterQuarterQuarter.map((d) =>
d.map((d) => (d == dir[desc[3]] ? 1 : 0))
);
if (["Sh", "Wh", "Eh", "Nh"].indexOf(desc[desc.length - 1]) == -1) {
lookupMatrix = getAllIndexes(
printMatrix(
math.dotMultiply(
math.dotMultiply(math.dotMultiply(layer1, layer2), layer3),
layer4
),
16,
16
),
1
);
} else {
lookupMatrix = getAllIndexes(
printMatrix(
math.dotMultiply(math.dotMultiply(layer1, layer2), layer3),
16,
16
),
1
);
if (desc[desc.length - 1] == "Sh") {
lookupMatrix.splice(0, 2);
}
if (desc[desc.length - 1] == "Wh") {
lookupMatrix = [lookupMatrix[0], lookupMatrix[2]];
}
if (desc[desc.length - 1] == "Eh") {
lookupMatrix = [lookupMatrix[1], lookupMatrix[3]];
}
if (desc[desc.length - 1] == "Nh") {
lookupMatrix.splice(2, 4);
}
}
break;
}
let output = new Grid({
id: uid,
graphicHeight: 360,
graphicWidth: 360,
symbolWidth: 360 / 16,
symbolHeight: 360 / 16,
totalValue: 256,
symbolperLine: 16,
symbolRatio: 1,
selected: (feature) => {
if (lookupMatrix.indexOf(feature) > -1) {
return true;
} else {
return false;
}
},
fillColor: (feature) => {
if (lookupMatrix.indexOf(feature) > -1) {
return style["highlight"]["fill"];
} else {
return style["default"]["fill"];
}
},
strokeColor: (feature) => {
if (lookupMatrix.indexOf(feature) > -1) {
return style["highlight"]["stroke"];
} else {
return style["default"]["stroke"];
}
},
strokeWidth: (feature) => {
if (lookupMatrix.indexOf(feature) > -1) {
return style["highlight"]["stroke-width"];
} else {
return style["default"]["stroke-width"];
}
},
backgroundColor: "ivory",
borderColor: "black"
});
let q = new Grid({
id: "q",
graphicHeight: 360,
graphicWidth: 360,
symbolWidth: 360 / 2,
symbolHeight: 360 / 2,
totalValue: 2 * 2,
symbolperLine: 2,
symbolRatio: 1,
fillColor: style["overlay"]["fill"],
strokeColor: style["overlay"]["stroke"],
strokeWidth: style["overlay"]["stroke-width"],
backgroundColor: "black",
borderColor: "black"
});
var stringToHTML = function (str) {
var dom = document.createElement("div");
dom.innerHTML = str;
return dom;
};
return stringToHTML(
`<svg width="360" height="360">${output.render.innerHTML}${q.render.innerHTML}</svg>`
);
} else {
console.log("not a valid entry");
}
} else {
console.log("not a valid entry");
}
}