github.com/liucxer/courier@v1.7.1/h3/polygon.go (about)

     1  package h3
     2  
     3  /**
     4   * Create a bounding box from a GeoPolygon
     5   * @param polygon Input GeoPolygon
     6   * @param bboxes  Output bboxes, one for the outer loop and one for each hole
     7   */
     8  func bboxesFromGeoPolygon(polygon *GeoPolygon, bboxes []BBox) {
     9  	bboxFrom(&polygon.geofence, &bboxes[0])
    10  	for i := 0; i < polygon.numHoles; i++ {
    11  		bboxFrom(&polygon.holes[i], &bboxes[i+1])
    12  	}
    13  }
    14  
    15  /**
    16   * pointInsidePolygon takes a given GeoPolygon data structure and
    17   * checks if it contains a given geo coordinate.
    18   *
    19   * @param geoPolygon The geofence and holes defining the relevant area
    20   * @param bboxes     The bboxes for the main geofence and each of its holes
    21   * @param coord      The coordinate to check
    22   * @return           Whether the point is contained
    23   */
    24  func pointInsidePolygon(geoPolygon *GeoPolygon, bboxes []BBox, coord *GeoCoord) bool {
    25  	// Start with contains state of primary geofence
    26  	contains := pointInside(&(geoPolygon.geofence), &bboxes[0], coord)
    27  
    28  	// If the point is contained in the primary geofence, but there are holes in
    29  	// the geofence iterate through all holes and return false if the point is
    30  	// contained in any hole
    31  	if contains && geoPolygon.numHoles > 0 {
    32  		for i := 0; i < geoPolygon.numHoles; i++ {
    33  			if pointInside(&(geoPolygon.holes[i]), &bboxes[i+1], coord) {
    34  				return false
    35  			}
    36  		}
    37  	}
    38  
    39  	return contains
    40  }