jbuckmccready / cavalier_contours

2D polyline/shape library for offsetting, combining, etc.
Apache License 2.0
151 stars 13 forks source link

Hatching a polygon #37

Open zdila opened 10 months ago

zdila commented 10 months ago

I am trying to use this library also for hatching a polygon. With polygons without holes it works - for each hatching line I find intersections with a polygon and draw the segments. My initial code can be found at https://github.com/FreemapSlovakia/rustmap/blob/215c1c6c6929f16eef74b8aea5164c7cb6254ab7/src/draw.rs#L38 (disclaimer - it is work in progress and I am very new to Rust).

Unfortunately I don't know how to do it with polygons containing holes. I tried to search for Polygon struct that is capable to hold also holes but was not successful. Could you please help me with this?

jbuckmccready commented 10 months ago

For what you want to do with hatching I think you can do the following: find intersects with all the polylines (surrounding and holes) and then for each line order the intersect points by their position along the line and for each pair of points test if the midpoint between them is inside the hole it intersected with, if it is inside the hole discard it, otherwise keep it. You can test if the midpoint is inside a polyline by using the winding_number (method here), if the winding number is non-zero then the point is inside the hole polyline. The test is needed is to catch case when there is only single intersect with a hole otherwise you could just use all the lines (i1, i2), (i3, i4), (i5, i6), ....

NOTE: there may be a better way to do this, this is just first thing that came to mind that is pretty straight forward.

See the following image for visual (i1, i2, i3, ... are the intersect points ordered along the line).

image