How to copy an image in a set of vertices

How can I copy an image that’s given in say a set of 25 points? I have used image:copy which uses a box between two points but I need a more specific outline for copying as not all shapes are squares. The only way I can think of is to use a sort of flood fill or scan line to capture all of the pixels in the one image given in the set of points and set the pixels of a new image that’s the same size as the bounding box of the polygon given as the set of points.

Can’t you make it square and use transparent pixels in the non-image space?

Or do I not understand the question?

I have that you need, but this is in beta version for the moment (because of parameters are not all able and code is dirty).

https://gist.github.com/HyroVitalyProtago/9a6101389343fa1843b9

If you want the set of 25 points, use `Physics.getPoints(src)` where src is the location of image.

If this doesn’t good work, it’s because of `Physics.binaries` function.
try to replace `if r > 0 then` by `if r > moyenne then` or try values [0-255].

You’re on the right track @spacemonkey, I want to be able to find all the pixels in the area of a polygon with for example 25 points

Sorry, i’ve thought that you search to transform an image in array of points of this contour. Go back home xP.

Way back in the day I remember hacking the wolfenstein 3d wad files. They encoded non square sprites in an interesting way. Say your overall bounding box is 100x100 pixels it would first encode 100 pairs of values being the first pixel and last pixel in a row being a position from 1-100 for each value. Then it would have a continuous stream of color bytes that you would fill left to right row by row within the bounding offsets for each row.

Not sure this would be performant in lua though…

Also, that doesn’t encode well if the shape has convex cuttouts in the vertical axis…

Haha nah it’s all good, I did give your code a good read, could be really damn useful for a lot of other things though

@Luatee - I’ve done a bit of this. I think there are some distinct steps

1. Draw a line round all the points to make a closed shape, onto an image in memory, using setContext
2. Loop through the pixel values, storing the lowest and highest x and y values with non blank pixels
3. Copy this part of the image

You can do item 1 by

1. Calculating the weighted centre of the points.
2. Translating there.
3. Calculating the angle of each point relative to the weighted centre.
4. Sorting by angle.
5. Drawing lines from one point to the next

Actually, my step 2, the bounding box, can be calculated from the set of points, no need to look at pixels