@Test
public void testDistanceWithinPolygon() {

    // Testing polygon
    final double[] lons = { 2.333523, 2.333432, 2.333833, 2.333983, 2.334815, 2.334766 };
    final double[] lats = { 48.823778, 48.824091, 48.82389, 48.824165, 48.824232, 48.82384 };
    OsmNogoPolygon polygon = new OsmNogoPolygon(true);
    for (int i = 0; i < lons.length; i++) {
        polygon.addVertex(toOsmLon(lons[i], 0), toOsmLat(lats[i], 0));
    }
    OsmNogoPolygon polyline = new OsmNogoPolygon(false);
    for (int i = 0; i < lons.length; i++) {
        polyline.addVertex(toOsmLon(lons[i], 0), toOsmLat(lats[i], 0));
    }
    // Check with a segment with a single intersection with the polygon
    int lon1 = toOsmLon(2.33308732509613, 0);
    int lat1 = toOsmLat(48.8238790443901, 0);
    int lon2 = toOsmLon(2.33378201723099, 0);
    int lat2 = toOsmLat(48.8239585098974, 0);
    assertEquals(
      "Should give the correct length for a segment with a single intersection",
      17.5,
      polygon.distanceWithinPolygon(lon1, lat1, lon2, lat2),
      0.05 * 17.5
    );
    // Check with a segment crossing multiple times the polygon
    lon2 = toOsmLon(2.33488172292709, 0);
    lat2 = toOsmLat(48.8240891862353, 0);
    assertEquals(
      "Should give the correct length for a segment with multiple intersections",
      85,
      polygon.distanceWithinPolygon(lon1, lat1, lon2, lat2),
      0.05 * 85
    );
    // Check that it works when a point is within the polygon
    lon2 = toOsmLon(2.33433187007904, 0);
    lat2 = toOsmLat(48.8240238480664, 0);
    assertEquals(
      "Should give the correct length when last point is within the polygon",
      50,
      polygon.distanceWithinPolygon(lon1, lat1, lon2, lat2),
      0.05 * 50
    );
    lon1 = toOsmLon(2.33433187007904, 0);
    lat1 = toOsmLat(48.8240238480664, 0);
    lon2 = toOsmLon(2.33488172292709, 0);
    lat2 = toOsmLat(48.8240891862353, 0);
    assertEquals(
      "Should give the correct length when first point is within the polygon",
      35,
      polygon.distanceWithinPolygon(lon1, lat1, lon2, lat2),
      0.05 * 35
    );
    lon1 = toOsmLon(2.333523, 0);
    lat1 = toOsmLat(48.823778, 0);
    lon2 = toOsmLon(2.333432, 0);
    lat2 = toOsmLat(48.824091, 0);
    assertEquals(
      "Should give the correct length if the segment overlaps with an edge of the polygon",
      CheapRuler.distance(lon1, lat1, lon2, lat2),
      polygon.distanceWithinPolygon(lon1, lat1, lon2, lat2),
      0.05 * CheapRuler.distance(lon1, lat1, lon2, lat2)
    );
    lon1 = toOsmLon(2.333523, 0);
    lat1 = toOsmLat(48.823778, 0);
    lon2 = toOsmLon(2.3334775, 0);
    lat2 = toOsmLat(48.8239345, 0);
    assertEquals(
      "Should give the correct length if the segment overlaps with a polyline",
      CheapRuler.distance(lon1, lat1, lon2, lat2),
      polyline.distanceWithinPolygon(lon1, lat1, lon2, lat2),
      0.05 * CheapRuler.distance(lon1, lat1, lon2, lat2)
    );
}