Maybe this “Ramer–Douglas–Peucker Algorithm” sketch from this topic below which I’ve converted to many versions in different Processing language flavors, including Java Mode, p5js, Python Mode and recently PyScript + p5js:
This is my most recent post there, w/ links to both PyScript & p5js versions: