faiface / pixel

A hand-crafted 2D game library in Go
MIT License
4.46k stars 245 forks source link

Use IMDraw to create a shape with a hole #227

Closed mr-panta closed 4 years ago

mr-panta commented 4 years ago

Hi contributors,

I'm new for this library and like to know if there is a way to use IMDraw to create a shape with a hole inside like the given images?

63HcV fillrule-evenodd ref: https://stackoverflow.com/questions/3742479/how-to-cut-a-hole-in-an-svg-rectangle

Thank you

pentaphobe commented 4 years ago

You could use image masking in a shader, but that's a fairly limiting scenario & doesn't seem like what you want

So - not as far as I'm aware, so you'd have to implement some form of constructive geometry

IIRC there are tonnes of implementations of the basic algorithms which would be a breeze to port, try searching for the algorithm names in the above wikipedia article. Failing that, there may be another library which better suits your needs

bcvery1 commented 4 years ago

This was mentioned on the discord, but posting here for visibility, check if the Lights example provides the functionality you need: https://github.com/faiface/pixel-examples/tree/master/lights

gremour commented 4 years ago

You can draw this as a polygon. Imagine a cut between top left point of rectangle and top left point of triangle. This cut creates 2 more edges and you get closed polygon.

If you need complex things like dynamic hole rotation, this will be more tricky. Boils down to triangulation algorithm.