Only render acute triangles

This commit is contained in:
2025-12-19 14:50:09 -08:00
parent 7b63f0524c
commit fcbae33d51

View File

@@ -31,6 +31,16 @@
fill: yellow; fill: yellow;
} }
#frames {
padding: 1px;
font-size: 4pt;
font-family: courier;
}
#lines {
opacity: 0.5;
}
#lines line { #lines line {
stroke: limegreen; stroke: limegreen;
stroke-width: 0.5px; stroke-width: 0.5px;
@@ -54,8 +64,8 @@
<g> <g>
<g class="hitbox"> <g class="hitbox">
<g class="ship"> <g class="ship">
<line x1="0" y1="0" x2="0" y2="8" stroke="black"/> <line id="cannon" x1="0" y1="0" x2="0" y2="8" stroke="black"/>
<circle cx="0" cy="0" r="5"/> <circle id="body" cx="0" cy="0" r="5"/>
</g> </g>
</g> </g>
@@ -101,7 +111,7 @@
const fps = document.querySelector("#fps"); const fps = document.querySelector("#fps");
const info = document.querySelector("#debug"); const info = document.querySelector("#debug");
const ship = document.querySelector(".ship"); const ship = document.querySelector(".ship");
const gun = ship.querySelector('line'); const gun = ship.querySelector('#cannon');
const shipBody = ship.querySelector("circle"); const shipBody = ship.querySelector("circle");
const hitbox = document.querySelector(".hitbox"); const hitbox = document.querySelector(".hitbox");
@@ -157,6 +167,7 @@
const delim = ' '; const delim = ' ';
const className = 'clockwise-orientation'; const className = 'clockwise-orientation';
const visible = []; const visible = [];
const PI = Math.PI / 2;
triangles.forEach(t => { triangles.forEach(t => {
const attr = t.getAttribute('points').split(delim); const attr = t.getAttribute('points').split(delim);
@@ -169,14 +180,28 @@
const pos = `${positionX},${positionY}`; const pos = `${positionX},${positionY}`;
const cwOrientation = det < 0; const cwOrientation = det < 0;
const isClockwise = det < 0; const isClockwise = det < 0;
let isAcute = false;
if (isClockwise) {
const [[ax, ay], [bx, by], [shipx, shipy]] =
t.getAttribute('points').split(' ').map(n => n.split(',').map(n => +n));
const da = distance(ax, ay, shipx, shipy);
const db = distance(bx, by, shipx, shipy);
const dc = distance(ax, ay, bx, by);
const alpha = Math.acos((db**2 + dc**2 - da**2) / (2 * db * dc));
const beta = Math.acos((da**2 + dc**2 - db**2) / (2 * da * dc));
isAcute = alpha < PI && beta < PI;
}
if (pos !== attr.pop()) { if (pos !== attr.pop()) {
attr.push(pos); attr.push(pos);
t.setAttribute('points', attr.join(delim)); t.setAttribute('points', attr.join(delim));
} }
t.classList[isClockwise ? "add" : "remove"](className); t.classList[isClockwise && isAcute ? "add" : "remove"](className);
if (isClockwise) visible.push(t); if (isClockwise && isAcute) visible.push(t);
// return isClockwise ? [t, ...acc] : acc; // return isClockwise ? [t, ...acc] : acc;
// i think i can also discard obtuse triangles? // i think i can also discard obtuse triangles?
@@ -319,7 +344,6 @@
frameCount++; frameCount++;
} }
let degrees = getRotate(gun); let degrees = getRotate(gun);
if (rotate > 0) gun.style.transform = `rotate(${(+degrees + rotationSpeed * elapsed) % 360}deg)`; if (rotate > 0) gun.style.transform = `rotate(${(+degrees + rotationSpeed * elapsed) % 360}deg)`;

Before

Width:  |  Height:  |  Size: 18 KiB

After

Width:  |  Height:  |  Size: 19 KiB