WIP: simplify/clarify firing arc algo

This commit is contained in:
Catalin Constantin Mititiuc 2024-04-25 20:06:51 -07:00
parent 1833dfebf1
commit 917058f024
3 changed files with 106 additions and 258 deletions

View File

@ -144,11 +144,15 @@ polygon.firing-arc[data-allegiance="liao"] {
stroke: none; stroke: none;
} }
#lines polygon { #lines polyline, #lines line {
fill: none; fill: none;
stroke: black; stroke: black;
} }
#lines line {
pointer-events: none;
}
.sight-line { .sight-line {
stroke: orangered; stroke: orangered;
stroke-width: 0.5px; stroke-width: 0.5px;
@ -203,7 +207,7 @@ g.start-locations > g:last-child {
} }
/* Inradius and circumradius values come from the hexagon */ /* Inradius and circumradius values come from the hexagon */
.grid { .grid, g.start-locations {
--inradius: 8.66px; --inradius: 8.66px;
--circumradius: 10px; --circumradius: 10px;
--x-step: calc(var(--inradius) * 2); --x-step: calc(var(--inradius) * 2);
@ -215,7 +219,7 @@ g[data-y], g.start-locations > g {
transform: translate(var(--translateX), calc(var(--y-step) * var(--i))); transform: translate(var(--translateX), calc(var(--y-step) * var(--i)));
} }
g[data-y]:nth-child(even) { g[data-y]:nth-child(odd) {
--translateX: calc(var(--inradius)); --translateX: calc(var(--inradius));
} }

View File

@ -45,15 +45,13 @@
<image id="map3" class="map-scans" href="scans/map3.jpg" width="2284" height="1518"/> <image id="map3" class="map-scans" href="scans/map3.jpg" width="2284" height="1518"/>
</g> </g>
<g class="board">
<g id="test-arcs">
</g>
<g id="firing-arcs"> <g id="firing-arcs">
<g id="shapes"/> <g id="shapes"/>
<g id="lines"/> <g id="lines"/>
</g> </g>
<g class="board">
<g id="test-arcs">
</g>
<g class="grid">
<g class="start-locations"> <g class="start-locations">
<g> <g>
<g data-x="13" class="counter" data-allegiance="liao" data-number="1"><use href="#t-1"/></g> <g data-x="13" class="counter" data-allegiance="liao" data-number="1"><use href="#t-1"/></g>
@ -74,6 +72,7 @@
<g data-x="19" class="counter" data-allegiance="davion" data-number="7"><use href="#t-7"/></g> <g data-x="19" class="counter" data-allegiance="davion" data-number="7"><use href="#t-7"/></g>
</g> </g>
</g> </g>
<g class="grid">
<g data-y="0"> <g data-y="0">
<g data-x="0"><use href="#hex"/></g> <g data-x="0"><use href="#hex"/></g>
<g data-x="1"><use href="#hex"/></g> <g data-x="1"><use href="#hex"/></g>

Before

Width:  |  Height:  |  Size: 81 KiB

After

Width:  |  Height:  |  Size: 81 KiB

View File

@ -17,7 +17,9 @@ const svgns = "http://www.w3.org/2000/svg",
firingArcVisibility = { firingArcVisibility = {
davion: false, davion: false,
liao: false liao: false
}; },
clippedFiringArcRadius = 100;
let svg; let svg;
@ -37,12 +39,13 @@ function calculateAngle(xDiff, yDiff) {
} }
function edgePoint(x1, y1, x2, y2, maxX, maxY, minX = 0, minY = 0) { function edgePoint(x1, y1, x2, y2, maxX, maxY, minX = 0, minY = 0) {
let pointCoords, const xDiff = x2 - x1,
xDiff = x2 - x1,
yDiff = y2 - y1, yDiff = y2 - y1,
xIntercept = y => (y - y1) * xDiff / yDiff + x1, xIntercept = y => (y - y1) * xDiff / yDiff + x1,
yIntercept = x => (x - x1) * yDiff / xDiff + y1; yIntercept = x => (x - x1) * yDiff / xDiff + y1;
let pointCoords;
if (xDiff > 0 && yDiff > 0) { if (xDiff > 0 && yDiff > 0) {
let x = xIntercept(maxY); let x = xIntercept(maxY);
@ -67,21 +70,28 @@ function edgePoint(x1, y1, x2, y2, maxX, maxY, minX = 0, minY = 0) {
let cornerPoints; let cornerPoints;
function position(e) { function position(e) {
// let activeFiringArc = this.querySelector('polygon.firing-arc.active'); // TODO: handle exactly horizontal and vertical lines?
let activeFiringArc;
let size = this.querySelector('polygon.firing-arc.active').dataset.size;
// TODO: handle exactly horizontal and vertical lines let arcLayer = this.querySelector('#shapes');
const activeFiringArc = arcLayer.querySelector('polygon.firing-arc.active');
if (!activeFiringArc) {
return;
}
let outlineLayer = this.querySelector('#lines');
const { dataset: { allegiance, number }} = activeFiringArc,
outlineSelector = `polyline[data-number="${number}"][data-allegiance="${allegiance}"]`,
activeFiringArcOutline = outlineLayer.querySelector(outlineSelector);
let grid = this.querySelector('.grid'); let grid = this.querySelector('.grid');
let testAim = this.querySelector('#test-arcs line'); let aim = outlineLayer.querySelector('line');
let testOutline = this.querySelector('#test-arcs polyline');
let testArea = this.querySelector('#test-arcs polygon');
let pt = new DOMPoint(e.clientX, e.clientY); let pt = new DOMPoint(e.clientX, e.clientY);
let { x, y } = pt.matrixTransform(grid.getScreenCTM().inverse()); let { x, y } = pt.matrixTransform(grid.getScreenCTM().inverse());
let pivotPt = [x1, y1] = [testAim.getAttribute('x1'), testAim.getAttribute('y1')].map(n => parseFloat(n)); let pivotPt = [x1, y1] = [aim.getAttribute('x1'), aim.getAttribute('y1')].map(n => parseFloat(n));
let [maxX, maxY, minX, minY] = [ let [maxX, maxY, minX, minY] = [
grid.getBBox().x + grid.getBBox().width, grid.getBBox().x + grid.getBBox().width,
grid.getBBox().y + grid.getBBox().height, grid.getBBox().y + grid.getBBox().height,
@ -89,20 +99,14 @@ function position(e) {
grid.getBBox().y grid.getBBox().y
].map(n => parseFloat(n)); ].map(n => parseFloat(n));
let edgePointArgs = [testAim.getAttribute('x1'), testAim.getAttribute('y1'), x, y, maxX, maxY, minX, minY].map(n => parseFloat(n)) let edgePointArgs = [aim.getAttribute('x1'), aim.getAttribute('y1'), x, y, maxX, maxY, minX, minY].map(n => parseFloat(n))
let aimPt = [x2, y2] = edgePoint(...edgePointArgs); let aimPt = [x2, y2] = edgePoint(...edgePointArgs);
testAim.setAttributeNS(null, 'x2', x2); aim.setAttributeNS(null, 'x2', x2);
testAim.setAttributeNS(null, 'y2', y2); aim.setAttributeNS(null, 'y2', y2);
// console.log(testAim);
let angle = calculateAngle(x2 - x1, y2 - y1); let angle = calculateAngle(x2 - x1, y2 - y1);
let arcAngle = arcSize[activeFiringArc.dataset.size];
let arcAngle = arcSize[size];
// let arcAngle = Math.PI / 4;
let distance = Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2); let distance = Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);
let yDelta = distance * Math.cos(angle) * Math.tan(arcAngle); let yDelta = distance * Math.cos(angle) * Math.tan(arcAngle);
let xDelta = distance * Math.sin(angle) * Math.tan(arcAngle); let xDelta = distance * Math.sin(angle) * Math.tan(arcAngle);
@ -113,13 +117,9 @@ function position(e) {
let arcPt1 = [newX1, newY1] = edgePoint(x1, y1, newX1, newY1, maxX, maxY, minX, minY); let arcPt1 = [newX1, newY1] = edgePoint(x1, y1, newX1, newY1, maxX, maxY, minX, minY);
let arcPt2 = [newX2, newY2] = edgePoint(x1, y1, newX2, newY2, maxX, maxY, minX, minY); let arcPt2 = [newX2, newY2] = edgePoint(x1, y1, newX2, newY2, maxX, maxY, minX, minY);
testOutline.setAttributeNS(null, 'points', `${newX1},${newY1} ${x1},${y1} ${newX2},${newY2}`); activeFiringArcOutline.setAttributeNS(null, 'points', `${newX1},${newY1} ${x1},${y1} ${newX2},${newY2}`);
// console.log('area ', testArea.getAttribute('points').split(' ').map(n => n.split(',').map(n => Math.round(parseFloat(n)))).join(' \t'));
// console.log('lines ', testOutline.getAttribute('points').split(' ').map(n => n.split(',').map(n => Math.round(parseFloat(n)))).join(' \t'));
function touchSameEdge([x1, y1], [x2, y2]) { function touchSameEdge([x1, y1], [x2, y2]) {
// console.log('touchSameEdge', x1, y1, x2, y2);
return x1 === x2 || y1 === y2; return x1 === x2 || y1 === y2;
} }
@ -132,192 +132,62 @@ function position(e) {
return x1 === x2 || y1 === y2; return x1 === x2 || y1 === y2;
} }
function findSharedValue(pt, ...pts) {
// which arcpt does aimpt share a value with? // which arcpt does aimpt share a value with?
// if they share an x value, we will look for corner y value // if they share an x value, we will look for corner y value
// if they share a y value, we will look for corner x value // if they share a y value, we will look for corner x value
// is aim pt non-shared value greater or less than arcpt non-shared value? // is aim pt non-shared value greater or less than arcpt non-shared value?
function findSharedValue(pt, ...pts) {
const sharedValPt = pts.find(([ptX, ptY]) => pt.includes(ptX) || pt.includes(ptY)); const sharedValPt = pts.find(([ptX, ptY]) => pt.includes(ptX) || pt.includes(ptY));
let limits = [[minX, maxX], [minY, maxY]];
if (!sharedValPt) { if (!sharedValPt) {
return; return;
} }
const nonSharedValIndex = pt[0] === sharedValPt[0] ? 1 : 0; const limits = [[minX, maxX], [minY, maxY]],
const cornerVal = pt[nonSharedValIndex] < sharedValPt[nonSharedValIndex] ? Math.min(...limits[nonSharedValIndex]) : Math.max(...limits[nonSharedValIndex]); nonSharedValIndex = pt[0] === sharedValPt[0] ? 1 : 0;
let cps = cornerPoints.filter(cp => cp[nonSharedValIndex] === cornerVal);
return cps; let cornerVal;
}
console.log('cornerPoints', cornerPoints); if (pt[nonSharedValIndex] < sharedValPt[nonSharedValIndex]) {
cornerVal = Math.min(...limits[nonSharedValIndex])
if (touchSameEdge(arcPt1, arcPt2)) {
// 0 corner case
console.log('0-corner case');
testArea.setAttributeNS(null, 'points', [pivotPt, arcPt1, arcPt2].join(' '));
} else if (touchOrthogonalEdges(arcPt1, arcPt2)) {
// 1 corner or 3 corner case
if (touchSameEdge(aimPt, arcPt1) || touchSameEdge(aimPt, arcPt2)) {
console.log('1-corner case');
let cp = cornerPoints.find(cp => shareValue(cp, arcPt1) && shareValue(cp, arcPt2));
testArea.setAttributeNS(null, 'points', [pivotPt, arcPt1, cp, arcPt2].join(' '));
} else { } else {
console.log('3-corner case'); cornerVal = Math.max(...limits[nonSharedValIndex]);
console.log('cornerPoints', cornerPoints);
let cps = cornerPoints.filter(cp => !shareValue(cp, arcPt1) || !shareValue(cp, arcPt2));
let index = cps.findIndex(cp => shareValue(cp, arcPt2));
console.log('cps', cps, 'index', index, 'arcPt2', arcPt2, 'arcPt1', arcPt1);
cps.splice(index + 1, 0, arcPt2, pivotPt, arcPt1);
console.log('after splice', cps);
// testArea.setAttributeNS(null, 'points', [pivotPt, arcPt1, cps.join(' '), arcPt2].join(' '));
testArea.setAttributeNS(null, 'points', cps.join(' '));
}
} else {
// 2 corner case
if (touchSameEdge(aimPt, arcPt1) || touchSameEdge(aimPt, arcPt2)) {
console.log('2-corner case, edge shared');
let cps = findSharedValue(aimPt, arcPt1, arcPt2);
let index = cps.findIndex(cp => shareValue(cp, arcPt2));
// console.log('cps', cps, 'index', index, 'arcPt2', arcPt2, 'arcPt1', arcPt1);
cps.splice(index + 1, 0, arcPt2, pivotPt, arcPt1);
// testArea.setAttributeNS(null, 'points', [pivotPt, arcPt1, cps.join(' '), arcPt2].join(' '));
testArea.setAttributeNS(null, 'points', cps.join(' '));
} else {
console.log('2-corner case, all separate edges');
let cps = cornerPoints.filter(cp => shareValue(cp, aimPt) || shareValue(cp, aimPt));
let index = cps.findIndex(cp => shareValue(cp, arcPt2));
cps.splice(index + 1, 0, arcPt2, pivotPt, arcPt1);
// testArea.setAttributeNS(null, 'points', [pivotPt, arcPt1, cps.join(' '), arcPt2].join(' '));
testArea.setAttributeNS(null, 'points', cps.join(' '));
}
} }
// testArea.setAttributeNS(null, 'points', cornerPoints.join(' ')); return cornerPoints.filter(cp => cp[nonSharedValIndex] === cornerVal);
}
if (activeFiringArc) {
let activeFiringArcOutline = this.querySelector(`#lines polygon[data-number="${activeFiringArc.dataset.number}"][data-allegiance="${activeFiringArc.dataset.allegiance}"]`),
board = this.querySelector('#image-maps'),
{ width, height } = board.getBBox(),
pt = new DOMPoint(e.clientX, e.clientY),
{ x: pointerX, y: pointerY } = pt.matrixTransform(board.getScreenCTM().inverse()),
[maxXpx, maxYpx] = [width, height],
{ x: x1px, y: y1px } = activeFiringArc.points[0];
let [x2px, y2px] = [
pointerX / width * maxXpx,
pointerY / height * maxYpx
];
let xDiff = x2px - x1px;
let yDiff = y2px - y1px;
let angle = calculateAngle(xDiff, yDiff);
let arcAngle = arcSize[activeFiringArc.dataset.size];
let distance = Math.sqrt((x2px - x1px) ** 2 + (y2px - y1px) ** 2);
let yDelta = distance * Math.cos(angle) * Math.tan(arcAngle);
let xDelta = distance * Math.sin(angle) * Math.tan(arcAngle);
let [newY1, newX1] = [y2px + yDelta, x2px + xDelta];
let [newY2, newX2] = [y2px - yDelta, x2px - xDelta];
[newX1, newY1] = edgePoint(x1px, y1px, newX1, newY1, maxXpx, maxYpx);
[newX2, newY2] = edgePoint(x1px, y1px, newX2, newY2, maxXpx, maxYpx);
let oppositeEdgeConditions = [
newX1 == 0 && newX2 == maxXpx,
newX2 == 0 && newX1 == maxXpx,
newY1 == 0 && newY2 == maxYpx,
newY2 == 0 && newY1 == maxYpx
]
let orthogonalEdgeConditions = [
(newX1 == 0 || newX1 == maxXpx) && (newY2 == 0 || newY2 == maxYpx),
(newX2 == 0 || newX2 == maxXpx) && (newY1 == 0 || newY1 == maxYpx),
]
let points; let points;
if (oppositeEdgeConditions.some(e => e)) { if (touchSameEdge(arcPt1, arcPt2)) {
let cornerPoints; // 0-corner case
points = [arcPt2, pivotPt, arcPt1];
if (xDiff > 0 && yDiff > 0) { } else if (touchOrthogonalEdges(arcPt1, arcPt2)) {
if ((newY1 == 0 && newY2 == maxYpx) || (newY1 == maxYpx && newY2 == 0)) { if (touchSameEdge(aimPt, arcPt1) || touchSameEdge(aimPt, arcPt2)) {
cornerPoints = [[maxXpx, 0], [maxXpx, maxYpx]]; // 1-corner case
let cp = cornerPoints.find(cp => shareValue(cp, arcPt1) && shareValue(cp, arcPt2));
points = [arcPt2, pivotPt, arcPt1, cp];
} else { } else {
cornerPoints = [[maxXpx, maxYpx], [0, maxYpx]]; // 3-corner case
points = cornerPoints.filter(cp => !shareValue(cp, arcPt1) || !shareValue(cp, arcPt2));
let index = points.findIndex(cp => shareValue(cp, arcPt2));
points.splice(index + 1, 0, arcPt2, pivotPt, arcPt1);
} }
} else if (xDiff > 0 && yDiff < 0) {
if ((newY1 == 0 && newY2 == maxYpx) || (newY1 == maxYpx && newY2 == 0)) {
cornerPoints = [[maxXpx, 0], [maxXpx, maxYpx]];
} else { } else {
cornerPoints = [[0, 0], [maxXpx, 0]]; if (touchSameEdge(aimPt, arcPt1) || touchSameEdge(aimPt, arcPt2)) {
// 2-corner case, aim and an arc point touch the same edge
points = findSharedValue(aimPt, arcPt1, arcPt2);
let index = points.findIndex(cp => shareValue(cp, arcPt2));
points.splice(index + 1, 0, arcPt2, pivotPt, arcPt1);
} else {
// 2-corner case, aim and both arc points all touch different edges
points = cornerPoints.filter(cp => shareValue(cp, aimPt) || shareValue(cp, aimPt));
let index = points.findIndex(cp => shareValue(cp, arcPt2));
points.splice(index + 1, 0, arcPt2, pivotPt, arcPt1);
}
} }
} else if (xDiff < 0 && yDiff > 0) { activeFiringArc.setAttributeNS(null, 'points', points.join(' '));
if ((newY1 == 0 && newY2 == maxYpx) || (newY1 == maxYpx && newY2 == 0)) {
cornerPoints = [[0, maxYpx], [0, 0]];
} else {
cornerPoints = [[maxXpx, maxYpx], [0, maxYpx]];
}
} else {
if ((newY1 == 0 && newY2 == maxYpx) || (newY1 == maxYpx && newY2 == 0)) {
cornerPoints = [[0, maxYpx], [0, 0]];
} else {
cornerPoints = [[0, 0], [maxXpx, 0]];
}
}
points = `${x1px},${y1px} ${newX1},${newY1} ${cornerPoints[1]} ${cornerPoints[0]} ${newX2},${newY2}`;
} else if (orthogonalEdgeConditions.some(e => e)) {
let cornerPoints = [];
let cp1, cp2;
if (newX1 == 0 || newX1 == maxXpx) {
cp1 = [newX1, yDiff > 0 ? maxYpx : 0];
} else {
cp1 = [xDiff > 0 ? maxXpx : 0, newY1];
}
if (newX2 == 0 || newX2 == maxXpx) {
cp2 = [newX2, yDiff > 0 ? maxYpx : 0];
} else {
cp2 = [xDiff > 0 ? maxXpx : 0, newY2];
}
if (cp1[0] == cp2[0] && cp1[1] == cp2[1]) {
cornerPoints.push(cp1);
} else {
cornerPoints.push(cp1);
cornerPoints.push([xDiff > 0 ? maxXpx : 0, yDiff > 0 ? maxYpx : 0])
cornerPoints.push(cp2);
}
points = `${x1px},${y1px} ${newX1},${newY1} ${cornerPoints.join(' ')} ${newX2},${newY2}`;
} else {
points = `${x1px},${y1px} ${newX1},${newY1} ${newX2},${newY2}`;
}
activeFiringArcOutline.setAttributeNS(null, 'points', points);
activeFiringArc.setAttributeNS(null, 'points', points);
}
} }
function setDataAttrs({ dataset: { allegiance, number }}, el) { function setDataAttrs({ dataset: { allegiance, number }}, el) {
@ -342,60 +212,35 @@ export default function (el) {
let arcLayer = svg.querySelector('#shapes'); let arcLayer = svg.querySelector('#shapes');
let outlineLayer = svg.querySelector('#lines'); let outlineLayer = svg.querySelector('#lines');
let arcContainer = svg.querySelector('#firing-arcs'); let arcContainer = svg.querySelector('#firing-arcs');
let grid = svg.querySelector('.grid');
let aim = document.createElementNS(svgns, 'line');
aim.setAttributeNS(null, 'x1', x);
aim.setAttributeNS(null, 'y1', y);
let testArcs = svg.querySelector('#test-arcs'); aim.setAttributeNS(null, 'x2', x);
let testAim = document.createElementNS(svgns, 'line'); aim.setAttributeNS(null, 'y2', y);
testAim.setAttributeNS(null, 'x1', x); outlineLayer.appendChild(aim);
testAim.setAttributeNS(null, 'y1', y);
testAim.setAttributeNS(null, 'x2', x);
testAim.setAttributeNS(null, 'y2', y);
testArcs.appendChild(testAim);
let testOutline = document.createElementNS(svgns, 'polyline');
testOutline.setAttributeNS(null, 'points', `${x},${y} ${x},${y} ${x},${y}`);
testArcs.appendChild(testOutline);
let testGrid = svg.querySelector('.grid');
let gridArea = document.createElementNS(svgns, 'polygon');
cornerPoints = (function () { cornerPoints = (function () {
let { x, y, width, height } = testGrid.getBBox(); let { x, y, width, height } = grid.getBBox();
return [[x, y], [x + width, y], [x + width, y + height], [x, y + height]]; return [[x, y], [x + width, y], [x + width, y + height], [x, y + height]];
})(); })();
gridArea.setAttributeNS(null, 'points', cornerPoints.join(' '));
testArcs.appendChild(gridArea);
// console.log(testArcs);
let grid = svg.querySelector('.board');
const transform = getComputedStyle(grid).transform.match(/-?\d+\.?\d*/g);
const pt = new DOMPoint(x, y);
const mtx = new DOMMatrix(transform);
let tPt = pt.matrixTransform(mtx);
let pivotPoint = [tPt.x, tPt.y];
let firingArc = document.createElementNS(svgns, 'polygon'); let firingArc = document.createElementNS(svgns, 'polygon');
let firingArcOutline = document.createElementNS(svgns, 'polygon'); let firingArcOutline = document.createElementNS(svgns, 'polyline');
setDataAttrs(counter, firingArc); setDataAttrs(counter, firingArc);
firingArc.dataset.size = size; firingArc.dataset.size = size;
firingArc.classList.add('firing-arc', 'active'); firingArc.classList.add('firing-arc', 'active');
firingArc.setAttributeNS(null, 'points', `${pivotPoint} ${pivotPoint} ${pivotPoint}`); firingArc.setAttributeNS(null, 'points', `${x},${y} ${x},${y} ${x},${y}`);
setDataAttrs(counter, firingArcOutline); setDataAttrs(counter, firingArcOutline);
firingArcOutline.setAttributeNS(null, 'points', `${pivotPoint} ${pivotPoint} ${pivotPoint}`); firingArcOutline.setAttributeNS(null, 'points', `${x},${y} ${x},${y} ${x},${y}`);
let clipShape = document.createElementNS(svgns, 'circle'); let clipShape = document.createElementNS(svgns, 'circle');
clipShape.setAttributeNS(null, 'cx', tPt.x); clipShape.setAttributeNS(null, 'cx', x);
clipShape.setAttributeNS(null, 'cy', tPt.y); clipShape.setAttributeNS(null, 'cy', y);
clipShape.setAttributeNS(null, 'r', 100); clipShape.setAttributeNS(null, 'r', clippedFiringArcRadius);
let clipPath = document.createElementNS(svgns, 'clipPath'); let clipPath = document.createElementNS(svgns, 'clipPath');
setDataAttrs(counter, clipPath); setDataAttrs(counter, clipPath);
@ -438,7 +283,7 @@ export default function (el) {
}; };
this.get = function ({ dataset: { allegiance, number }}) { this.get = function ({ dataset: { allegiance, number }}) {
return svg.querySelectorAll(`#firing-arcs polygon[data-number="${number}"][data-allegiance="${allegiance}"]`); return svg.querySelectorAll(`#firing-arcs [data-number="${number}"][data-allegiance="${allegiance}"], #firing-arcs line`);
}; };
this.toggleVisibility = function (allegiance) { this.toggleVisibility = function (allegiance) {