How to break a sprite into shapes with script

So I’m trying to make a sprite break into pieces with a script and I’ve found this post that cuts the original sprite into a bunch of squares. I would like to do this with any polygon.

In this picture the first example is what the script in the post could do, but I would like to be able to cut it into a shape such as a triangle like in the second example. How can I go about doing this?