Closing shapes at non-endpoints
In CAD systems this is done


On user side

find intersection of two curves
split curves by their intersection
make closed path to form a polygon
fill polygon
you can still group separated line into one line and operate with it as previously, change in internal logic (math core side) will be hidden from you

On math core side

choose appropriate method of intersection of curves based on the type of the curves
do intersection either by exact intersection if lines types support this (eg straight lines) or by numerical method (usually iterative) in other cases (eg when you have splines underlying)
create new curves based on existent edges of lines and on the found intersection point
an so on