Last active
February 10, 2018 09:30
-
-
Save wottpal/2e33941c546be3b02fabe79d1634cd9c to your computer and use it in GitHub Desktop.
* numberOfPoints defines amount of points
* respects frameWidth
* returns array of triangles in the format [ [[x1, y1],[x2, y2],[x3, y3]], ... ]
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
function triangulateCanvas() { | |
let points = [], triangles = [] | |
for (let i = 0; i < numberOfPoints; i++) { | |
const randomX = random(frameWidth, WIDTH-frameWidth) | |
const randomY = random(frameWidth, HEIGHT-frameWidth) | |
points.push([ | |
constrain(randomX, frameWidth, WIDTH-frameWidth), | |
constrain(randomY, frameWidth, HEIGHT-frameWidth) | |
]) | |
} | |
const d = new Delaunator(points) | |
for (var i = 0; i < d.triangles.length; i += 3) { | |
triangles.push([ | |
points[d.triangles[i]], | |
points[d.triangles[i + 1]], | |
points[d.triangles[i + 2]] | |
]) | |
} | |
return triangles | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment