Implementation of GJK (Gilbert-Johnson-Keerthi) algorithm for calculation distance between convex polygons and intersection detection.
var gjk = require('gjk');
var polygon = [
{x: 1, y: 1},
{x: 2, y: 1},
{x: 2, y: 2},
{x: 1, y: 2}
];
var notIntersectedPolygon = [
{x: 3, y: 1},
{x: 4, y: 1},
{x: 4, y: 2},
{x: 3, y: 2}
];
var intersectedPolygon = [
{x: 1.5, y: 1},
{x: 4, y: 1},
{x: 4, y: 2},
{x: 1.5, y: 2}
]
var distance = gjk.distance(polygon, notIntersectedPolygon); //1
distance = gjk.distance(polygon, notIntersectedPolygon); //0
var intersected = gjk.intersect(polygon, notIntersectedPolygon); //false
intersected = gjk.intersect(polygon, intersectedPolygon); //true
Polygons might be one of the next formats
var polygon = [
{x: 1, y: 1},
{x: 2, y: 1},
{x: 2, y: 2},
{x: 1, y: 2}
];
var polygon = [
1, 1,
2, 1,
2, 2,
1, 2
];
var polygon = [
[1, 1],
[2, 1],
[2, 2],
[1, 2]
];