gradientspace / geometry3Sharp

C# library for 2D/3D geometric computation, mesh algorithms, and so on. Boost license.
http://www.gradientspace.com
Boost Software License 1.0
1.71k stars 384 forks source link

Subtraction of polygon #114

Open stefanoromanello opened 5 years ago

stefanoromanello commented 5 years ago

Hi, is it possible from 2 polygon obtain the result of the subtraction (se photo)? Orange is the first polygon, green is the second one. I want to do orange-green and blu is the result. from to

CBenghi commented 4 years ago

Hi @stefanoromanello,

it's been a while since you asked, but if you could not find this in the library you might want to look at gl_tessellation, if you are looking for something 2d.

You could start from: http://www.songho.ca/opengl/gl_tessellation.html

Best, Claudio

rms80 commented 4 years ago

you could try TriangulatedPolygonGenerator.cs, it uses a GeneralPolygon2d which is a polygon-with-holes, so I think it handles holes (but I can't remember).

CBenghi commented 4 years ago

thanks @rms80,

I've tried following your advice and I could write and test some code to get the results expected.

The code is quite simple:

TriangulatedPolygonGenerator tg = new TriangulatedPolygonGenerator();
tg.Polygon = new GeneralPolygon2d(new Polygon2d(
    new List<Vector2d>()
    {
        new Vector2d(0,0),
        new Vector2d(0,4),
        new Vector2d(5,4),
        new Vector2d(5,0)
    }
    ));
tg.Polygon.AddHole(new Polygon2d(
    new List<Vector2d>()
    {
        new Vector2d(1,1),
        new Vector2d(2,1),
        new Vector2d(1,2)
    }
    )); 

@stefanoromanello, have a look at a working sample at

https://github.com/CBenghi/geometry3SharpDemos/blob/51bcd57a9a5233579660ad1420dcd208396ed075/geometry3Test/test_MeshGen.cs#L78

image