X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/50a14534a51f892500ee82f867e8ab2f85b936ae..dade5f874b05c2263bbbe668a77021d487b0761b:/src/vikcoord.c?ds=inline diff --git a/src/vikcoord.c b/src/vikcoord.c index a805e4a7..c61f30ec 100644 --- a/src/vikcoord.c +++ b/src/vikcoord.c @@ -27,7 +27,7 @@ void vik_coord_convert(VikCoord *coord, VikCoordMode dest_mode) { - static VikCoord tmp; + VikCoord tmp; if ( coord->mode != dest_mode ) { if ( dest_mode == VIK_COORD_LATLON ) { @@ -56,7 +56,7 @@ void vik_coord_copy_convert(const VikCoord *coord, VikCoordMode dest_mode, VikCo static gdouble vik_coord_diff_safe(const VikCoord *c1, const VikCoord *c2) { - static struct LatLon a, b; + struct LatLon a, b; vik_coord_to_latlon ( c1, &a ); vik_coord_to_latlon ( c2, &b ); return a_coords_latlon_diff ( &a, &b ); @@ -115,3 +115,59 @@ gboolean vik_coord_equals ( const VikCoord *coord1, const VikCoord *coord2 ) else /* VIK_COORD_UTM */ return coord1->utm_zone == coord2->utm_zone && coord1->north_south == coord2->north_south && coord1->east_west == coord2->east_west; } + +static void get_north_west(struct LatLon *center, struct LatLon *dist, struct LatLon *nw) +{ + nw->lat = center->lat + dist->lat; + nw->lon = center->lon - dist->lon; + if (nw->lon < -180) + nw->lon += 360.0; + if (nw->lat > 90.0) { /* over north pole */ + nw->lat = 180 - nw->lat; + nw->lon = nw->lon - 180; + } +} + +static void get_south_east(struct LatLon *center, struct LatLon *dist, struct LatLon *se) +{ + se->lat = center->lat - dist->lat; + se->lon = center->lon + dist->lon; + if (se->lon > 180.0) + se->lon -= 360.0; + if (se->lat < -90.0) { /* over south pole */ + se->lat += 180; + se->lon = se->lon - 180; + } +} + +void vik_coord_set_area(const VikCoord *coord, const struct LatLon *wh, VikCoord *tl, VikCoord *br) +{ + struct LatLon center, nw, se; + struct LatLon dist; + + dist.lat = wh->lat/2; + dist.lon = wh->lon/2; + + vik_coord_to_latlon(coord, ¢er); + get_north_west(¢er, &dist, &nw); + get_south_east(¢er, &dist, &se); + + *((struct LatLon *)tl) = nw; + *((struct LatLon *)br) = se; + tl->mode = br->mode = VIK_COORD_LATLON; +} + +gboolean vik_coord_inside(const VikCoord *coord, const VikCoord *tl, const VikCoord *br) +{ + struct LatLon ll, tl_ll, br_ll; + + vik_coord_to_latlon(coord, &ll); + vik_coord_to_latlon(tl, &tl_ll); + vik_coord_to_latlon(br, &br_ll); + + if ((ll.lat > tl_ll.lat) || (ll.lon < tl_ll.lon)) + return FALSE; + if ((ll.lat < br_ll.lat) || (ll.lon > br_ll.lon)) + return FALSE; + return TRUE; +}