Skip to content

Instantly share code, notes, and snippets.

@EduardoJM
Created July 11, 2020 18:07
Show Gist options
  • Save EduardoJM/0278ca62fbc4f27e58607a5839697831 to your computer and use it in GitHub Desktop.
Save EduardoJM/0278ca62fbc4f27e58607a5839697831 to your computer and use it in GitHub Desktop.
Separating Axis Theorem Polygon in Axis Projection
polygon.projectInAxis = function(x, y) {
let min = 10000000000;
let max = -10000000000;
for (let i = 0; i < polygon.vertices.length; i++) {
let px = polygon.vertices[i].x;
let py = polygon.vertices[i].y;
var projection = (px * x + py * y) / (Math.sqrt(x * x + y * y));
if (projection > max) {
max = projection;
}
if (projection < min) {
min = projection;
}
}
return { min, max };
};
@EduardoJM
Copy link
Author

Thanks for your comment. Is most correct to use this way with MAX and MIN instead of some larger number (my original and fast created solution).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment