SixLabors / ImageSharp.Drawing

:pen: Extensions to ImageSharp containing a cross-platform 2D polygon manipulation API and drawing operations.
https://sixlabors.com/products/imagesharp-drawing/
Other
282 stars 38 forks source link

Convenience constructors for shapes #100

Closed antonfirsov closed 1 year ago

antonfirsov commented 3 years ago

Given a PointF arrays contour and hole, this is how a ComplexPolygon could be constructed today:

ComplexPolygon polygon = new ComplexPolygon(
                new Path(new LinearLineSegment(contour)),
                new Path(new LinearLineSegment(hole)));

It should be as easy as:

ComplexPolygon polygon = new ComplexPolygon(contour, hole);

We need to review the shape types and add convenience constructors based on the principle of "good defaults".

JimBobSquarePants commented 3 years ago

@antonfirsov I'm assuming we'd accept a Span<PointF> for each parameter?

antonfirsov commented 3 years ago

Hah, this seems to be an easy question but it isn't.

Currently the LinearLineSegment(PointF[]) constructor takes ownership of the array without copying, this makes sense since it avoids an allocation and a copy for the user, but note that with Span (or rather ReadOnlySpan) the constructor is forced to do a copy. Do we want the Span and the ROS constructor to behave differently? If most users will fill these things from arrays anyways, maybe we just want to keep these API-s oldschool, and array-only?

JimBobSquarePants commented 3 years ago

Old school it is then!

JimBobSquarePants commented 1 year ago

It looks like we've covered this now. Closing

antonfirsov commented 1 year ago

I didn't mean this just for ComplexPolygon. Path and Polygon could use a similar constructors.

JimBobSquarePants commented 1 year ago

I had a look at those but couldn’t see a way to improve them. Happy to hear suggestions though! 🙂

antonfirsov commented 1 year ago

I expect most real-life paths and polygons to be created given a list of points, yet there is no way for feeding those points directly to Path and Polygon constructors. A user needs to create LinearLineSegment around them first. We need Path(PointF[]) and Polygon(PointF[]) constructors.