Changeset 18341 in josm for trunk


Ignore:
Timestamp:
2021-12-20T17:10:59+01:00 (2 years ago)
Author:
Don-vip
Message:

fix #21646 - Round new points in imagery.Shape (patch by taylor.smock)

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/imagery/Shape.java

    r17727 r18341  
    8686    }
    8787
     88    /**
     89     * Check if the coordinates are inside this shape.
     90     * @see Polygon#contains(int, int)
     91     * @param latlon The latlon to look for
     92     * @return {@code true} if the LatLon is inside the shape.
     93     */
    8894    public boolean contains(LatLon latlon) {
    8995        return coords.contains(
     
    115121
    116122        coords.addPoint(
    117                 (int) (lon * LatLon.MAX_SERVER_INV_PRECISION),
    118                 (int) (lat * LatLon.MAX_SERVER_INV_PRECISION));
     123                (int) Math.round((lon * LatLon.MAX_SERVER_INV_PRECISION)),
     124                (int) Math.round((lat * LatLon.MAX_SERVER_INV_PRECISION)));
    119125    }
    120126
  • trunk/test/unit/org/openstreetmap/josm/data/imagery/ShapeTest.java

    r17275 r18341  
    22package org.openstreetmap.josm.data.imagery;
    33
     4import static org.junit.jupiter.api.Assertions.assertAll;
    45import static org.junit.jupiter.api.Assertions.assertEquals;
    56
     
    78
    89import org.junit.jupiter.api.Test;
     10import org.junit.jupiter.params.ParameterizedTest;
     11import org.junit.jupiter.params.provider.ValueSource;
    912
    1013/**
     
    2932        assertEquals("47.1,11.1,47.2,11.2,47.3,11.3;47.1,11.1,47.2,11.2,47.3,11.3", Shape.encodeAsString(Arrays.asList(shape, shape)));
    3033    }
     34
     35    /**
     36     * Check double edge cases
     37     * @param coordinate the coordinate to check
     38     */
     39    @ParameterizedTest
     40    @ValueSource(doubles = {
     41            // The double representation of 0.2575799 * 1e7 is 2575798.9999999995. Directly casting to int will round down.
     42            0.2575799,
     43            // Check that 2575798.0000000005 is rounded down
     44            0.2575798
     45    })
     46    void testDoubleEdgeCases(final double coordinate) {
     47        final Shape shape = new Shape();
     48        shape.addPoint(Double.toString(1), Double.toString(coordinate));
     49        shape.addPoint(Double.toString(coordinate), Double.toString(1));
     50        shape.addPoint(Double.toString(coordinate), Double.toString(coordinate));
     51        assertAll("Coordinates are not properly rounded on entry",
     52                () -> assertEquals(coordinate, shape.getPoints().get(0).getLon()),
     53                () -> assertEquals(coordinate, shape.getPoints().get(1).getLat()));
     54    }
    3155}
Note: See TracChangeset for help on using the changeset viewer.