Created
July 11, 2020 18:07
-
-
Save EduardoJM/0278ca62fbc4f27e58607a5839697831 to your computer and use it in GitHub Desktop.
Separating Axis Theorem Polygon in Axis Projection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | |
}; |
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;
Whoops. You're right!
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
Could do: