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 };
};
@jayantbh
Copy link

jayantbh commented Oct 4, 2020

Could do:

let min = Number.MIN_SAFE_INTEGER;
let max = Number.MAX_SAFE_INTEGER;

@EduardoJM
Copy link
Author

EduardoJM commented Oct 4, 2020

Could do:

let min = Number.MIN_SAFE_INTEGER;
let max = Number.MAX_SAFE_INTEGER;

Yes, but changing the order (the min is initialized as max and max initialized as min for correct min\max set in the vertices loop), then the code is:

let min = Number.MAX_SAFE_INTEGER;
let max = Number.MIN_SAFE_INTEGER;

@jayantbh
Copy link

jayantbh commented Oct 4, 2020

Whoops. You're right!

@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