MCPcopy
hub / github.com/golang/geo / ExamplePolygonFromOrientedLoops

Function ExamplePolygonFromOrientedLoops

s2/example_test.go:69–118  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

67}
68
69func ExamplePolygonFromOrientedLoops() {
70 // Let's define three loops, in format World Geodetic System 1984,
71 // the format that geoJSON uses. The third loop is a hole in the second,
72 // the first loop is remote from the others. Loops 1 and 2 are counter-clockwise,
73 // while loop 3 is clockwise.
74 l1 := [][]float64{
75 {102.0, 2.0},
76 {103.0, 2.0},
77 {103.0, 3.0},
78 {102.0, 3.0},
79 }
80 l2 := [][]float64{
81 {100.0, 0.0},
82 {101.0, 0.0},
83 {101.0, 1.0},
84 {100.0, 1.0},
85 }
86 l3 := [][]float64{
87 {100.2, 0.2},
88 {100.2, 0.8},
89 {100.8, 0.8},
90 {100.8, 0.2},
91 }
92 toLoop := func(points [][]float64) *s2.Loop {
93 var pts []s2.Point
94 for _, pt := range points {
95 pts = append(pts, s2.PointFromLatLng(s2.LatLngFromDegrees(pt[1], pt[0])))
96 }
97 return s2.LoopFromPoints(pts)
98 }
99 // We can combine all loops into a single polygon:
100 p := s2.PolygonFromOrientedLoops([]*s2.Loop{toLoop(l1), toLoop(l2), toLoop(l3)})
101
102 for i, loop := range p.Loops() {
103 fmt.Printf("loop %d is hole: %t\n", i, loop.IsHole())
104 }
105 fmt.Printf("Combined area: %.7f\n", p.Area())
106
107 // Note how the area of the polygon is the area of l1 + l2 - invert(l3), because l3 is a hole:
108 p12 := s2.PolygonFromOrientedLoops([]*s2.Loop{toLoop(l1), toLoop(l2)})
109 p3 := s2.PolygonFromOrientedLoops([]*s2.Loop{toLoop(l3)})
110 p3.Invert()
111 fmt.Printf("l1+l2 = %.7f, inv(l3) = %.7f; l1+l2 - inv(l3) = %.7f\n", p12.Area(), p3.Area(), p12.Area()-p3.Area())
112 // Output:
113 // loop 0 is hole: false
114 // loop 1 is hole: false
115 // loop 2 is hole: true
116 // Combined area: 0.0004993
117 // l1+l2 = 0.0006089, inv(l3) = 0.0001097; l1+l2 - inv(l3) = 0.0004993
118}
119
120func ExampleEdgeQuery_FindEdges_findClosestEdges() {
121 // Let's start with one or more Polylines that we wish to compare against.

Callers

nothing calls this directly

Calls 8

PointFromLatLngFunction · 0.92
LatLngFromDegreesFunction · 0.92
LoopFromPointsFunction · 0.92
PolygonFromOrientedLoopsFunction · 0.92
LoopsMethod · 0.80
IsHoleMethod · 0.80
AreaMethod · 0.45
InvertMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…