diff --git a/src/load_distribution/load_distribution.py b/src/load_distribution/load_distribution.py index 9f30871..48231f4 100644 --- a/src/load_distribution/load_distribution.py +++ b/src/load_distribution/load_distribution.py @@ -183,18 +183,27 @@ def singularities_to_polygon(los: list[Singularity], xy: bool = False) -> Polygo prev_x = None n = None # Create a list of the x-ordinates required - # Always starts on 0.0 - x_acc.append(0.0) - for idx, sing in enumerate(sorted_sings): + if len(sorted_sings) == 1: + sing = sorted_sings[0] n = sing.precision - eps = 10 ** (-2 * n) - if prev_x != sing.x0 and prev_x is not None: - x_acc.append(prev_x + eps) - if prev_x is not None and not math.isclose(prev_x, sing.x0): - x_acc.append(sing.x0) + eps = 10 ** (-2 * n - 1) + x_acc.append(sing.x0) x_acc.append(sing.x0 + eps) x_acc.append(sing.x1 - eps) - prev_x = sing.x1 + x_acc.append(sing.x1) + else: + # Always starts on 0.0 + x_acc.append(0.0) + for idx, sing in enumerate(sorted_sings): + n = sing.precision + eps = 10 ** (-2 * n) + if prev_x != sing.x0 and prev_x is not None: + x_acc.append(prev_x + eps) + if prev_x is not None and not math.isclose(prev_x, sing.x0): + x_acc.append(sing.x0) + x_acc.append(sing.x0 + eps) + x_acc.append(sing.x1 - 10 * eps) + prev_x = sing.x1 # There are two scenarios: sing functions that describe trapezoids/triangles # and sing functions that describe step functions (rectangles). To ensure @@ -203,6 +212,7 @@ def singularities_to_polygon(los: list[Singularity], xy: bool = False) -> Polygo # duplicate x-ordinates. The goal is to have the minimum amount to describe the # required shape, even if that means the exact x value is omitted (because we are # keeping the value immediately to the left and immediately to the right instead). + print(f"{x_acc=}") x_acc = sorted(list(set(x_acc))) x_ord_count = Counter([round(x, 6) for x in x_acc]) to_filter = [] diff --git a/tests/test_load_distribution.py b/tests/test_load_distribution.py index 7707697..fa2a46a 100644 --- a/tests/test_load_distribution.py +++ b/tests/test_load_distribution.py @@ -196,6 +196,12 @@ def test_singularities_to_polygon(): ).wkt == "POLYGON ((0 0, 0 10, 2 10, 2 4, 4 4, 4 8, 8 8, 8 10, 10 10, 10 0, 0 0))" ) + assert ( + ld.singularities_to_polygon( + [ld.Singularity(x0=1.0, x1=3.0, m=0.0, y0=10.0, precision=6, eps=1e-12)] + ).wkt + == "POLYGON ((1 0, 1 10, 3 10, 3 0, 1 0))" + ) def test_overlap_region_to_singularity():