golang / geo

S2 geometry library in Go
Apache License 2.0
1.69k stars 182 forks source link

Bug on Polygon Contains (linked to https://github.com/golang/geo/issues/77 ?) #78

Open gcollot opened 3 years ago

gcollot commented 3 years ago

Hello There,

we noticed that Contains method returns false sometimes.

Ex: https://play.golang.org/p/WqDx36fG4rG

package main

import (
    "fmt"
    "github.com/golang/geo/s2"
)

func main() {
    polygon := s2.PolygonFromLoops([]*s2.Loop{
        s2.LoopFromPoints([]s2.Point{
            s2.PointFromLatLng(s2.LatLngFromDegrees(-38.0, -135.0)),
            s2.PointFromLatLng(s2.LatLngFromDegrees(-38.0, 149.0)),
            s2.PointFromLatLng(s2.LatLngFromDegrees(77.0, 149.0)),
            s2.PointFromLatLng(s2.LatLngFromDegrees(77.0, -135.0)),
            s2.PointFromLatLng(s2.LatLngFromDegrees(-38.0, -135.0)),
        }),
    })

    tile := s2.PolygonFromLoops([]*s2.Loop{
        s2.LoopFromPoints([]s2.Point{
            s2.PointFromLatLng(s2.LatLngFromDegrees(37.99616267972809, 13.007812500000002)),
            s2.PointFromLatLng(s2.LatLngFromDegrees(37.99616267972809, 13.359375000000002)),
            s2.PointFromLatLng(s2.LatLngFromDegrees(38.272819658516866, 13.359375000000002)),
            s2.PointFromLatLng(s2.LatLngFromDegrees(38.272819658516866, 13.007812500000002)),
            s2.PointFromLatLng(s2.LatLngFromDegrees(37.99616267972809, 13.007812500000002)),
        }),
    })

    // +0.2 lat +0.2 lon
    tileExtended :=  s2.PolygonFromLoops([]*s2.Loop{
        s2.LoopFromPoints([]s2.Point{
            s2.PointFromLatLng(s2.LatLngFromDegrees(37.99616267972809, 13.007812500000002)),
            s2.PointFromLatLng(s2.LatLngFromDegrees(37.99616267972809, 13.559375000000002)),
            s2.PointFromLatLng(s2.LatLngFromDegrees(38.472819658516866, 13.559375000000002)),
            s2.PointFromLatLng(s2.LatLngFromDegrees(38.472819658516866, 13.007812500000002)),
            s2.PointFromLatLng(s2.LatLngFromDegrees(37.99616267972809, 13.007812500000002)),
        }),
    })

    fmt.Println(polygon.Contains(tile))
    fmt.Println(polygon.Contains(tileExtended))
}

Returns

false
true

Program exited.

However, all these geometries (tile & tileExtended) are contained in polygon.

Maybe, this might be linked to https://github.com/golang/geo/issues/77

sreekanth-cb commented 2 years ago

Yes @gcollot, this is related to #77.