Leaflet.glify icon indicating copy to clipboard operation
Leaflet.glify copied to clipboard

Fix hole to non-hole vertex border draw bug

Open melgrove opened this issue 2 years ago • 3 comments

Discussion

Polygons with holes draw a border between the last and first vertices of consequent holes. This is because lines are getting drawn between every vertex in a polygon. This makes polygons with many holes have many border lines connecting all the holes. This addition uses the earcut flat.holes array to skip drawing a border between the last outer polygon vertex and the first hole vertex, as well as the last vertex of any hole and the first vertex of the next hole.

Example

The simple polygon with a hole below can be used an as example. Without this PR, there is a line between one of the corners of the polygon to one of the corners of the hole. With the PR that line is not drawn.

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              6.767578125,
              46.619261036171515
            ],
            [
              13.447265624999998,
              46.619261036171515
            ],
            [
              13.447265624999998,
              51.12421275782688
            ],
            [
              6.767578125,
              51.12421275782688
            ],
            [
              6.767578125,
              46.619261036171515
            ]
          ],
          [[
              7.998046875,
              47.45780853075031
            ],
            [
              12.392578125,
              47.45780853075031
            ],
            [
              12.392578125,
              50.14874640066278
            ],
            [
              7.998046875,
              50.14874640066278
            ],
            [
              7.998046875,
              47.45780853075031
            ]]
        ]
      }
    }
  ]
}

melgrove avatar Jun 06 '22 01:06 melgrove

maybe just judge the vertex index, should fix this bug. As below: add to shapes.ts after line 239 if (flat.holes.includes((i + 1) / 2)) continue;

YHLpuyu avatar Jul 08 '22 04:07 YHLpuyu

both solutions seem to work!

trafficonese avatar Apr 29 '23 12:04 trafficonese

It seems like it would be much slower to call .includes every iteration of the for loop when you know which element you are checking for each time, that's why I used holeIndex. @YHLpuyu 's ((i + 1) / 2) formulation is great so maybe changing line 242 of my commit to if(((i + 1) / 2) !== flat.holes[holeIndex]) { would be fastest?

melgrove avatar May 01 '23 02:05 melgrove