Skip to content

RayzRazko/gjk-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gjk

Implementation of GJK (Gilbert-Johnson-Keerthi) algorithm for calculation distance between convex polygons and intersection detection.

Basic usage

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

Polygons might be one of the next formats

Array of objects

var polygon = [
    {x: 1, y: 1},
    {x: 2, y: 1},
    {x: 2, y: 2},
    {x: 1, y: 2}
];

Array of numbers

var polygon = [
    1, 1,
    2, 1,
    2, 2,
    1, 2
];

Array of arrays of numbers

var polygon = [
    [1, 1],
    [2, 1],
    [2, 2],
    [1, 2]
];

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published