Skip to content

Instantly share code, notes, and snippets.

@EduardoJM
Created July 11, 2020 17:55
Show Gist options
  • Save EduardoJM/0d7e1fbde695c445bb65c3b2db0c8653 to your computer and use it in GitHub Desktop.
Save EduardoJM/0d7e1fbde695c445bb65c3b2db0c8653 to your computer and use it in GitHub Desktop.
Separating Axis Theorem Helper Functions
function buildEdges(vertices) {
const edges = [];
if (vertices.length < 3) {
console.error("Only polygons supported.");
return edges;
}
for (let i = 0; i < vertices.length; i++) {
const a = vertices[i];
let b = vertices[0];
if (i + 1 < vertices.length) {
b = vertices[i + 1];
}
edges.push({
x: (b.x - a.x),
y: (b.y - a.y),
});
}
return edges;
}
function intervalDistance(minA, maxA, minB, maxB) {
if (minA < minB) {
return (minB - maxA);
}
return (minA - maxB);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment