Created
April 20, 2018 19:22
-
-
Save abeforgit/e3170504dd1a16ff3738d07c1a65dc4b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const compare = (a, b) => { | |
return a.x === b.x && a.y === b.y; | |
}; | |
const distance = (a, b) => { | |
return Math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) ** 2); | |
}; | |
const draaien = (a, b, c) => { | |
if (compare(a, b) || compare(b, c) || compare(a, c)) { | |
throw "AssertionError: drie punten moeten verschillend zijn"; | |
} | |
let calc = ((b.x - a.x) * (c.y - a.y)) - ((b.y - a.y) * (c.x - a.x)); | |
if (!calc) { | |
return calc; | |
} else { | |
return Math.abs(calc) / calc; | |
} | |
}; | |
const volgende = (point, pointset, flag) => { | |
let current = undefined; | |
let check = 1; | |
if (!flag) { | |
check = -1; | |
} | |
pointset.filter((o) => !compare(o, point)).forEach( | |
(p) => { | |
if (!current) { | |
current = p; | |
} else if (draaien(point, current, p) === check) { | |
current = p; | |
} else if (draaien(point, current, p) === 0) { | |
if (distance(point, current) < distance(point, p)) { | |
current = p; | |
} | |
} | |
}); | |
return current; | |
}; | |
const contour = (pointset, flag) => { | |
let first = pointset.reduce((prev, cur) => { | |
if (prev.x === cur.x) { | |
if (cur.y < prev.y) { | |
return cur; | |
} else { | |
return prev; | |
} | |
} else { | |
if (cur.x < prev.x) { | |
return cur; | |
} | |
return prev; | |
} | |
}); | |
let rslt = [first]; | |
let next = volgende(first, pointset, flag); | |
while (!compare(next, first)) { | |
rslt.push(next); | |
next = volgende(next, pointset, flag); | |
} | |
return rslt; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment