Use a class instead of changing direct styles

This commit is contained in:
2025-12-18 15:32:27 -08:00
parent c3bd622841
commit 7ad55c07ae

View File

@@ -38,10 +38,15 @@
} }
#lines polygon { #lines polygon {
fill: white;
stroke: teal;
fill-opacity: 0.2; fill-opacity: 0.2;
stroke-width: 0.5px; stroke-width: 0.5px;
fill: none;
stroke: none;
}
#lines polygon.clockwise-orientation {
fill: white;
stroke: red;
} }
</style> </style>
@@ -146,17 +151,27 @@
function updateTriangles([positionX, positionY]) { function updateTriangles([positionX, positionY]) {
const delim = ' '; const delim = ' ';
const className = 'clockwise-orientation';
triangles.forEach(triangle => { triangles.forEach(triangle => {
const attr = triangle.getAttribute('points').split(delim) const attr = triangle.getAttribute('points').split(delim);
const [[xa, ya], [xb, yb], [xc, yc]] = attr.map(t => t.split(',').map(n => +n)); const [[xa, ya], [xb, yb], [xc, yc]] = attr.map(t => t.split(',').map(n => +n));
const det = (xb - xa) * (yc - ya) - (xc - xa) * (yb - ya); const det = (xb - xa) * (yc - ya) - (xc - xa) * (yb - ya);
attr.pop(); const pos = `${positionX},${positionY}`;
attr.push(`${positionX},${positionY}`); const list = triangle.classList;
const clockwiseOrientation = det < 0;
if (pos !== attr.pop()) {
attr.push(pos);
triangle.setAttribute('points', attr.join(delim)); triangle.setAttribute('points', attr.join(delim));
triangle.style.fill = det < 0 ? "white" : "none"; }
triangle.style.stroke = det < 0 ? "red" : "none";
clockwiseOrientation ? list.add(className) : list.remove(className);
// i think i can also discard obtuse triangles? // i think i can also discard obtuse triangles?
// if all the triangles are obtuse, i only need to check the nearest corner for a collision
// otherwise, i need to check only the acute triangles
}); });
} }

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 15 KiB